Bug 8037 - GetCustomAttributes returns wrong results when using NET4 and NET35 assemblies mixed
Summary: GetCustomAttributes returns wrong results when using NET4 and NET35 assemblie...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
: 6528 ()
Depends on:
Reported: 2012-10-28 15:23 UTC by Timo Dörr
Modified: 2012-11-17 08:23 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Timo Dörr 2012-10-28 15:23:14 UTC
When calling typeof(MyClass).GetCustomAttribtutes () the results may be wrong when MyClass is defined in a NET4 assembly and GetcustomAttributes is called from withing a NET35 assembly.

I've set up a minimal testcase to reproduce (the source along with .sln/.csproj is also on github for convenience: git@github.com:Dynalon/getcustomattributes-bug.git)

Steps to reproduce:

1) Create a new .NET35 library project, add the following source:

using System;
using System.Runtime.Serialization;
using System.Linq;

namespace Sample35Project
	public static class Sample35Class
		public static void Print<T> ()
			var isDataContract = typeof(T).GetCustomAttributes (typeof(DataContractAttribute), false).Any ();
			Console.WriteLine (isDataContract);

2) In the same solution, create a new .NET4 cmdline project that references the previous Sample35Project:

using System;
using System.Runtime.Serialization;
using System.Linq;
using Sample35Project;

namespace Test
	public class MyClass
		[DataMember (Name = "some-title")]
		string Title { get; set; }

	class MainClass
		public static void Main (string[] args)
			Sample35Class.Print<MyClass> ();

3) Run the program. The result will be "False", because isDataContract will evaluate to false. When running the same binaries on a windows7 machine, the output is True (which is correct).

Expected: True (isDataContract should evaluate to true).

Other notable behaviour which might help:
4) Switch the cmdline program build target to NET35. Recompile an run the program, the result will be True (which is correct).
5) Switch the Sample35Project build target to NET4 (so now both are NET4). The result will be True again (which is correct).

So basically, only if the cmdline program uses NET4 and the other project (that calls GetCustomAttributes) is NET35, there will be wrong behavior. this does not happen on a windows7 machine using microsoft's .NET VM.

Tested on Mac OS X Lion with Mono 2.10.9 and 3.0 beta, as well as on an opensuse 12.1 box using mono 2.10.6 - all affected. This bugs affects ServiceStack usage with mono, see the issue on their github: https://github.com/ServiceStack/ServiceStack/issues/307
Comment 1 Marek Safar 2012-11-11 12:50:00 UTC
Fixed in master
Comment 2 Andres G. Aragoneses 2012-11-17 08:23:44 UTC
*** Bug 6528 has been marked as a duplicate of this bug. ***