(Probably obvious) .NET question - creating objects dynamically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jethro

    (Probably obvious) .NET question - creating objects dynamically

    Hi all,

    I have a solution, which contains several projects. Each project occupies
    the same namespace, and compiles to s different .NET DLL

    e.g.

    MyNamespace.Thi sClass is in one DLL
    MyNamespace.Tha tClass is in another DLL

    I wish for a .NET EXE to be able to dynamically create an instance of either
    on of the classes, given the classes name as a string.

    I have tried using System.Reflecti on.Assembly calls, but watching in debug,
    the Load call fails.

    What would the mechanism be for doing this ?

    thanks in advance.


  • Herfried K. Wagner [MVP]

    #2
    Re: (Probably obvious) .NET question - creating objects dynamically

    * "Jethro" <Jethro_uk@hotm ail.com> scripsit:[color=blue]
    > I have a solution, which contains several projects. Each project occupies
    > the same namespace, and compiles to s different .NET DLL
    >
    > e.g.
    >
    > MyNamespace.Thi sClass is in one DLL
    > MyNamespace.Tha tClass is in another DLL
    >
    > I wish for a .NET EXE to be able to dynamically create an instance of either
    > on of the classes, given the classes name as a string.
    >
    > I have tried using System.Reflecti on.Assembly calls, but watching in debug,
    > the Load call fails.[/color]

    \\\
    Private Function CreateClassByNa me( _
    ByVal PartialAssembly Name As String, _
    ByVal QualifiedClassN ame As String _
    ) As Object
    Return _
    Activator.Creat eInstance( _
    [Assembly].LoadWithPartia lName( _
    PartialAssembly Name _
    ).GetType(Quali fiedClassName) _
    )
    End Function
    ///

    Usage:

    \\\
    Dim c As Control = _
    DirectCast( _
    CreateClassByNa me( _
    "System.Windows .Forms", _
    "System.Windows .Forms.Button" _
    ), _
    Control _
    )
    With c
    .Location = New Point(10, 10)
    .Size = New Size(80, 26)
    .Text = "Hello World"
    End With
    Me.Controls.Add (c)
    ///

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: (Probably obvious) .NET question - creating objects dynamically

      Jethro,
      A variation of the Activator.Creat eInstance that I normally use.

      I use System.Type.Get Type to return a Type object for the concrete class I
      want (determined via the app.config). The strings in the app.config should
      to be in "myproject.mycl ass, myassembly" format.

      I use System.Activato r.CreateInstanc e to create an instance of the concrete
      class.

      ' VB.NET sample
      Dim s As String
      s = ConfigurationSe ttings.AppSetti ngs("myplugin")

      Dim t As Type
      t = Type.GetType(s)

      Dim obj As Object
      obj = Activator.Creat eInstance(t)


      ' Or if you want to pass parameters to the constructor
      obj = Activator.Creat eInstance(t, New Object() {p1, p2})
      ' the above calls the constructor that has two parameters.
      ' there are 7 or so overloads of CreateInstance,
      ' if you have default constructors or other needs

      Dim plugin As MyPlugInBase
      plugin = DirectCast(obj, MyBaseClass)

      I use this in a couple of projects, works very well.

      Hope this helps
      Jay




      "Jethro" <Jethro_uk@hotm ail.com> wrote in message
      news:cajo2b$haa $1@hercules.bti nternet.com...[color=blue]
      > Hi all,
      >
      > I have a solution, which contains several projects. Each project occupies
      > the same namespace, and compiles to s different .NET DLL
      >
      > e.g.
      >
      > MyNamespace.Thi sClass is in one DLL
      > MyNamespace.Tha tClass is in another DLL
      >
      > I wish for a .NET EXE to be able to dynamically create an instance of[/color]
      either[color=blue]
      > on of the classes, given the classes name as a string.
      >
      > I have tried using System.Reflecti on.Assembly calls, but watching in[/color]
      debug,[color=blue]
      > the Load call fails.
      >
      > What would the mechanism be for doing this ?
      >
      > thanks in advance.
      >
      >[/color]


      Comment

      Working...