C# - 通过反射获取泛型类型的所有属性

时间:2018-04-01 21:19:12

标签: c# generics reflection

我的代码:

public abstract class Entity
{
    // I need a method here
}

public abstract class ValueObject<T> where T : ValueObject<T>
{
}

public class User : Entity
{
    public Password Password { get; private set; }
}

public class Password : ValueObject<Password>
{
}

我需要Entity类中的一个方法来获取ValueObject

类型的所有属性

[编辑1]

我尝试过这样的事情:

    var properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);

    foreach(var property in properties)
    {
       var type = property.PropertyType;

       if (property.PropertyType.IsSubclassOf(typeof(ValueObject<type>)) // Here is an error in "type"
       {
            // Here is other unnecessary things. Some things I already know how to do.
       }
    }

0 个答案:

没有答案