CreateInstance OK but calling dynamic method fails

  • Thread starter Thread starter eric_mamet_test
  • Start date Start date
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
 
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
 
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
 
José,

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


Thanks a lot, much appreciated


Eric
 
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.
 
Back
Top