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.
"(E-Mail Removed)" wrote:
> 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
>
>
|