Attributes and Reflection

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
 
J

Jon Skeet [C# MVP]

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.
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top