以字符串形式获取属性名称 [英] Get name of property as a string
问题描述
(请参阅下面我使用我接受的答案创建的解决方案)
(See below solution I created using the answer I accepted)
我正在尝试提高一些涉及反射的代码的可维护性.该应用程序有一个 .NET Remoting 接口,它公开(除其他外)一个名为 Execute
的方法,用于访问未包含在其发布的远程接口中的应用程序部分.
I'm trying to improve the maintainability of some code involving reflection. The app has a .NET Remoting interface exposing (among other things) a method called Execute
for accessing parts of the app not included in its published remote interface.
以下是应用程序如何指定可通过执行访问的属性(本例中为静态属性):
Here is how the app designates properties (a static one in this example) which are meant to be accessible via Execute:
RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty");
所以远程用户可以调用:
So a remote user could call:
string response = remoteObject.Execute("SomeSecret");
并且应用程序将使用反射来查找 SomeClass.SomeProperty 并将其值作为字符串返回.
and the app would use reflection to find SomeClass.SomeProperty and return its value as a string.
不幸的是,如果有人重命名 SomeProperty 并忘记更改 ExposeProperty() 的第三个参数,则会破坏此机制.
Unfortunately, if someone renames SomeProperty and forgets to change the 3rd parm of ExposeProperty(), it breaks this mechanism.
我需要相当于:
SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
用作 ExposeProperty
中的第三个参数,以便重构工具负责重命名.
to use as the 3rd parm in ExposeProperty
so refactoring tools would take care of renames.
有没有办法做到这一点?
Is there a way to do this?
好的,这是我最终创建的内容(基于我选择的答案和他引用的问题):
Okay, here's what I ended up creating (based upon the answer I selected and the question he referenced):
// <summary>
// Get the name of a static or instance property from a property access lambda.
// </summary>
// <typeparam name="T">Type of the property</typeparam>
// <param name="propertyLambda">lambda expression of the form: '() => Class.Property' or '() => object.Property'</param>
// <returns>The name of the property</returns>
public string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
var me = propertyLambda.Body as MemberExpression;
if (me == null)
{
throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
}
return me.Member.Name;
}
用法:
// Static Property
string name = GetPropertyName(() => SomeClass.SomeProperty);
// Instance Property
string name = GetPropertyName(() => someObject.SomeProperty);
现在有了这个很酷的功能,是时候简化 ExposeProperty 方法了.抛光门把手是一项危险的工作...
Now with this cool capability, it's time to simplify the ExposeProperty method. Polishing doorknobs is dangerous work...
推荐答案
从这里使用 GetMemberInfo:从 lambda 表达式中检索属性名称 你可以这样做:
Using GetMemberInfo from here: Retrieving Property name from lambda expression you can do something like this:
RemoteMgr.ExposeProperty(() => SomeClass.SomeProperty)
public class SomeClass
{
public static string SomeProperty
{
get { return "Foo"; }
}
}
public class RemoteMgr
{
public static void ExposeProperty<T>(Expression<Func<T>> property)
{
var expression = GetMemberInfo(property);
string path = string.Concat(expression.Member.DeclaringType.FullName,
".", expression.Member.Name);
// Do ExposeProperty work here...
}
}
public class Program
{
public static void Main()
{
RemoteMgr.ExposeProperty("SomeSecret", () => SomeClass.SomeProperty);
}
}
这篇关于以字符串形式获取属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!