By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

Problem with reflection casting in .NET 2.0

P: n/a
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

Nov 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 2 '06 #2

P: n/a
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

Marc Gravell wrote:
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
Nov 2 '06 #3

P: n/a
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
Nov 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.