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

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


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


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:
Status:
RESOLVED FIXED

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