(参见下面我使用我接受的答案创建的解决方案)
我正在努力提高一些涉及反射的代码的可维护性。该应用程序有一个.NET Remoting接口,公开(除此之外)一个名为Execute的方法,用于访问未包含在其已发布的远程接口中的应用程序部分。
以下是应用程序如何指定可通过Execute访问的属性(本例中为静态属性):
RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty");
远程用户可以致电:
string response = remoteObject.Execute("SomeSecret");
并且app会使用反射来查找SomeClass.SomeProperty并将其值作为字符串返回。
不幸的是,如果有人重命名SomeProperty并忘记更改ExposeProperty()的第3个参数,则会破坏此机制。
我需要相当于:
SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
用作ExposeProperty中的第3个parm,因此重构工具将负责重命名。
有办法做到这一点吗?提前谢谢。
好的,这是我最终创作的内容(根据我选择的答案和他引用的问题):
// <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方法了。抛光门把手是危险的工作......
谢谢大家。
答案 0 :(得分:367)
答案 1 :(得分:59)
从这里使用GetMemberInfo:Retrieving Property name from lambda expression你可以这样做:
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);
}
}
答案 2 :(得分:17)
有一个众所周知的hack从lambda表达式中提取它(这是来自PropertyObserver类,Josh Smith,在他的MVVM基础中):
private static string GetPropertyName<TPropertySource>
(Expression<Func<TPropertySource, object>> expression)
{
var lambda = expression as LambdaExpression;
MemberExpression memberExpression;
if (lambda.Body is UnaryExpression)
{
var unaryExpression = lambda.Body as UnaryExpression;
memberExpression = unaryExpression.Operand as MemberExpression;
}
else
{
memberExpression = lambda.Body as MemberExpression;
}
Debug.Assert(memberExpression != null,
"Please provide a lambda expression like 'n => n.PropertyName'");
if (memberExpression != null)
{
var propertyInfo = memberExpression.Member as PropertyInfo;
return propertyInfo.Name;
}
return null;
}
对不起,这个错过了一些上下文。这是较大类的一部分,其中TPropertySource
是包含该属性的类。您可以在TPropertySource中使函数通用,以从类中提取它。我建议您查看MVVM Foundation的完整代码。
答案 3 :(得分:15)
好的,这是我最终创作的内容(根据我选择的答案和他引用的问题):
// <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);
答案 4 :(得分:8)
如果我理解正确,PropertyInfo课程可以帮助您实现这一目标。
PropertyInfo[] propInfos = typeof(ReflectedType).GetProperties();
propInfos.ToList().ForEach(p =>
Console.WriteLine(string.Format("Property name: {0}", p.Name));
这是你需要的吗?
答案 5 :(得分:6)
您可以使用Reflection获取属性的实际名称。
http://www.csharp-examples.net/reflection-property-names/
如果您需要一种方法为属性分配“字符串名称”,为什么不编写一个可以反映的属性来获取字符串名称?
[StringName("MyStringName")]
private string MyProperty
{
get { ... }
}
答案 6 :(得分:5)
我修改了你的解决方案以链接多个属性:
public static string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
MemberExpression me = propertyLambda.Body as MemberExpression;
if (me == null)
{
throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
}
string result = string.Empty;
do
{
result = me.Member.Name + "." + result;
me = me.Expression as MemberExpression;
} while (me != null);
result = result.Remove(result.Length - 1); // remove the trailing "."
return result;
}
用法:
string name = GetPropertyName(() => someObject.SomeProperty.SomeOtherProperty);
// returns "SomeProperty.SomeOtherProperty"
答案 7 :(得分:4)
根据问题和本文中已有的答案:https://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/我正在提出解决此问题的方法:
public static class PropertyNameHelper
{
/// <summary>
/// A static method to get the Propertyname String of a Property
/// It eliminates the need for "Magic Strings" and assures type safety when renaming properties.
/// See: http://stackoverflow.com/questions/2820660/get-name-of-property-as-a-string
/// </summary>
/// <example>
/// // Static Property
/// string name = PropertyNameHelper.GetPropertyName(() => SomeClass.SomeProperty);
/// // Instance Property
/// string name = PropertyNameHelper.GetPropertyName(() => someObject.SomeProperty);
/// </example>
/// <typeparam name="T"></typeparam>
/// <param name="propertyLambda"></param>
/// <returns></returns>
public static 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;
}
/// <summary>
/// Another way to get Instance Property names as strings.
/// With this method you don't need to create a instance first.
/// See the example.
/// See: https://handcraftsman.wordpress.com/2008/11/11/how-to-get-c-property-names-without-magic-strings/
/// </summary>
/// <example>
/// string name = PropertyNameHelper((Firma f) => f.Firmenumsatz_Waehrung);
/// </example>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TReturn"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression)
{
MemberExpression body = (MemberExpression)expression.Body;
return body.Member.Name;
}
}
还有一个测试,它还显示了实例和静态属性的用法:
[TestClass]
public class PropertyNameHelperTest
{
private class TestClass
{
public static string StaticString { get; set; }
public string InstanceString { get; set; }
}
[TestMethod]
public void TestGetPropertyName()
{
Assert.AreEqual("StaticString", PropertyNameHelper.GetPropertyName(() => TestClass.StaticString));
Assert.AreEqual("InstanceString", PropertyNameHelper.GetPropertyName((TestClass t) => t.InstanceString));
}
}
答案 8 :(得分:3)
老问题,但这个问题的另一个答案是在使用CallerMemberNameAttribute的帮助器类中创建一个静态函数。
public static string GetPropertyName([CallerMemberName] String propertyName = null) {
return propertyName;
}
然后使用它:
public string MyProperty {
get { Console.WriteLine("{0} was called", GetPropertyName()); return _myProperty; }
}
答案 9 :(得分:0)
您可以使用StackTrace类获取当前函数的名称,(或者如果您将代码放在函数中,然后降低级别并获取调用函数)。
请参阅http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace(VS.71).aspx
答案 10 :(得分:0)
我一直在使用这个答案效果很好:Get the property, as a string, from an Expression<Func<TModel,TProperty>>
我意识到我已经回答了这个问题了。我的另一个答案唯一的优点是它适用于静态属性。我发现此答案中的语法更有用,因为您不必创建要反映的类型的变量。
答案 11 :(得分:0)
我在使用已经针对我的特定用例建议的解决方案时遇到了一些困难,但最终还是弄明白了。我不认为我的具体案例值得一个新问题,所以我在这里发布我的解决方案以供参考。 (这与问题密切相关,并为其他任何有类似情况的人提供解决方案。)
我最终得到的代码如下:
public class HideableControl<T>: Control where T: class
{
private string _propertyName;
private PropertyInfo _propertyInfo;
public string PropertyName
{
get { return _propertyName; }
set
{
_propertyName = value;
_propertyInfo = typeof(T).GetProperty(value);
}
}
protected override bool GetIsVisible(IRenderContext context)
{
if (_propertyInfo == null)
return false;
var model = context.Get<T>();
if (model == null)
return false;
return (bool)_propertyInfo.GetValue(model, null);
}
protected void SetIsVisibleProperty(Expression<Func<T, bool>> propertyLambda)
{
var expression = propertyLambda.Body as MemberExpression;
if (expression == null)
throw new ArgumentException("You must pass a lambda of the form: 'vm => vm.Property'");
PropertyName = expression.Member.Name;
}
}
public interface ICompanyViewModel
{
string CompanyName { get; }
bool IsVisible { get; }
}
public class CompanyControl: HideableControl<ICompanyViewModel>
{
public CompanyControl()
{
SetIsVisibleProperty(vm => vm.IsVisible);
}
}
对我来说,重要的部分是在CompanyControl
类中,编译器只允许我选择ICompanyViewModel
的布尔属性,这使得其他开发人员更容易正确使用它。
我的解决方案和接受的答案之间的主要区别在于我的类是通用的,我只想匹配布尔值泛型类型的属性。
答案 12 :(得分:0)
它是我如何实现它的,背后的原因是如果你想从它的成员获得名字的类不是静态的,那么你需要创建一个instanse,然后得到会员的名字。如此通用这里来帮助
public static string GetName<TClass>(Expression<Func<TClass, object>> exp)
{
MemberExpression body = exp.Body as MemberExpression;
if (body == null)
{
UnaryExpression ubody = (UnaryExpression)exp.Body;
body = ubody.Operand as MemberExpression;
}
return body.Member.Name;
}
用法就像这样
var label = ClassExtension.GetName<SomeClass>(x => x.Label); //x is refering to 'SomeClass'