强类型属性声明 - 这段代码安全吗?
我想知道以下代码是否“安全”。我所说的“安全”是指我不依赖于某些特定的编译器版本或未记录的功能。 我想获取带有属性/字段名称的字符串,但我想使用强类型来声明它(我希望编译器检查特定字段/属性是否存在)。 我的方法如下所示:
string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
if (expression.Body is UnaryExpression)
{
var operand = ((UnaryExpression)expression.Body).Operand.ToString();
return operand.Substring(operand.IndexOf(".") + 1);
}
else if (expression.Body is MemberExpression)
{
return ((MemberExpression)expression.Body).Member.Name;
}
else
{
throw new NotImplementedException();
}
}
这就是我想要使用它的方式:
class Foo
{
public string A { get; set; }
public Bar B { get; set; }
}
class Bar
{
public int C { get; set; }
public Baz D { get; set; }
}
class Baz
{
public int E { get; set; }
}
GetPropertyName<Foo>(x => x.A)
GetPropertyName<Foo>(x => x.B)
GetPropertyName<Foo>(x => x.B.C)
GetPropertyName<Foo>(foo => foo.B.D.E)
提前感谢您的帮助。
I'm wondering if following code is "safe". By "safe" I mean that I don't depend on some specific compiler version or undocumented feature.
I want to get string with property / field name, but I want to declare it using strong typing (I want the compiler to check if specific field / property exists).
My method looks like this:
string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
if (expression.Body is UnaryExpression)
{
var operand = ((UnaryExpression)expression.Body).Operand.ToString();
return operand.Substring(operand.IndexOf(".") + 1);
}
else if (expression.Body is MemberExpression)
{
return ((MemberExpression)expression.Body).Member.Name;
}
else
{
throw new NotImplementedException();
}
}
And here is how I want to use it:
class Foo
{
public string A { get; set; }
public Bar B { get; set; }
}
class Bar
{
public int C { get; set; }
public Baz D { get; set; }
}
class Baz
{
public int E { get; set; }
}
GetPropertyName<Foo>(x => x.A)
GetPropertyName<Foo>(x => x.B)
GetPropertyName<Foo>(x => x.B.C)
GetPropertyName<Foo>(foo => foo.B.D.E)
Thanks in advance for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定 < 的输出code>ToString 方法以任何方式得到保证。文档只是说它“返回
表达式
的文本表示”。(我怀疑输出不太可能在不同的平台/版本之间改变,但是当你的目标是使用强类型、编译时检查等时,我有点不愿意依赖它。)
这是我的方法,它做了一些事情不使用 ToString 类似:
I'm not sure that the output of the
ToString
method is guaranteed in any way. The documentation just says that it "returns a textual representation of theExpression
".(I suspect that the output is unlikely to change across different platforms/versions, but I'd be a bit reluctant to rely on it when your aim is to use strong typing, compile-time checks etc.)
Here's my method that does something similar without using
ToString
:我认为你的代码没问题。我没有看到任何问题。要深入了解这一点,我建议您阅读 本文 和 这个,也是。
I think you code is okay. I don't see any problems. To get a little deep into this, I recommend you read this article and this one, too.