473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing a Delegate to a Constructor using Activator.CreateInstance()

Hi,

I'd be grateful for some help...

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

Activator.CreateInstance(Type, ConstructorParameters())

The constructor of the object takes a delegate as the only parameter.
However, ConstructorParameters() 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("Namespace.ClassName,AssemblyName")
Dim p() as Object = {AddressOf DelegatedMethod}
Dim o As IObject = DirectCast(Activator.CreateInstance(t, p), IObject)

Is there a work around to this?

TIA for any help.

Regards
John.

Nov 21 '05 #1
2 3316
Comments inline:

<jc*****@southwestwater.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi,

I'd be grateful for some help...

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

Activator.CreateInstance(Type, ConstructorParameters())

The constructor of the object takes a delegate as the only parameter.
However, ConstructorParameters() 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("Namespace.ClassName,AssemblyName")
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(AddressOf DelegatedMethod)}
Dim o As IObject = DirectCast(Activator.CreateInstance(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
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...
6
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...
3
by: hazz | last post by:
Is Activator.CreateInstance(t,BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.CreateInstance, null, args, null) appropriate given the following; ...
7
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...
15
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...
1
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...
4
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
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...
0
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...

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.