Monday, August 16, 2010
Monday, August 9, 2010
How to initialze an generic Type by default constructor
class Program
{
static void Main(string[] args)
{
var testObj = BuildGenericType<TestClass>();
testObj.Member1 = "tgest";
Console.WriteLine(testObj.Member1);
Console.Read();
}
static T BuildGenericType<T>()
{
var cInfo = typeof(T).GetConstructor(new Type[] { });
T targetObj = (T)cInfo.Invoke(new object[] { });
return targetObj;
}
class TestClass
{
public string Member1;
public int Member2;
}
}
Subscribe to:
Posts (Atom)