reflection question (advanced)


M

Matt

Hi,

Is it possible to write a method which can take both a normal
instansiated type and reflection instansiated type?

For example, the following code doesn't work. It throws a casting
exception claiming that a type cannot be converted to another type
(they are both the same reported type - RoundedRectangle):

///code that sets classThatImplementsFactorySuppliedInterface by
enumerating types in an assembly. Works fine
///...
//problem code
ConstructorInfo cInfo =
classThatImplementsFactorySuppliedInterface.GetConstructor(argumentTypes);
object o = cInfo.Invoke(new object[] { });
Console.WriteLine(((RoundedRectangle)o).ZIndex.ToString()); //throws
casting exception

Perhaps this isn't possible - i'd like to write a factory method which
can select a dll dynamically and invoke its interface methods to
provide data access. The factory method can also opt to instansiate a
concrete type declared in the executing assembly (which implements the
interface).

Is this even possible?
Thanks in advance
Matt
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Matt said:
Is it possible to write a method which can take both a normal
instansiated type and reflection instansiated type?

For example, the following code doesn't work. It throws a casting
exception claiming that a type cannot be converted to another type
(they are both the same reported type - RoundedRectangle):

I suspect you'll find that they're not *really* the same type though.
Usually this happens if one type was loaded from one DLL, and another
was loaded from a different DLL (possibly with the same name but from a
different directory).
 
Ad

Advertisements

M

Matt

I suspect you'll find that they're not *really* the same type though.
Usually this happens if one type was loaded from one DLL, and another
was loaded from a different DLL (possibly with the same name but from a
different directory).

I see, thanks, that worked - by re-jigging my test code so that each
type derived from a common dll. Excellent. Thanks Jon
 

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