Bug 9225 - Conditional Attributes Inconsistent with MS .NET
Summary: Conditional Attributes Inconsistent with MS .NET
Alias: None
Product: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2013-01-03 11:14 UTC by Brandon Vaughan
Modified: 2013-01-04 10:03 UTC (History)
2 users (show)

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

The source file used to reproduce the problem. (929 bytes, application/octet-stream)
2013-01-03 11:14 UTC, Brandon Vaughan

Description Brandon Vaughan 2013-01-03 11:14:57 UTC
Created attachment 3158 [details]
The source file used to reproduce the problem.

When using a conditional attribute on an attribute class, the attribute is being created even if the condition isn't met.  The following code prints "Hello World!" in Mono and "Attribute not found." in Visual Studio.

using System;
using System.Reflection;
using System.Diagnostics;

namespace ConditionalAttributeTesting
	class MainClass
		public static void Main (string[] args)
	class UtilityClass
		[Some("Hello World!")]
		public static void HelloWorld ()
			MethodInfo methodInfo = typeof(UtilityClass).GetMethod ("HelloWorld");
			if (methodInfo != null) {
				SomeAttribute someAttribute = Attribute.GetCustomAttribute (methodInfo, typeof(SomeAttribute)) as SomeAttribute;
				if (someAttribute != null) {
				Console.WriteLine (someAttribute.myString);
				} else
					Console.WriteLine("Attribute not found.");

	public sealed class SomeAttribute : Attribute
		public string myString { get; private set; }

		public SomeAttribute (string someText)
			myString = someText;
Comment 1 Marek Safar 2013-01-04 10:03:54 UTC
Fixed in master

Note You need to log in before you can comment on or make changes to this bug.