我的项目中有一些单元测试,我们希望能够设置一些具有私有设置器的属性。目前我正在通过反射和这种扩展方法来做到这一点:
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);
但是,我想在编译时验证属性名称,因此我希望能够在via表达式中传递属性,如下所示:
myTestObject.SetPrivateProperty(o => o.TestProperty, 1);
我该怎么做?
答案 0 :(得分:9)
如果获取者是公开的,那么以下内容应该有效。它将为您提供如下所示的扩展方法:
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);
}
}
答案 1 :(得分:4)
C#6.0新手:nameof(属性)