8

我的项目中有一些单元测试,我们希望能够设置一些具有私有设置器的属性。目前我正在通过反射和这种扩展方法来做到这一点:

public static void SetPrivateProperty(this object sourceObject, string propertyName, object propertyValue)
{
    sourceObject.GetType().GetProperty(propertyName).SetValue(sourceObject, propertyValue, null);
}

假设我有一个这样的 TestObject:

public class TestObject
{
    public int TestProperty{ get; private set; }
}

然后我可以在我的单元测试中调用它,如下所示:

myTestObject.SetPrivateProperty("TestProperty", 1);

但是,我想在编译时验证属性名称,因此我希望能够通过表达式传递属性,如下所示:

myTestObject.SetPrivateProperty(o => o.TestProperty, 1);

我怎样才能做到这一点?

4

2 回答 2

9

如果 getter 是公开的,那么以下应该可以工作。它将为您提供如下所示的扩展方法:

var propertyName = myTestObject.NameOf(o => o.TestProperty);

它需要一个公共吸气剂。我希望,在某个时候,像这样的反射功能会融入到语言中。

public static class Name
{
    public static string Of(LambdaExpression selector)
    {
        if (selector == null) throw new ArgumentNullException("selector");

        var mexp = selector.Body as MemberExpression;
        if (mexp == null)
        {
            var uexp = (selector.Body as UnaryExpression);
            if (uexp == null)
                throw new TargetException(
                    "Cannot determine the name of a member using an expression because the expression provided cannot be converted to a '" +
                    typeof(UnaryExpression).Name + "'."
                );
            mexp = uexp.Operand as MemberExpression;
        }

        if (mexp == null) throw new TargetException(
            "Cannot determine the name of a member using an expression because the expression provided cannot be converted to a '" +
            typeof(MemberExpression).Name + "'."
        );
        return mexp.Member.Name;
    }

    public static string Of<TSource>(Expression<Func<TSource, object>> selector)
    {
        return Of<TSource, object>(selector);
    }

    public static string Of<TSource, TResult>(Expression<Func<TSource, TResult>> selector)
    {
        return Of(selector as LambdaExpression);
    }
}

public static class NameExtensions
{
    public static string NameOf<TSource, TResult>(this TSource obj, Expression<Func<TSource, TResult>> selector)
    {
        return Name.Of(selector);
    }
}
于 2012-04-17T14:49:42.143 回答
4

C# 6.0 的新手:nameof(属性)

于 2015-08-24T20:53:48.847 回答