PC Review


Reply
Thread Tools Rate Thread

Dynamic call object in C#

 
 
Michael
Guest
Posts: n/a
 
      29th Sep 2005
Hi all :

in vb.net I know use the follow code can implement dynamic call
Dim asm As [Assembly] = [Assembly].LoadFrom(<FileName>)
Dim ty As Type = asm.GetType(<Object Name and class name>)
Dim obj As Object = Activator.CreateInstance(ty)
obj.popupmsg()

how can I implement in C#

Thx


 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      29th Sep 2005
Pretty much the same way, but you might need to cast the "obj" variable to
the type that has the desired method.


"Michael" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all :
>
> in vb.net I know use the follow code can implement dynamic call
> Dim asm As [Assembly] = [Assembly].LoadFrom(<FileName>)
> Dim ty As Type = asm.GetType(<Object Name and class name>)
> Dim obj As Object = Activator.CreateInstance(ty)
> obj.popupmsg()
>
> how can I implement in C#
>
> Thx
>



 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      29th Sep 2005
> Dim asm As [Assembly] = [Assembly].LoadFrom(<FileName>)
> Dim ty As Type = asm.GetType(<Object Name and class name>)
> Dim obj As Object = Activator.CreateInstance(ty)
> obj.popupmsg()


Assembly asm = Assembly.LoadFrom(<FileName>);
Type ty = asm.GetType(<Object Name and class name>);
object obj = Activator.CreateInstance(ty);

Here you run into a problem (and I didn't realize your above code would work
in VB.Net).
The obj variable is of type 'object' - and the object class obviously does
not define the popupmsg() method.

You have two options.
1) If the code you are writing contains the definition for the 'real' type
of obj, you can cast it, and then call popupmsg:
SomeType someType = obj as SomeType;
if (someType != null)
{
someType.popupmsg();
}


2) If you do not have this definition, you will have to use reflection to
access the method and call it. Note - you may need to change the flags
passed to GetMethod:

MethodInfo popupMethod = ty.GetMethod("popupmsg", BindingFlags.Instance |
BindingFlags.Public);
if (popupMethod != null)
{
popupMethod.Invoke(obj, null);
}


HTH

--
Adam Clauss

"Michael" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all :
>
> in vb.net I know use the follow code can implement dynamic call
> Dim asm As [Assembly] = [Assembly].LoadFrom(<FileName>)
> Dim ty As Type = asm.GetType(<Object Name and class name>)
> Dim obj As Object = Activator.CreateInstance(ty)
> obj.popupmsg()
>
> how can I implement in C#
>
> Thx
>



 
Reply With Quote
 
Adam Clauss
Guest
Posts: n/a
 
      29th Sep 2005
"Adam Clauss" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Assembly asm = Assembly.LoadFrom(<FileName>);
> Type ty = asm.GetType(<Object Name and class name>);
> object obj = Activator.CreateInstance(ty);


> 2) If you do not have this definition, you will have to use reflection to
> access the method and call it. Note - you may need to change the flags
> passed to GetMethod:
>
> MethodInfo popupMethod = ty.GetMethod("popupmsg", BindingFlags.Instance |
> BindingFlags.Public);
> if (popupMethod != null)
> {
> popupMethod.Invoke(obj, null);
> }


In reading your post again - since you have to use Activator.CreateInstance
(rather than just normally constructing the object), you will almost
certainly need to use the second option (Reflection).


--
Adam Clauss


 
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
Fw: Referenced COM Object Versus Dynamic Load Of COM Object Claire Microsoft C# .NET 2 9th Oct 2008 03:17 AM
How to call a dynamic function from Dynamic Menu? tsair Microsoft C# .NET 1 14th Apr 2005 09:20 AM
Dynamic call .net object Tom Microsoft VB .NET 3 26th Oct 2004 02:26 AM
Object reference not set to an instance of an object error during netapi32 call Kurt Van Campenhout Microsoft VB .NET 5 19th Nov 2003 11:46 PM
Object reference not set to an instance of an object error during netapi32 call Kurt Van Campenhout Microsoft VB .NET 1 18th Nov 2003 09:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 PM.