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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jcomber@southwestwater.co.uk

    #1

    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.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.

  • Nick Hall

    #2
    Re: Passing a Delegate to a Constructor using Activator.Creat eInstance()

    Comments inline:

    <jcomber@southw estwater.co.uk> wrote in message
    news:1127984840 .590690.317770@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > 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.[/color]
    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.
    [color=blue]
    >
    > 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 }[/color]
    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 )}
    [color=blue]
    > 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.
    >[/color]

    Hope this helps,

    Nick Hall


    Comment

    • jcomber@southwestwater.co.uk

      #3
      Re: Passing a Delegate to a Constructor using Activator.Creat eInstance()

      Hi Nick,

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

      Cheers
      John.

      Comment

      Working...