C# 使用 lambda 表达式获取属性名称和类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/273941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get property name and type using lambda expression
提问by pythonandchips
I am trying to write a function that will pull the name of a property and the type using syntax like below:
我正在尝试编写一个函数,该函数将使用如下语法提取属性名称和类型:
private class SomeClass
{
Public string Col1;
}
PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>();
propertyMapper.MapProperty(x => x.Col1)
Is there any way to pass the property through to the function without any major changes to this syntax?
有没有办法将属性传递给函数而不对该语法进行任何重大更改?
I would like to get the property name and the property type.
我想获取属性名称和属性类型。
So in the example below i would want to retrieve
所以在下面的例子中我想检索
Name = "Col1"
and Type = "System.String"
Name = "Col1"
和 Type = "System.String"
Can anyone help?
任何人都可以帮忙吗?
采纳答案by Jacob Carpenter
Here's enough of an example of using Expressionsto get the name of a property or field to get you started:
以下是使用表达式获取属性或字段名称的示例,足以让您入门:
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression)
{
var member = expression.Body as MemberExpression;
if (member != null)
return member.Member;
throw new ArgumentException("Expression is not a member access", "expression");
}
Calling code would look like this:
调用代码如下所示:
public class Program
{
public string Name
{
get { return "My Program"; }
}
static void Main()
{
MemberInfo member = ReflectionUtility.GetMemberInfo((Program p) => p.Name);
Console.WriteLine(member.Name);
}
}
A word of caution, though: the simple statment of (Program p) => p.Name
actually involves quite a bit of work (and can take measurable amounts of time). Consider caching the result rather than calling the method frequently.
但是,请注意:简单的陈述(Program p) => p.Name
实际上涉及相当多的工作(并且可能需要可测量的时间)。考虑缓存结果而不是频繁调用方法。
回答by chapacool
I found this very useful.
我发现这非常有用。
public class PropertyMapper<T>
{
public virtual PropertyInfo PropertyInfo<U>(Expression<Func<T, U>> expression)
{
var member = expression.Body as MemberExpression;
if (member != null && member.Member is PropertyInfo)
return member.Member as PropertyInfo;
throw new ArgumentException("Expression is not a Property", "expression");
}
public virtual string PropertyName<U>(Expression<Func<T, U>> expression)
{
return PropertyInfo<U>(expression).Name;
}
public virtual Type PropertyType<U>(Expression<Func<T, U>> expression)
{
return PropertyInfo<U>(expression).PropertyType;
}
}
I made this little class to follow the original request. If you need the name of the property you can use it like this:
我做了这个小类是为了遵循最初的要求。如果您需要属性的名称,您可以像这样使用它:
PropertyMapper<SomeClass> propertyMapper = new PropertyMapper<SomeClass>();
string name = propertyMapper.PropertyName(x => x.Col1);
回答by TheRock
I just thought I would put this here to build on the previous approach.
我只是想我会把它放在这里以建立在以前的方法之上。
public static class Helpers
{
public static string PropertyName<T>(Expression<Func<T>> expression)
{
var member = expression.Body as MemberExpression;
if (member != null && member.Member is PropertyInfo)
return member.Member.Name;
throw new ArgumentException("Expression is not a Property", "expression");
}
}
You can then call it in the following fashion:
然后,您可以按以下方式调用它:
Helpers.PropertyName(() => TestModel.TestProperty);
I should also point out that with VS 2015 and C# 6.0 you can simply use nameof.
我还应该指出,在 VS 2015 和 C# 6.0 中,您可以简单地使用 nameof。
回答by Anand Patel
This can be easily done in C# 6. To get the name of property use nameof operator.
这可以在 C# 6 中轻松完成。要获取属性的名称,请使用 nameof 运算符。
nameof(User.UserId)
and to get type of property use typeof operator.
并使用 typeof 运算符获取属性类型。
typeof(User.UserId)