CreateInstance OK but calling dynamic method fails

E

eric_mamet_test

I am trying to create a class at runtime from some library and call its
method dynamically but it keeps failing when calling the method...

In a separate DLL, I have a class as follows

namespace MyClassLibrary
{
public class SimpleClass
{
public string SayHello()
{
return "Hello";
}
}
}

In a console app, I load an assembly from my DLL file, create a
SimpleClass object

myAssembly = Assembly.LoadFrom(MyDLLFilename);
object someObject =
myAssembly.CreateInstance("MyClassLibrary.SimpleClass");

so far so good, it looks like someObject is created
but then it fails if I try to cast my object to a SimpleClass in order
to call its properties...

string MyString = ((MyClassLibrary.SimpleClass) someObject).SayHello();

Either there is a bug or I don't understand (I suspect the second!)


Thanks for any help


Eric
 
?

=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=

Hello, Eric:

I'm not an expert but I think that you can't cast to a type unknown at compile time.
You may use the MethodInfo class:

myAssembly = Assembly.LoadFrom(MyDLLFilename);
object someObject = myAssembly.CreateInstance("MyClassLibrary.SimpleClass");
MethodInfo theMethod = someObject.GetType.GetMethod("SayHello");
theMethod.Invoke(someObject, null);

Regards.


<[email protected]> escribió en el mensaje |I am trying to create a class at runtime from some library and call its
| method dynamically but it keeps failing when calling the method...
|
| In a separate DLL, I have a class as follows
|
| namespace MyClassLibrary
| {
| public class SimpleClass
| {
| public string SayHello()
| {
| return "Hello";
| }
| }
| }
|
| In a console app, I load an assembly from my DLL file, create a
| SimpleClass object
|
| myAssembly = Assembly.LoadFrom(MyDLLFilename);
| object someObject =
| myAssembly.CreateInstance("MyClassLibrary.SimpleClass");
|
| so far so good, it looks like someObject is created
| but then it fails if I try to cast my object to a SimpleClass in order
| to call its properties...
|
| string MyString = ((MyClassLibrary.SimpleClass) someObject).SayHello();
|
| Either there is a bug or I don't understand (I suspect the second!)
|
|
| Thanks for any help
|
|
| Eric
 
E

Eric Le Bouffon

Actually, I made my example simpler for the sake of the message.

In fact, I declared a base class, my SimpleClass inherits from the base
class and I handle methods from the base class only (hence known at
compile time).
The overall reason is to try and implement something like the Provider
pattern (I think that's how it's called).

Anyway, I'll give MethodInfo a shot.


Thanks
 
E

Eric Le Bouffon

José,

You may not be an "expert" but you got it spot on this time!


Thanks a lot, much appreciated


Eric
 
G

Guest

I have called CreateInstance in this fashion and it works:

Class1 class1;

assmbly = Assembly.LoadFrom(dll);
class1 = (Class1)assembly.CreateInstance(className,
true,
BindingFlags.Default,
null,
null,
new CultureInfo(1),
null );

The time when I got an error similar to yours was when it was unable to find
the dll. Anyways, looks like your issue has been resolved.
 

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