8

我有以下课程:

public class POCOConfiguration : EntityTypeConfiguration<POCO>
{
    public POCOConfiguration()
    {

    }
}
POCOConfiguration instance = new POCOConfiguration();

如何POCO实例中获取类型?

谢谢

4

6 回答 6

13
instance.GetType().BaseType.GetGenericArguments()[0]
于 2012-05-23T07:13:47.300 回答
1

另一个答案很简单,即instance.GetType().BaseType返回类或父类的基本类型。

instance.GetType().BaseType.GetGenericArguments()[0]可以抛出异常。

于 2018-11-10T06:27:27.107 回答
0

看看那个:http: //msdn.microsoft.com/en-us/library/b8ytshk6.aspx

有一个示例显示如何获取类型(步骤 3,4 和 5)

于 2012-05-23T07:11:18.853 回答
0

如果该类具有无参数构造函数,那么您可以编写:

public class POCOConfiguration : EntityTypeConfiguration<POCO> where POCO : new()
{     
    public POCOConfiguration()
    {
        var poco = new POCO();
    } 
} 

否则,您必须使用反射,请参阅Activator类。

于 2012-05-23T07:12:20.747 回答
0

检查这个

POCOConfiguration instance = new POCOConfiguration();
Type t = instance.GetType().BaseType.GetGenericArguments()[0];
//here t is the type of POCO

将工作...

于 2012-05-23T07:15:59.060 回答
0

如果我理解正确,您想获得 POCO 的类型。然后声明一个方法并像下面这样调用它;

public Type GetTypeOfPoco<T>(EntityTypeConfiguration<T> entityTypeConfiguration)
{
    Type t = typeof(T);
    return t;
}

称它为;

Type t = GetTypeOfPoco(instance);
于 2012-05-23T07:16:52.610 回答