获取的属性名作为字符串 [英] Get name of property as a string
问题描述
(请参见下面的解决方案我创建使用我接受的答案)
(See below solution I created using the answer I accepted)
我试图改善涉及一些反思code的可维护性。该应用程序有一个.NET远程接口暴露(除其他事项外)被称为执行访问不包括在其公布的远程接口的应用程序的部分方法。
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()的第三PARM,它打破了这种机制。
Unfortunately, if someone renames SomeProperty and forgets to change the 3rd parm of ExposeProperty(), it breaks this mechanism.
我需要等价的:
SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
在ExposeProperty第三PARM这样的重构工具将采取重命名的注意使用。
to use as the 3rd parm in ExposeProperty so refactoring tools would take care of renames.
有没有办法做到这一点?先谢谢了。
Is there a way to do this? Thanks in advance.
好吧,这里是我结束了创建(根据我选择了答案,他所引用的问题):
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:<一href=\"http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-ex$p$pssion\">http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-ex$p$pssion你可以做这样的事情:
Using GetMemberInfo from here: http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression you can do something like this:
RemoteMgr.ExposeProperty(()=&GT; 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屋!