1、使用反射创建泛型对象
public T Method<T>(string param)
{
var obj = Activator.CreateInstance(typeof(T));
// 设置默认值
var col = obj.GetType().GetProperty("属性名");
if (col != null)
{
col.SetValue(obj, "属性值")
}
return (T)obj;
}
2、实例化泛型对象
public T Method<T>(string param) where T: class, new();
{
return new T();
}
3、对象转换为泛型
return (T)(object)obj
标签:obj,c#,对象,实例,泛型,return,net,col
From: https://www.cnblogs.com/fanyang1/p/17205605.html