PC Review


Reply
Thread Tools Rate Thread

Problems with Reflection

 
 
bikhod
Guest
Posts: n/a
 
      3rd Feb 2004
Hi

Using Reflection, I am able to instantiate an instance of
my class using a constructor that takes a primitive data
type (e.g. string).

I have two other constructors, one of which takes an enum
value (Enum defined by me) and the other takes an object
whose class is defined by me. In both these cases, the
CreateInstance() function call on my assembly instance
fails with "Method not found".

Is it not possible to use Reflection with non-primitive
data types?

Below is the code I am using:

// Dummy class
public class Class2
{
public Class2() {}
}

public class Class1
{
public Class1(string s) {}
public Class1(Foo bar) {}
public Class1(Class2 class2) {}
}

// Reflection code
// object[] args = new object[] { c2 }; // *FAILS*
// object[] args = new object[] { Foo.Bar }; // *FAILS*
object[] args = new object[] { "Test input" };

Assembly assemblyInstance = Assembly.LoadFrom
("ClassLibrary1.dll");

newobj = assemblyInstance.CreateInstance(CLASS_NAME,
true,
BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlag
s.Public,
null,
args,
null,
null);

// use object
 
Reply With Quote
 
 
 
 
bikhod
Guest
Posts: n/a
 
      3rd Feb 2004

Forgot to add Foo is an enum and c2 is an instance of
Class2:

public enum Foo
{
Bar,
Gub
}

and

Class2 c2 = new Class2();


>-----Original Message-----
>Hi
>
>Using Reflection, I am able to instantiate an instance of
>my class using a constructor that takes a primitive data
>type (e.g. string).
>
>I have two other constructors, one of which takes an enum
>value (Enum defined by me) and the other takes an object
>whose class is defined by me. In both these cases, the
>CreateInstance() function call on my assembly instance
>fails with "Method not found".
>
>Is it not possible to use Reflection with non-primitive
>data types?
>
>Below is the code I am using:
>
>// Dummy class
>public class Class2
>{
> public Class2() {}
>}
>
>public class Class1
>{
> public Class1(string s) {}
> public Class1(Foo bar) {}
> public Class1(Class2 class2) {}
>}
>
>// Reflection code
>// object[] args = new object[] { c2 }; //

*FAILS*
>// object[] args = new object[] { Foo.Bar }; // *FAILS*
>object[] args = new object[] { "Test input" };
>
>Assembly assemblyInstance = Assembly.LoadFrom
>("ClassLibrary1.dll");
>
>newobj = assemblyInstance.CreateInstance(CLASS_NAME,
> true,
>BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFla

g
>s.Public,
> null,
> args,
> null,
> null);
>
>// use object
>.
>

 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      3rd Feb 2004

In which assembly are Class2 and Foo defined? In the calling assembly,
the ClassLibrary1.dll assembly or some other?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Guest
Posts: n/a
 
      4th Feb 2004
All three (Foo, Class1, and Class2) are defined in an
assembly different to the one that is has the reflection
code.

Basically, I have a class library assembly that has the
above three types defined in it. I am now trying to write
a Windows App assembly that uses reflection to generically
load object from the above (and other) assemblies.


>-----Original Message-----
>
>In which assembly are Class2 and Foo defined? In the

calling assembly,
>the ClassLibrary1.dll assembly or some other?
>
>
>
>Mattias
>
>--
>Mattias Sjögren [MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/ |

http://www.dotnetinterop.com
>Please reply only to the newsgroup.
>.
>

 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      4th Feb 2004

>All three (Foo, Class1, and Class2) are defined in an
>assembly different to the one that is has the reflection
>code.


OK, the I assume that you have a design time reference to that
assembly, otherwise code such as

object[] args = new object[] { Foo.Bar };

wouldn't even compile.

If you do have a reference to it, I don't see why you want to load it
with Assembly.LoadFrom at runtime?

To learn why this doesn't work, you should read

http://www.gotdotnet.com/team/clr/Lo...Isolation.aspx
http://blogs.msdn.com/suzcook/



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Reflection Problems GhostInAK Microsoft VB .NET 0 7th Jun 2006 04:02 AM
Com interop, reflection & other problems... Francesco Mancini Microsoft C# .NET 1 12th Apr 2005 06:18 PM
Re: Problems with Reflection please help Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 1 7th Apr 2005 07:29 PM
Reflection problems =?Utf-8?B?aG93YXJkIGRpZXJraW5n?= Microsoft Dot NET Framework 2 11th Nov 2004 10:11 PM
Reflection & InvokeMember problems!! Karthik Vazhkudai Microsoft VB .NET 5 18th Sep 2003 10:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 AM.