473,499 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic call object in C#

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
Nov 17 '05 #1
3 2097
Pretty much the same way, but you might need to cast the "obj" variable to
the type that has the desired method.
"Michael" <la********@gmail.com> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #2
> 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" <la********@gmail.com> wrote in message
news:eV**************@TK2MSFTNGP09.phx.gbl... 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

Nov 17 '05 #3
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:11*************@corp.supernews.com...
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
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1616
by: prashna | last post by:
Hi all, Is'nt a function invocation through a function pointer is dynamic binding? For example consider the following program 1 int main() 2 { 3 int (*fun_ptr)(); 4 int...
0
1466
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
2
673
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
3
5488
by: Tom | last post by:
Can I dynamic call vb.net object such like vb6 as sample dim A as object set a = createobject("myobject.test") a.open() now mypbject.test is create vb.net . How can I dynamic call without...
4
2019
by: Tom | last post by:
Hi .Net Professional : In recent , I starting to learn about dynamic call .net object in vb.net. Before, some IT people teach me use the following method and its work !! Dim asm As =...
2
3621
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
2040
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
7
8203
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
0
2689
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.