is it possible to initialize a class by a string variable? I have code in PHP.
<?php
$classname = "test";
$oTest = new $classname();
class test{
}
?>
how do I do this in c#?
is it possible to initialize a class by a string variable? I have code in PHP.
<?php
$classname = "test";
$oTest = new $classname();
class test{
}
?>
how do I do this in c#?
System.Activator.CreateInstance(Type.GetType(className))
The problem, however, is that C# 3.0 is a statically typed language. You can't just call random methods on the returned object. You can have the classes you might instantiate implement some common interface and cast the result of the above expression to the interface or manually use reflection to call methods on the returned object.
You can use Activator.CreateInstance. The documentation for the various overloads is here: http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx