Create Object from Variable

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

    #1

    Create Object from Variable

    Is there a way, to create dynamicly an object from a variable in c#.

    Something like

    string myclass ="testclass" ;

    Object t = new (object)myclass ;

    then the contains a new testclass ...

    any idea how to do this??


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Create Object from Variable

    Patrick,

    Yes, you can. You can use the static CreateInstance method on the
    Activator class to do this.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Patrick" <patrick911@blu email.ch> wrote in message
    news:1066658374 .212560@fuchs.c yberlink.ch...[color=blue]
    > Is there a way, to create dynamicly an object from a variable in c#.
    >
    > Something like
    >
    > string myclass ="testclass" ;
    >
    > Object t = new (object)myclass ;
    >
    > then the contains a new testclass ...
    >
    > any idea how to do this??
    >
    >[/color]


    Comment

    • Patrick

      #3
      Re: Create Object from Variable

      Hi ... I tried to do this in that way

      object bla;

      bla =
      Activator.Creat eInstance(Syste m.Reflection.As sembly.GetExecu tingAssembly(). T
      oString(),"DynC lass");

      public class DynClass

      {

      public DynClass(){}

      }


      but it always says: "Could not load type DynClass from assembly test"

      Can you tell me whats wrong?
      Thanks



      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> schrieb
      im Newsbeitrag news:%23skn$Mxl DHA.644@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > Patrick,
      >
      > Yes, you can. You can use the static CreateInstance method on the
      > Activator class to do this.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Patrick" <patrick911@blu email.ch> wrote in message
      > news:1066658374 .212560@fuchs.c yberlink.ch...[color=green]
      > > Is there a way, to create dynamicly an object from a variable in c#.
      > >
      > > Something like
      > >
      > > string myclass ="testclass" ;
      > >
      > > Object t = new (object)myclass ;
      > >
      > > then the contains a new testclass ...
      > >
      > > any idea how to do this??
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...