这是一个具有挑战性的问题。是否可以使用任何方法隐式确定作为参数传递给方法的属性的名称?
(这可能起初看起来像是另一个问题的副本,但是有一个微妙但重要的不同,因为我们总是使用属性,这是关键)。
以下是示例方案:
public class Foo
{
public string Bar { get; set; }
}
public void SomeStrangeMethod()
{
Foo foo = new Foo() { Bar = "Hello" };
string result = FindContext(foo.Bar); // should return "Bar"
}
public string FindContext(object obj)
{
// TODO? - figure out the property name corresponding to the passed parameter.
// In this example, we need to somehow figure out that the value of "obj"
// is the value of the property foo.Bar, and return "Bar"
}
假设在FindContext中,传递的参数将始终是对象的属性。问题是,我们不知道是什么对象。
显然,通过传递提供缺失上下文的第二个参数可以很容易地解决问题,即......
FindContext(foo, foo.Bar);
FindContext("Bar", foo.Bar);
....但那不是我想要的。我希望能够传递单个参数并确定值所代表的属性名称。
据我所知,当传递参数时,FindContext的方法上下文不包含足够的信息来确定这一点。但是,使用堆栈跟踪和IL的一些手法,也许我们仍然可以做到这一点。我认为这必须是可能的原因是:
要求传递给FindContext的参数必须始终是另一个对象的属性,并且我们知道可以使用反射获取所述属性名称。
使用StackTrace,我们可以获得调用上下文。
在调用上下文之外,我们应该能够以某种方式找到正在使用的符号。
从该符号中,我们应该能够检索属性名称和/或调用对象的类型,通过(1)我们应该能够转换为调用对象的属性。
有人知道怎么做吗?注意:这个问题很难,但我不相信这是不可能的。除非有人能证明为什么不可能,否则我不会接受任何“不可能”的答案。
答案 0 :(得分:3)
MVVM Light Toolkit使用C#的表达式树支持传递“属性”以实现INotifyPropertyChanging
和INotifyPropertyChanged
。有关详细信息,请参阅ObservableObject.cs
void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var handler = PropertyChanged;
if (handler != null)
{
var propertyName = GetPropertyName(propertyExpression);
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
然后调用代码可以使用:
RaisePropertyChanged(() => this.Property);
而不是像这样依赖硬编码的字符串:
RaisePropertyChanged("Property");
答案 1 :(得分:2)
如果传递lambda表达式,则可以
public class Foo
{
public string Bar { get; set; }
}
public void SomeStrangeMethod()
{
Foo foo = new Foo() { Bar = "Hello" };
string result = GetName(()=>foo.Bar); // should return "Bar"
Debug.WriteLine(result); // "Bar"
}
public static string GetName<T>(Expression<Func<T>> expression)
{
return ExtractPropertyName(expression);
}
/// <summary>
/// Extracts the name of a property from a suitable LambdaExpression.
/// </summary>
/// <param name="propertyExpression">The property expression.</param>
/// <returns></returns>
public static string ExtractPropertyName(LambdaExpression propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException(@"Not a member expression", "propertyExpression");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException(@"Not a property", "propertyExpression");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException(@"Can't be static", "propertyExpression");
}
return memberExpression.Member.Name;
}
答案 2 :(得分:1)
不 - 这并非总是可行。
3. Out of the calling context, we should be able to somehow locate the symbol being used.
这是失败的部分。在运行时,您无法获得用于方法参数的符号(至少不是直接)。 Reflection库中没有提供此类分析或元数据的工具。
话虽如此,通过大量的IL分析,这可能是可能的。如果您使用Mono.Cecil之类的工具来反编译程序集,请找到“调用上下文”,然后检查IL以查询相关方法。
答案 3 :(得分:1)
为了能够从Expression
找到属性名称而不在Expression
中明确指定对象类型,您需要一个扩展方法:
public static class ObjectExt
{
public static string FindContext<T,TProp>(this T obj, Expression<Func<T,TProp>> expression) {
return ( expression.Body as MemberExpression ).Member.Name;
}
}
然后,如果我们将它放在您的代码示例中
public class Foo
{
public string Bar { get; set; }
}
public void SomeStrangeMethod()
{
Foo foo = new Foo() { Bar = "Hello" };
string result = foo.FindContext(s => s.Bar); // should return "Bar"
}