473,775 Members | 2,434 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 3359
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
2803
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
1194
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
5201
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
9454
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
10267
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...
0
10106
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...
0
9915
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
8939
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
7463
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
5358
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...
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.