If you have an
instance of a Class, you can call instance .GetType() on it. If you simply have a
reference to a valid Type
identifier ... it's name ... you can call 'typeof(name).
using System;
namespace Whatever
{
public class TypeFun1
{
private string Name;
public TypeFun1(string name)
{
Name = name;
}
}
public class TypeFun2
{
private string Name;
public TypeFun2(string name)
{
Name = name;
}
}
public static class FunFactory<T>
{
public static T MakeNewT()
{
switch (typeof(T).Name)
{
case "TypeFun1":
return (T) Activator.CreateInstance(typeof(T), "me one");
case "TypeFun2":
return (T) Activator.CreateInstance(typeof(T), "me two");
default:
return default(T);
}
}
}
}
Test:
public void testClassCreationMethods()
{
var tclass1 = new TypeFun1("me one");
var tclass2 = new TypeFun2("me two");
var tfromfactory1 = FunFactory<TypeFun1>.MakeNewT();
var tfromfactory2 = FunFactory<TypeFun2>.MakeNewT();
bool isTypeFun1 = tclass1.GetType() == typeof(TypeFun1);
bool isSameTypes = tfromfactory1.GetType() == typeof(TypeFun1);
bool sametypr = tclass1.GetType() == tfromfactory1.GetType();
}