473,624 Members | 2,252 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(<File Name>)
Dim ty As Type = asm.GetType(<Ob ject Name and class name>)
Dim obj As Object = Activator.Creat eInstance(ty)
obj.popupmsg()

how can I implement in C#

Thx
Nov 17 '05 #1
3 2103
Pretty much the same way, but you might need to cast the "obj" variable to
the type that has the desired method.
"Michael" <la********@gma il.com> wrote in message
news:eV******** ******@TK2MSFTN GP09.phx.gbl...
Hi all :

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

how can I implement in C#

Thx

Nov 17 '05 #2
> Dim asm As [Assembly] = [Assembly].LoadFrom(<File Name>)
Dim ty As Type = asm.GetType(<Ob ject Name and class name>)
Dim obj As Object = Activator.Creat eInstance(ty)
obj.popupmsg()
Assembly asm = Assembly.LoadFr om(<FileName>);
Type ty = asm.GetType(<Ob ject Name and class name>);
object obj = Activator.Creat eInstance(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.popupm sg();
}
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("p opupmsg", BindingFlags.In stance |
BindingFlags.Pu blic);
if (popupMethod != null)
{
popupMethod.Inv oke(obj, null);
}
HTH

--
Adam Clauss

"Michael" <la********@gma il.com> wrote in message
news:eV******** ******@TK2MSFTN GP09.phx.gbl... Hi all :

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

how can I implement in C#

Thx

Nov 17 '05 #3
"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:11******** *****@corp.supe rnews.com...
Assembly asm = Assembly.LoadFr om(<FileName>);
Type ty = asm.GetType(<Ob ject Name and class name>);
object obj = Activator.Creat eInstance(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("p opupmsg", BindingFlags.In stance |
BindingFlags.Pu blic);
if (popupMethod != null)
{
popupMethod.Inv oke(obj, null);
}


In reading your post again - since you have to use Activator.Creat eInstance
(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
1628
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 fun(){printf("HIIIIII\n");}
0
1472
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 help ensure correct behavior in a concurrent environment. That's impressive. That's *really* impressive. I know of no other language that does that - though they probably exist. I'd be interested in references to them. Normally, I think of...
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 very basic scenario: using System;
3
5504
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 use reference method.
4
2028
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 = .LoadFrom("D:\vb7Project\dynamicCall\prj01\prj01\bin\prj01.dll") Dim ty As Type = asm.GetType("prj01.dbLib")
2
3631
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
2046
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
7
8215
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
2700
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 and in turn, calls my common event handler. However, once in the common event handler code, the "this pointer" is wrong and hence I can't access other class properties and methods. Even if I invoke the dynamic method directly, the "this pointer" is...
0
8242
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7170
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.