Random Reflection Trivia
Wednesday, February 15 2006 - blog
Given the following class definition:
[MyAttribute]
public class Foo
{
...
}
Under what circumstances will typeof(Foo).IsDefined( typeof(MyAttribute), true ) return
false?
Hint: typeof(Foo).IsDefined( typeof(MyAttribute), true )
can be made to return true by changing one line of config.
- #1 David Smith on 2.15.2006 at 11:15 AM
-
No idea, but I'm looking forward to others' insight.
- #2 Wilco Bauwer on 2.15.2006 at 11:51 AM
-
My guess is that you have an attribute class called MyAttributeAttribute : Attribute, and you have a class called "MyAttribute". The compiler will figure out that the applied attribute 'MyAttribute' is really the MyAttributeAttribute class, because that's the only class that derives from Attribute.However, typeof(MyAttribute) just represents the non-attribute class, and (ofcourse) this type is not an attribute of Foo.
- #3 Steve Maine on 2.15.2006 at 1:45 PM
-
Wilco -- nice catch! Suspecting the compiler of doing some type resolution magic under the hood here is a reasonable thing to do, but that's not the scenario I'm thinking of here.There are only two types involved: MyAttribute and Foo.
- #4 Wilco Bauwer on 2.15.2006 at 2:38 PM
-
Hm... When you say "one line of config", are you talking about the app/web.config, or some (assembly-level) attribute (in which case I guess it could have something to do with the TypeForwardedToAttribute)?
- #5 Steve Maine on 2.15.2006 at 3:02 PM
-
Config, not assembly attributes. Specifically web.config.
