C#: Instaniate an instance @ runtime

  • Thread starter Thread starter Action
  • Start date Start date
A

Action

Let's say
class parent
class B : parent
class C : parent
....etc. (may add later......so I don't know how many classes will there...)

I wanna to let the user type in the class name and instaniate a new instance
e.g.
string userinput = "classB"

parent temp = new SOMEFUNCTION("classB");
I wanted to ask what can SOMEFUNCTION be???

thx!
 
You almost had it.. you would do this:

if ( strUserType == "classb" )
parent objParentType = new classB();
else
parent objParentType = new classA();

Since they are both "of type Parent" - then this should work fine..
 
HI,
nope, this is not what I wanted
what I want is NO if-else
just a single line
e.g.
parent objParentType = new SOMEFUNCTION(strUserType);
 
string className = "classB";
parent obj = Activator.CreateInstance(typeof(this).Assembly.FullName,
className);

GP
 
Action said:
Let's say
class parent
class B : parent
class C : parent
...etc. (may add later......so I don't know how many classes will there...)

I wanna to let the user type in the class name and instaniate a new instance
e.g.
string userinput = "classB"

parent temp = new SOMEFUNCTION("classB");
I wanted to ask what can SOMEFUNCTION be???

thx!

These 2 lines should give you enough info:

System.Reflection.Assembly assy =
System.Reflection.Assembly.LoadFile(AssemblyFilename);
parent x = assy.CreateInstance(userinput, true) as parent;

Success,

Pj
 
I think Action wanted something that wouldn't need to be modified when new
classes are added later.

You can do this by using the Assembly class to load the assembly that
contains all of the classes, and then using the Assembly.CreateInstance
method.
 
Let's say
class parent
class B : parent
class C : parent
...etc. (may add later......so I don't know how many classes will
there...)

I wanna to let the user type in the class name and instaniate a new
instance e.g.
string userinput = "classB"

parent temp = new SOMEFUNCTION("classB");
I wanted to ask what can SOMEFUNCTION be???

thx!

Use Reflection. Here is a code snippet from the generic ADO.NET project
mentioned in my signature.

===============================================================
private string _targetAssembly; private string _targetName;
private IDbTemplate _target;
private System.Reflection.Assembly _assem;
....
public DbContext(string assembly, string typeName)
{
_targetAssembly = assembly;
_assem = Assembly.LoadFrom(_targetAssembly);
_targetName = typeName;
Type DBType = _assem.GetType(typeName);
if (DBType == null)
{//if typeName did not include namespace...
System.Type[] types = _assem.GetTypes();
for (int i=0; i < types.Length; i++)
{
Type nType = (Type)types.GetValue(i);
if (nType.FullName.IndexOf(typeName) >=0)
{
_targetName = nType.FullName;
DBType = nType;
break;
}
}
}
if (DBType == null)
{
throw new ApplicationException(
"Type name does not exist in specified assembly.");
}
_target = (IDbTemplate)Activator.CreateInstance(DBType);
}
===============================================================
The key lines for you are:

Type DBType = _assem.GetType(typeName);
...
_target = (IDbTemplate)Activator.CreateInstance(DBType);

"IDbTemplate" would be replaced by your parent class or interface name.
If the type is defined in the same assembly as the function shown, then
the "assembly" arguement can be excluded. In that case set the "_assem"
variable with the current assembly.

_assem = Assembly.GetExecutingAssembly();

Let me know if you have further questions.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
 
This answer seems cool to me!!!
thank you!!!

Günter Prossliner said:
string className = "classB";
parent obj = Activator.CreateInstance(typeof(this).Assembly.FullName,
className);

GP
 
Back
Top