473,788 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a Delegate to a Constructor using Activator.Creat eInstance()

Hi,

I'd be grateful for some help...

I'm trying to create a late bound object using:

Activator.Creat eInstance(Type, ConstructorPara meters())

The constructor of the object takes a delegate as the only parameter.
However, ConstructorPara meters() is an object array and because a
Delegate is not derived from System.Object, it won't compile.

Here's an example of what I'm trying to achieve:

Dim t As Type = Type.GetType("N amespace.ClassN ame,AssemblyNam e")
Dim p() as Object = {AddressOf DelegatedMethod }
Dim o As IObject = DirectCast(Acti vator.CreateIns tance(t, p), IObject)

Is there a work around to this?

TIA for any help.

Regards
John.

Nov 21 '05 #1
2 3368
Comments inline:

<jc*****@southw estwater.co.uk> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi,

I'd be grateful for some help...

I'm trying to create a late bound object using:

Activator.Creat eInstance(Type, ConstructorPara meters())

The constructor of the object takes a delegate as the only parameter.
However, ConstructorPara meters() is an object array and because a
Delegate is not derived from System.Object, it won't compile. This is not true. ALL classes in the framework derive directly or
indirectly from System.Object; therefore an object of any class can be
assigned to a variable of type Object.

Here's an example of what I'm trying to achieve:

Dim t As Type = Type.GetType("N amespace.ClassN ame,AssemblyNam e")
Dim p() as Object = {AddressOf DelegatedMethod } This is your problem. Normally when using the AddressOf operator VB can
infer the type of delegate you are trying to create from the type of the
variable your are assigning the result to and create it automatically. In
this case your are assigning to an Object and VB has no idea what type of
delegate to create. You need to give it a helping hand in this case by
instantiating a specific Delegate type. For instance

Private Delegate Sub MyDelegate()
Dim p() as Object = {New MyDelegate(Addr essOf DelegatedMethod )}
Dim o As IObject = DirectCast(Acti vator.CreateIns tance(t, p), IObject)

Is there a work around to this?

TIA for any help.

Regards
John.


Hope this helps,

Nick Hall
Nov 21 '05 #2
Hi Nick,

That worked a treat. Thanks very much for your help.

Cheers
John.

Nov 21 '05 #3

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

Similar topics

3
3633
by: Marco | last post by:
Howdy! Given that: Type type = Type.GetType("System.Windows.Forms.Button"); Question: How do I obtain a Button object by only knowing the type object. In other words, how do I fire the constructor of a type object (Button(), in this case) ??? Thank you and God bless.
6
2804
by: Catherine Jones | last post by:
Hi all, we need urgent help in a matter. We are trying to pass a COM object from the client to server and are facing some problems in the same. We've our client in C# as well as the Server in C# and we're using remoting for client to server communication.
3
5746
by: hazz | last post by:
Is Activator.CreateInstance(t,BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args, null) appropriate given the following; ************************************** public interface IPasswordProvider { string GetPassword(Token token); } public DBPassword()
7
12036
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass a token into the instantiated class DBPassword and return a string; ************************************** namespace DBPasswordProvider public class DBPassword {
15
5753
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the remote publisher, so I used delegate that the user will be able to set on it his own function for that purpuse. The trouble is that this delegate must not be static because there may be many subscribers, and each subscriber may have different...
1
2013
by: Daniel | last post by:
How do I call a delegate in late bound C# dll? there some way to do this w/ a sharedinterface file? any examples? i tried this but it doesnt work: (oType.GetMethod("IOCTLJOB").Invoke(pObj, new object { pClass1.m_job } )).ToString(); and it returns the error: Additional information: Object type cannot be converted to target type.
4
2430
by: KC Eric | last post by:
Hi all, I have a dll file, it has a class, say: class Temp, this class has a function which has a delegate as a parameter, say: public void Test(GameOverHandler _overHandler)
2
1195
by: ThePinkPanther | last post by:
Hi. I'm looking for a way to make a delegate point to a constructor. I'm not too sure i'm explaining it right,so here's a sample code to demonstrate. ----- using System.Windows.Forms; class xyz { public delegate Form MyDelegate (string input);
4
5202
by: Rob Blackmore | last post by:
Hi, Can anyone advise how to call a private constructor using reflection? I currently get the above error which is rectified by changing the New() to Public from Friend but I ideally wish to leave it as Friend to prevent it being created outside of this project. Thanks in advance Rob
0
9656
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
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10113
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
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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
7519
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
6750
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();...
1
4074
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 we have to send another system
2
3677
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.