Create Object from Variable

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

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??
 
Patrick,

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

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

object bla;

bla =
Activator.CreateInstance(System.Reflection.Assembly.GetExecutingAssembly().T
oString(),"DynClass");

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 said:
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]
- (e-mail address removed)

Patrick said:
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??
 
Back
Top