Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#反射-不使用字符串获取PropertyInfo_C#_Reflection - Fatal编程技术网

C#反射-不使用字符串获取PropertyInfo

C#反射-不使用字符串获取PropertyInfo,c#,reflection,C#,Reflection,我在Myclass中有一个属性: public class MyClass{ public string FirstName {get;set;} } 如何在没有字符串的情况下获取PropertyInfo(使用GetProperty(“FirstName”) 今天我用这个: PropertyInfo propertyTitleNews = typeof(MyClass).GetProperty("FirstName"); 有没有这样的使用方法: PropertyInfo pr

我在
Myclass
中有一个属性:

public class MyClass{    
    public string FirstName {get;set;}
}
如何在没有字符串的情况下获取
PropertyInfo
(使用
GetProperty(“FirstName”)

今天我用这个:

PropertyInfo propertyTitleNews = typeof(MyClass).GetProperty("FirstName");
有没有这样的使用方法:

PropertyInfo propertyTitleNews = typeof(MyClass).GetProperty(MyClass.FirstName);
MyClass c;
PropertyInfo propertyTitleNews = c.GetPropertyName(x => x.FirstName);
?参见。其思想是使用表达式树

public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression)
{
    MemberExpression body = (MemberExpression)expression.Body;
    return body.Member.Name;
}
然后使用更干净:

var name = TypeMember<MyClass>.GetPropertyName(c => c.FirstName);
var name=TypeMember.GetPropertyName(c=>c.FirstName);

除此之外,另一种可能性是提供一种扩展方法:

public static class PropertyInfoExtensions
{
    public static string GetPropertyName<TType, TReturnType>(
        this TType @this,
        Expression<Func<TType, TReturnType>> propertyId)
    {
        return ((MemberExpression)propertyId.Body).Member.Name;
    }
}
缺点是您需要一个实例,但优点是不需要提供泛型参数。

您可以这样做

var property = ExpressionExtensions.GetProperty<MyClass>(o => o.FirstName);
var-property=ExpressionExtensions.GetProperty(o=>o.FirstName);
使用此帮助程序:

public static PropertyInfo GetProperty<T>(Expression<Func<T, Object>> expression)
{
     MemberExpression body = (MemberExpression)expression.Body;
     return typeof(T).GetProperty(body.Member.Name);
}
publicstaticpropertyinfo GetProperty(Expression)
{
MemberExpression body=(MemberExpression)expression.body;
返回typeof(T).GetProperty(body.Member.Name);
}

如果您能够像这样访问类的属性,您就不需要从反射开始了。如果这是.NET 4或更高版本,您是否尝试过使用Dynamic关键字?是的,有可能。我同意洛克的观点。这没有多大意义。当创建属性的唯一方法是从字符串创建属性时,通常使用反射。@Szymon不,一般来说,它是有意义的,因为
PropertyInfo
包含的信息不仅仅是属性的类型和值。注意所有涉及成员表达式的答案。有一个令人绊倒的角落案例:!这不是一件很安全的事情,如果没有lambda的开销,这是可能的吗?@rolls你说的开销是什么意思?lambda从未执行过,因此它只是编译器传递的数据。lambda也不会捕获任何局部变量,因此也没有额外的分配。这一点很好。我没有在那个细节层次上考虑它,它与传入对象/结构没有太大区别。
var property = ExpressionExtensions.GetProperty<MyClass>(o => o.FirstName);
public static PropertyInfo GetProperty<T>(Expression<Func<T, Object>> expression)
{
     MemberExpression body = (MemberExpression)expression.Body;
     return typeof(T).GetProperty(body.Member.Name);
}