Attributes and Reflection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Wierd stuff. Anyone seen this?

I load an assembly using Reflection (Assembly.LoadFrom), loop through public
classes in it and look at the class attributes (Type.GetCustomAttributes).
The loaded assembly contains a class which is annotated with my custom
attribute (which is referenced from another assembly) and this is returned.

The problem is that I can't cast the object returned by GetCustomAttributes
to my custom attribute even though the type name of the returned object is
the same as the type name of my custom attribute. "is" and "as" operators
both fail. Why is this?

Tried on both .NET 1.1 and 2.0b2. Same result.

TIA
Tim
 
Tim Haynes said:
Wierd stuff. Anyone seen this?

I load an assembly using Reflection (Assembly.LoadFrom), loop through public
classes in it and look at the class attributes (Type.GetCustomAttributes).
The loaded assembly contains a class which is annotated with my custom
attribute (which is referenced from another assembly) and this is returned.

The problem is that I can't cast the object returned by GetCustomAttributes
to my custom attribute even though the type name of the returned object is
the same as the type name of my custom attribute. "is" and "as" operators
both fail. Why is this?

Do you have the attribute defined in two assemblies, by any chance? If
so, chances are you're running into the problem described here:
http://www.pobox.com/~skeet/csharp/plugin.html

If not, could you post a short but complete program which demonstrates
the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks. Problem solved. The problem occurs if I load the assembly which has
the attribute class in, in addition to the reerenced one. Dumb. Sorry for
wasting your time.

Tim
 
Tim Haynes said:
Thanks. Problem solved. The problem occurs if I load the assembly which has
the attribute class in, in addition to the reerenced one. Dumb. Sorry for
wasting your time.

Hey, if I objected to answering people's questions, I wouldn't post on
newsgroups ;)
 
Back
Top