Bug 9225

Summary: Conditional Attributes Inconsistent with MS .NET
Product: [Mono] Compilers Reporter: Brandon Vaughan <brandon.vaughan>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: The source file used to reproduce the problem.

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)
		{
			UtilityClass.HelloWorld();
		}
	}
	
	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.");
				}
			}
		}
	}

	[AttributeUsage(AttributeTargets.All)]
	[Conditional("NOT_DEFINED")]
	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