473,320 Members | 1,910 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,320 software developers and data experts.

reflection question (advanced)

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.GetCon structor(argumentTypes);
object o = cInfo.Invoke(new object[] { });
Console.WriteLine(((RoundedRectangle)o).ZIndex.ToS tring()); //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

Oct 17 '07 #1
2 1291
Matt <m.*****@lbs-ltd.comwrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 17 '07 #2
On 17 Oct, 21:38, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Matt <m.ow...@lbs-ltd.comwrote:
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).

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
I see, thanks, that worked - by re-jigging my test code so that each
type derived from a common dll. Excellent. Thanks Jon

Oct 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Dave Wurtz | last post by:
All, Is it possible to get all of the current namespaces that are available through reflection? What I am trying to do is get back all of the namespaces, then classes within the namespaces,...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
4
by: Dov Tendler | last post by:
Hi All, I would like to get MethodInfo of current instantiated generic method call. Consider the following function call: C.f<int>(5); Where f is defined as follows: class C {
0
by: optictygre | last post by:
Databinding with reflection I have a class, SmartString: Public Class SmartString ...Psuedo code follows Public Property Value() as String Return Me.Text
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
7
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.