C# 如何通过lambda表达式传入属性?

C# 如何通过lambda表达式传入属性?,c#,lambda,C#,Lambda,在我的项目中有一些单元测试,我们希望能够设置一些具有私有setter的属性。目前,我正在通过反射和此扩展方法进行此操作: public static void SetPrivateProperty(this object sourceObject, string propertyName, object propertyValue) { sourceObject.GetType().GetProperty(propertyName).SetValue(sourceObject, prop

在我的项目中有一些单元测试,我们希望能够设置一些具有私有setter的属性。目前,我正在通过反射和此扩展方法进行此操作:

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(o => o.TestProperty, 1);
var propertyName = myTestObject.NameOf(o => o.TestProperty);
然后,我可以在单元测试中调用它,如下所示:

myTestObject.SetPrivateProperty("TestProperty", 1);
但是,我希望在编译时验证属性名称,因此我希望能够通过表达式传递属性,如下所示:

public class TestObject
{
    public int TestProperty{ get; private set; }
}
myTestObject.SetPrivateProperty(o => o.TestProperty, 1);
var propertyName = myTestObject.NameOf(o => o.TestProperty);

我该怎么做

如果getter是public的,那么下面的操作应该有效。它将为您提供一个如下所示的扩展方法:

public class TestObject
{
    public int TestProperty{ get; private set; }
}
myTestObject.SetPrivateProperty(o => o.TestProperty, 1);
var propertyName = myTestObject.NameOf(o => o.TestProperty);
它需要一个公共的getter。我希望,在某种程度上,像这样的反射功能被应用到语言中

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);
    }
}
公共静态类名
{
的公共静态字符串(LambdaExpression选择器)
{
如果(选择器==null)抛出新的ArgumentNullException(“选择器”);
var mexp=selector.Body作为MemberExpression;
if(mexp==null)
{
var uexp=(selector.Body作为UnaryExpression);
if(uexp==null)
抛出新的TargetException(
“无法使用表达式确定成员的名称,因为提供的表达式无法转换为“””+
typeof(UnaryExpression).Name+“.”
);
mexp=uexp。操作数作为MemberExpression;
}
如果(mexp==null)抛出新的TargetException(
“无法使用表达式确定成员的名称,因为提供的表达式无法转换为“””+
typeof(MemberExpression).Name+“.”
);
返回mexp.Member.Name;
}
的公共静态字符串(表达式选择器)
{
返回(选择器);
}
的公共静态字符串(表达式选择器)
{
返回(选择器为LambdaExpression);
}
}
公共静态类名称扩展
{
公共静态字符串名称(此TSource obj,表达式选择器)
{
返回名称(选择器);
}
}

C#6.0:(属性)的新增功能

lambda表达式的用途是什么?是否提供编译时验证?@mellamokb是。如果还有别的办法的话,我很高兴。看到了吗