C# 泛型类中的GetProperty返回null

C# 泛型类中的GetProperty返回null,c#,generics,C#,Generics,在我正在工作的项目中,有一个类定义如下: class DataImport<T> : DataBaseClass where T : IDataEntity, new() { } string tableName { get; } string Id { get; } int TypeId { get; } class DataImport<T> : DataBaseClass where T : IDataEntity, new() { public vo

在我正在工作的项目中,有一个类定义如下:

class DataImport<T> : DataBaseClass where T : IDataEntity, new()
{
}
string tableName { get; }
string Id { get; }
int TypeId { get;  }
class DataImport<T> : DataBaseClass where T : IDataEntity, new()
{
    public void WriteTableName(T arg)
    {
        string x = ((IDataEntity)arg).tableName;
        Console.WriteLine(x);
    }
}
在实现此接口的类中,有以下内容:

string IDataEntity.Id { get { return myobject_id.ToString(); } }        
int IDataEntity.TypeId { get { return 10; } }
string IDataEntity.tableName { get { return "tblObjects"; } }
比如说

DataImport
对象中,我希望执行以下操作:

string x = (T).tableName;
等等,但我当然不能这么做。 我试着将它们公开,但这是不允许的

如何访问
tableName
Id
TypeId
属性?我已经查看了
typeof(T).GetProperties()
,但是它们是
null
;在
GetFields()
中也有两个问题:

  • 您需要一个
    T
    的实例来访问非静态属性
    tableName
    ,在您的示例中,您对类型而不是实例调用
    (T).tableName
  • 由于类显式地实现了接口(
    string IDataEntity.tableName
    ),因此需要显式地将
    T
    的实例强制转换为
    IDataEntity
  • 因此,您的
    DataImport
    实现应该如下所示:

    class DataImport<T> : DataBaseClass where T : IDataEntity, new()
    {
    }
    
    string tableName { get; }
    string Id { get; }
    int TypeId { get;  }
    
    class DataImport<T> : DataBaseClass where T : IDataEntity, new()
    {
        public void WriteTableName(T arg)
        {
            string x = ((IDataEntity)arg).tableName;
            Console.WriteLine(x);
        }
    }
    
    class DataImport:DataBaseClass其中T:IDataEntity,new()
    {
    公共无效WriteTableName(T参数)
    {
    字符串x=((IDataEntity)arg).tableName;
    控制台写入线(x);
    }
    }
    
    有两个问题:

  • 您需要一个
    T
    的实例来访问非静态属性
    tableName
    ,在您的示例中,您对类型而不是实例调用
    (T).tableName
  • 由于类显式地实现了接口(
    string IDataEntity.tableName
    ),因此需要显式地将
    T
    的实例强制转换为
    IDataEntity
  • 因此,您的
    DataImport
    实现应该如下所示:

    class DataImport<T> : DataBaseClass where T : IDataEntity, new()
    {
    }
    
    string tableName { get; }
    string Id { get; }
    int TypeId { get;  }
    
    class DataImport<T> : DataBaseClass where T : IDataEntity, new()
    {
        public void WriteTableName(T arg)
        {
            string x = ((IDataEntity)arg).tableName;
            Console.WriteLine(x);
        }
    }
    
    class DataImport:DataBaseClass其中T:IDataEntity,new()
    {
    公共无效WriteTableName(T参数)
    {
    字符串x=((IDataEntity)arg).tableName;
    控制台写入线(x);
    }
    }
    
    您需要一个
    T
    的实例来调用非静态属性。你有吗?是的,我在密码里。但是我没有将参数(T)传递给方法。我刚刚意识到我需要这样做。你需要一个
    T
    的实例来调用一个非静态属性。你有吗?是的,我在密码里。但是我没有将参数(T)传递给方法。我刚刚意识到我需要这样做。我不知道如何使用t作为arg的修饰符。我可以通过以下方式获得类的特定名称:int index=typeof(T).name.IndexOf(“EntityName”);然后解析它,但我无法深入到对象中。@Ron不确定你所说的“修饰符”是什么意思
    T
    arg
    的类型。在运行时,它是您用来实例化一个特定类的
    DataImport
    @Ron的类型。也许您应该显示您到底想做什么(但我现在离线了几个小时)是的,但我不知道它是什么。类DataImport:DataBaseClass where T:IDataEntity,new(){public override int Run(int Length){int index=typeof(T).Name.IndexOf(“Entity”);//但我想知道这是什么具体的T//以便可以检查更多的数据元素//如typeof(T).GetProperty(“tblName”);但返回null。我觉得我在兜圈子。创建泛型类,然后需要知道特定类型似乎有点误解。无论如何,
    tableName
    是一个实例属性,因此您至少需要使用以实例为参数的
    GetProperty
    重载。该类型如何知道实例属性的值?正在电话中写入,因此现在无法使用更多详细信息和文档链接更新答案。我不知道如何使用t作为arg的修饰符。我可以通过以下方式获得类的特定名称:int index=typeof(t).name.IndexOf(“EntityName”);然后解析它,但我无法深入到对象中。@Ron不确定你所说的“修饰符”是什么意思。…
    T
    arg
    的类型。在运行时,它是你用来实例化
    DataImport
    的特定类的类型@Ron也许你应该显示你到底想做什么(但我现在离线了几个小时)是的,但我不知道它是什么。class DataImport:DataBaseClass where t:IDataEntity,new(){public override int Run(int Length){int index=typeof(t).Name.IndexOf(“Entity”);//但是我想知道这是什么具体的t//这样我可以检查更多的数据元素//比如typeof(t).GetProperty(“tblName”);但返回null。我觉得我在兜圈子。创建泛型类,然后需要知道特定类型似乎有点误解。无论如何,
    tableName
    是一个实例属性,因此您至少需要使用以实例为参数的
    GetProperty
    重载。该类型如何知道实例属性的值?正在电话中写入,因此无法使用更多详细信息和文档链接立即更新答案。