[英]Is it possible to write a wrapper for the C# nameof operator?
我阅读了有关nameof
运算符的相关问答,但它对我没有帮助,所以我在这里问了。
我想为 C# nameof
运算符编写一个包装器,这样它不仅会返回 class 属性的名称,而且还会将它与 class 名称连接起来。
让我们假设一个具有单一属性的 class:
class Foo
{
public string SomeProperty {get; set;}
}
现在如果用(C# 6 or higher)编译Console.WriteLine(nameof(Foo.SomeProperty))
,结果将是:
一些财产
所以有可能有这样的东西:
public string PrintFullName(???? object)
{
//????
}
我放了????
对于输入Type
,因为我不知道正确的输入Type
是什么。
我希望 PrintFullName 的结果是:
Foo.SomeProperty
我不一定要寻找运行时解决方案。 任何编译时解决方法也将有所帮助。
当然这是可能的,使用表达式树。
本网站上的完整说明(所有归功于 Dave Glick)。
归结为:
public void UseNames(string className, string memberName)
{
// Your code to use the class & membername go here
}
public void UseNames<T>(Expression<Func<T, object>> expression)
{
MemberExpression member = expression.Body as MemberExpression;
if (member == null)
{
// The property access might be getting converted to object to match the func
// If so, get the operand and see if that's a member expression
member = (expression.Body as UnaryExpression)?.Operand as MemberExpression;
}
if (member == null)
{
throw new ArgumentException("Action must be a member expression.");
}
// Pass the names on to the string-based UseNames method
UseNames(typeof(T).Name, member.Member.Name);
}
public void UseNames<T>(Expression<Func<T, string>> expression)
{
ConstantExpression constant = expression.Body as ConstantExpression;
if (constant == null)
{
throw new ArgumentException("Expression must be a constant expression.");
}
UseNames(typeof(T).Name, constant.Value.ToString());
}
像这样使用它:
UseNames<Foo>(x => nameof(x.Bar));
UseNames<Foo>(x => nameof(x.Baz));
一个简单(也许更快)的解决方案,没有表达式树(使用反射):
public string PrintFullName<T>(String memberName)
{
return $"{typeof(T).Name}.{memberName}";
}
用法:
PrintFullName<SomeType>(nameof(SomeType.SomeProperty));
// Compiled to: PrintFullName<SomeType>("SomeProperty");
nameof()
是一个编译时构造,因此无需构建一个稍微高级的解决方案,在运行时执行超出所需的评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.