C# 如何从基类中获取子类的“Type”

C# 如何从基类中获取子类的“Type”,c#,reflection,attributes,C#,Reflection,Attributes,我有一个抽象基类,我想在其中实现一个方法,该方法将检索继承类的属性。像这样的 public abstract class MongoEntityBase : IMongoEntity { public virtual object GetAttributeValue<T>(string propertyName) where T : Attribute { var attribute = (T)typeof(this).GetCustomAttribute(

我有一个抽象基类,我想在其中实现一个方法,该方法将检索继承类的属性。像这样的

public abstract class MongoEntityBase : IMongoEntity {

    public virtual object GetAttributeValue<T>(string propertyName) where T : Attribute {
        var attribute = (T)typeof(this).GetCustomAttribute(typeof(T));
        return attribute != null ? attribute.GetType().GetProperty(propertyName).GetValue(attribute, null) : null;
    }
}
公共抽象类MongoEntityBase:IMongoEntity{
公共虚拟对象GetAttributeValue(字符串propertyName),其中T:Attribute{
var属性=(T)typeof(this).GetCustomAttribute(typeof(T));
返回属性!=null?attribute.GetType().GetProperty(propertyName).GetValue(attribute,null):null;
}
}
并且像这样实施

[MongoDatabaseName("robotdog")]
[MongoCollectionName("users")]
public class User : MonogoEntityBase {
    public ObjectId Id { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string password { get; set; }

    public IEnumerable<Movie> movies { get; set; }
}
[MongoDatabaseName(“机器人狗”)]
[MongoCollectionName(“用户”)]
公共类用户:MonogEntityBase{
公共对象Id{get;set;}
[必需]
[数据类型(数据类型.电子邮件地址)]
公共字符串电子邮件{get;set;}
[必需]
[数据类型(数据类型.密码)]
公共字符串密码{get;set;}
公共IEnumerable电影{get;set;}
}
当然,对于上面的代码,
GetCustomAttribute()
不是一个可用的方法,因为它不是一个具体的类


抽象类中的
typeof(this)
需要更改为什么才能访问继承类?或者这不是一个好的实践,我应该在继承类中实现这个方法吗?

您应该使用
this.GetType()
。这将为您提供实例的实际具体类型

因此,在这种情况下:

public virtual object GetAttributeValue<T>(string propertyName) where T : Attribute {
    var attribute = this.GetType().GetCustomAttribute(typeof(T));
    return attribute != null ? attribute.GetType().GetProperty(propertyName).GetValue(attribute, null) : null;
}
然后
this.GetType()
将返回
AdministrativeUser


此外,这意味着您可以在
abstract
基类之外实现
GetAttributeValue
方法。您不需要实现者从
MongoEntityBase
继承

public static class MongoEntityHelper
{
    public static object GetAttributeValue<T>(IMongoEntity entity, string propertyName) where T : Attribute 
    {
        var attribute = (T)entity.GetType().GetCustomAttribute(typeof(T));
        return attribute != null ? attribute.GetType().GetProperty(propertyName).GetValue(attribute, null) : null;
    }
}
公共静态类MongoEntityHelper
{
公共静态对象GetAttributeValue(IMongoEntity实体,字符串propertyName),其中T:Attribute
{
var attribute=(T)entity.GetType().GetCustomAttribute(typeof(T));
返回属性!=null?attribute.GetType().GetProperty(propertyName).GetValue(attribute,null):null;
}
}
(如果愿意,也可以将其实现为扩展方法)

typeof(此)
不会编译


您正在搜索的是
this.GetType()

不应该
User
继承自
MongoEntityBase
?您是对的,谢谢。我修好了
public static class MongoEntityHelper
{
    public static object GetAttributeValue<T>(IMongoEntity entity, string propertyName) where T : Attribute 
    {
        var attribute = (T)entity.GetType().GetCustomAttribute(typeof(T));
        return attribute != null ? attribute.GetType().GetProperty(propertyName).GetValue(attribute, null) : null;
    }
}