您可以使用Lambda表达式来解决编译时的性能基准。 (从Retrieving Property name from lambda expression修改后的代码)
public PropertyInfo GetPropertyInfo<TProperty>(
Expression<Func<TProperty>> propertyLambda)
{
MemberExpression member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda.ToString()));
PropertyInfo propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda.ToString()));
return propInfo;
}
你并不需要使用它作为一个扩展方法(但如果你要适应它,你可以,但有一个源对象实例是没有必要的,除了写行)
public class Test
{
public string Prop { get; set; }
}
Test t = new Test();
PropertyInfo propInfo = GetPropertyInfo(() => t.Prop);
Console.WriteLine(propInfo.Name + " -> " + propInfo.PropertyType); //Prop -> System.String
编辑:如果你想有一些不错的语法和必须避免现有参考类型的对象,你可以这样做:
public static class TypedReflection<TSource>
{
public static PropertyInfo GetPropertyInfo<TProperty>(
Expression<Func<TSource, TProperty>> propertyLambda)
{
MemberExpression member = propertyLambda.Body as MemberExpression;
if (member == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda.ToString()));
PropertyInfo propInfo = member.Member as PropertyInfo;
if (propInfo == null)
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda.ToString()));
return propInfo;
}
}
,并调用它像:
PropertyInfo propInfo = TypedReflection<Test>.GetPropertyInfo(o => o.Prop);
在这一点上,它很容易增加额外的类型化思考方法(get方法,字段等)
编辑:它仍然是八九不离十两个泛型类型的依赖,但它通过类型推断隐藏起来。我更喜欢第二个例子;至少需要指定声明类类型(因为您想要类型安全),但不需要对象实例。它也有好处(我怀疑你是在这之后的),如果你重命名属性,它会传播到这个代码获得PropertyInfo
。
http://blog.slaks.net/2010/12/partial型推理功能于net.html – SLaks