我有以下课程:
public class POCOConfiguration : EntityTypeConfiguration<POCO>
{
public POCOConfiguration()
{
}
}
POCOConfiguration instance = new POCOConfiguration();
如何POCO
从实例中获取类型?
谢谢
我有以下课程:
public class POCOConfiguration : EntityTypeConfiguration<POCO>
{
public POCOConfiguration()
{
}
}
POCOConfiguration instance = new POCOConfiguration();
如何POCO
从实例中获取类型?
谢谢
instance.GetType().BaseType.GetGenericArguments()[0]
另一个答案很简单,即instance.GetType().BaseType
返回类或父类的基本类型。
instance.GetType().BaseType.GetGenericArguments()[0]
可以抛出异常。
看看那个:http: //msdn.microsoft.com/en-us/library/b8ytshk6.aspx
有一个示例显示如何获取类型(步骤 3,4 和 5)
如果该类具有无参数构造函数,那么您可以编写:
public class POCOConfiguration : EntityTypeConfiguration<POCO> where POCO : new()
{
public POCOConfiguration()
{
var poco = new POCO();
}
}
否则,您必须使用反射,请参阅Activator类。
检查这个
POCOConfiguration instance = new POCOConfiguration();
Type t = instance.GetType().BaseType.GetGenericArguments()[0];
//here t is the type of POCO
将工作...
如果我理解正确,您想获得 POCO 的类型。然后声明一个方法并像下面这样调用它;
public Type GetTypeOfPoco<T>(EntityTypeConfiguration<T> entityTypeConfiguration)
{
Type t = typeof(T);
return t;
}
称它为;
Type t = GetTypeOfPoco(instance);