Problem with reflection casting in .NET 2.0

  • Thread starter Thread starter TheLongshot
  • Start date Start date
T

TheLongshot

I'm in the process of converting a VS 2003 app to VS 2005. Right now
I've run into a problem which I can't figure out. The following
statement:

return
(IDataProviderBase)(((ConstructorInfo)cache["IDataProviderBase"]).Invoke(null)
);

worked fine in 2003, but gives me an InvalidCastException in 2005. The
object getting created is derived from IDataProviderBase, and when I
try to cast to the object's class, I still get an InvalidCastException.

All of this is in the same DLL, so there shouldn't be any versioning
issues as far as I know.

Any clue what the problem is?

Jason
 
I think you should break this line into sections and see where the exception
is actually happening. If (as you say) the object created does support that
interface, then this shouldn't be the issue. My guess would be somewhere in
the cache[string] code that you didn't show...

Marc
 
I did break it down to cast it to the actual object it was creating,
and it was still giving me the exception. For some reason, I can't
cast the object even to itself.

Really, I'm stumped here.

Jason
 
I wonder... you wouldn't have this interface declared (separately) in two
asemblies, would you? It doesn't matter if they are named the same and have
the same source code: if they are in different assemblies they *are*
different interfaces...

Assuming this isn't the problem, what happens if you put:
object obj = ((ConstructorInfo)cache["IDataProviderBase"]).Invoke(null);
Debug.WriteLine(obj.GetType().FullName);
then do you get what you expect? Alternatively you could use a break
point...

Marc
 

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

Back
Top