PC Review


Reply
Thread Tools Rate Thread

CreateInstance OK but calling dynamic method fails

 
 
eric_mamet_test@yahoo.co.uk
Guest
Posts: n/a
 
      13th Apr 2005
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Jos=E9_Manuel_Ag=FCero?=
Guest
Posts: n/a
 
      13th Apr 2005
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.


<(E-Mail Removed)> escribió en el mensaje news:(E-Mail Removed)...
|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

 
Reply With Quote
 
Eric Le Bouffon
Guest
Posts: n/a
 
      14th Apr 2005
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

 
Reply With Quote
 
Eric Le Bouffon
Guest
Posts: n/a
 
      15th Apr 2005
José,

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


Thanks a lot, much appreciated


Eric

 
Reply With Quote
 
=?Utf-8?B?UlI=?=
Guest
Posts: n/a
 
      20th Apr 2005
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
>
>

 
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
ExapndableObjectConvert.CreateInstance method is ignored Andrew2 Microsoft ASP .NET 0 10th Feb 2008 07:10 PM
CoCreateInstance fails when COM method calling it was called fromassembly ranin02 Microsoft Dot NET 6 16th Jan 2008 04:41 PM
TypeConverter.CreateInstance-Method Oskar Vaia Microsoft Dot NET Framework 1 27th May 2004 01:58 PM
TypeConverter.CreateInstance-Method Oskar Vaia Microsoft VB .NET 3 27th May 2004 01:58 PM
Child object calling dynamic-parent method Kelmen Wong Microsoft C# .NET 2 23rd Dec 2003 12:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:44 AM.