473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with reflection casting in .NET 2.0

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
3 1146
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Angelos Karantzalis | last post by:
Hi guys, I'm trying to load a class instance dynamically, and then cast it to it's base type so I can use it in my app. More specifically, I'm dynamically instantiating a...
1
by: Roman Muntyanu | last post by:
Hi all, I created object of MyClass : ISomeInterface Then I created object of this class using reflection mechanism. ISomeInterface instance = (ISomeInterface)constructorInfo.Invoke(new...
2
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work...
6
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
0
by: John Lafrowda | last post by:
Dear all, here is a simple problem that I cannot overcome: I'm trying to write a client/server application in Visual Basic .net. The server is an executable (.exe) project, the clients are class...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
15
by: Anthony Paul | last post by:
Let's say that I would like a generic type that supports Min/Max properties and can be double or integer or even datetime if need be, something flexible. So I go about creating the following...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.