获取属性的名称并声明类

本文关键字:声明 属性 获取 | 更新日期: 2023-09-27 18:35:19

如何使用反射来获取泛型类型属性的名称和声明类。目的是在我阅读到目前为止尚未写入任何内容的属性时获得异常。其中一个问题是检查必须独立于声明类。

value.GetType().DeclaringType总是null.

using System;
namespace TestCodeContract
{
    public struct CheckForNull<T> where T : struct
    {
        private T? backingField;
        public static implicit operator T(CheckForNull<T> value)
        {
            if (!(value.backingField.HasValue))
            {
                var t1 = value.GetType().DeclaringType; // always null.
                var propertyName = "Delta"; // TODO get from Reflection
                var className = "ContractClass"; // TODO get from Reflection
                var msg = String.Format("Proprety '{0}' in class '{1}' is not initialized", propertyName, className);
                throw new ApplicationException(msg);
            }
            return value.backingField.Value;
        }
        public static implicit operator CheckForNull<T>(T value)
        {
            return new CheckForNull<T> { backingField = value };
        }
    }
    public class ContractClass
    {
        public CheckForNull<int> Delta { get; set; }
        public void Test1()
        {
            int x = Delta; // Wanted: "Property 'Delta' in class 'ContractClass' is not initialized"
        }
    }
}

获取属性的名称并声明类

不,你不能那样做。我会建议这样的东西:

// You could do this without the constraint, with a bit of extra work.
public class ReadOnlyAfterWrite<T> where T : struct
{
    private T? value;
    private readonly string property;
    private readonly string type;
    public ReadOnlyAfterWrite(string property, string type)
    {
        this.property = property;
        this.type = type;
    }
    public T Value
    {
        get
        {
            if (value == null)
            {
                // Use type and property here
                throw new InvalidOperationException(...);
            }
            return (T) value;
        }
        set { this.value = value; }
    }
}
public class ContractClass
{
    // This is what I'd do in C# 6. Before that, probably just use string literals.
    private readonly ReadOnlyAfterWrite<int> delta =
        new ReadOnlyAfterWrite(nameof(Delta), nameof(ContractClass));
    public int Delta
    {
        get { return delta.Value; }
        set { delta.Value = value; }
    }
}

虽然它在实现中不是很干净,但我认为它是一个更好的公共 API - 它受到保护的事实对调用者来说是不可见的,他们只看到一个int属性。