在 C# 中获取对 "thismember" 的引用
Getting a reference to "thismember" in C#
有没有办法在 c# 中获取对成员函数或 属性 的自动引用?
我的意思是这样的:
class Foo {
bool prop;
public bool MyProp
{
get { return prop; }
set {
prop = value;
OnPropertyChanged(thismember);
}
}
}
而 'thismember' 是自动引用调用 属性 ('MyProp') 的东西,类型为 System.Reflection.PropertyInfo 或 System.Reflection.MemberInfo?
目前没有。但是考虑到您的示例,C# 6 会帮助您,因为 nameof
运算符即将到来,请在此处查看 https://msdn.microsoft.com/en-us/magazine/dn802602.aspx and here https://roslyn.codeplex.com/discussions/570551
不,没有这样的东西。
已经讨论过(但暂时放弃,因为它会很复杂)是传递给运算符的成员的infoof
operator. Such an operator could return the MemberInfo
。
最接近您正在寻找的东西可能是来自 C# 6.0 的即将推出的 nameof
operator。虽然您仍然必须显式声明成员名称,但您至少会在编译时检查成员名称,因此如果您通过重命名来重构您的成员,编译器会提醒您您还需要在其中指定新名称您调用 nameof
运算符。
prism 的 BindableBase 中有您想要的东西:
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals((object) storage, (object) value))
return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
它允许你做:
public bool MyProp
{
get { return prop; }
set {
SetProperty(ref prop, value);
}
}
您的视图模型当然需要从 bindablebase 派生。
我猜您想以某种方式自动调用 OnPropertyChanged 方法,而不指定特定 属性 调用它的方法。艰难,但你可以尝试另一种方式...
public class SomeClasss
{
public string Name { get; set; }
bool _prop;
public bool MyProp
{
get { return _prop; }
set
{
_prop = value;
//OnPropertyChanged(thismember);
MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
Name = className + "." + methodName;
}
}
}
主要...
class Program
{
static void Main()
{
SomeClasss c = new SomeClasss();
Console.WriteLine("Before: {0}", c.Name);
c.MyProp = true;
Console.WriteLine("After: {0}", c.Name);
Console.ReadKey();
}
}
结果:
Before:
After: SomeClasss.set_MyProp
使用提供的代码,您可以将 属性 名称传递给 OnPropertyChanged 方法,这可能会有所帮助。但是我不确定你的意图是什么,所以它可能不完全符合你的需求。
有没有办法在 c# 中获取对成员函数或 属性 的自动引用?
我的意思是这样的:
class Foo {
bool prop;
public bool MyProp
{
get { return prop; }
set {
prop = value;
OnPropertyChanged(thismember);
}
}
}
而 'thismember' 是自动引用调用 属性 ('MyProp') 的东西,类型为 System.Reflection.PropertyInfo 或 System.Reflection.MemberInfo?
目前没有。但是考虑到您的示例,C# 6 会帮助您,因为 nameof
运算符即将到来,请在此处查看 https://msdn.microsoft.com/en-us/magazine/dn802602.aspx and here https://roslyn.codeplex.com/discussions/570551
不,没有这样的东西。
已经讨论过(但暂时放弃,因为它会很复杂)是传递给运算符的成员的infoof
operator. Such an operator could return the MemberInfo
。
最接近您正在寻找的东西可能是来自 C# 6.0 的即将推出的 nameof
operator。虽然您仍然必须显式声明成员名称,但您至少会在编译时检查成员名称,因此如果您通过重命名来重构您的成员,编译器会提醒您您还需要在其中指定新名称您调用 nameof
运算符。
prism 的 BindableBase 中有您想要的东西:
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (object.Equals((object) storage, (object) value))
return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
它允许你做:
public bool MyProp
{
get { return prop; }
set {
SetProperty(ref prop, value);
}
}
您的视图模型当然需要从 bindablebase 派生。
我猜您想以某种方式自动调用 OnPropertyChanged 方法,而不指定特定 属性 调用它的方法。艰难,但你可以尝试另一种方式...
public class SomeClasss
{
public string Name { get; set; }
bool _prop;
public bool MyProp
{
get { return _prop; }
set
{
_prop = value;
//OnPropertyChanged(thismember);
MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
Name = className + "." + methodName;
}
}
}
主要...
class Program
{
static void Main()
{
SomeClasss c = new SomeClasss();
Console.WriteLine("Before: {0}", c.Name);
c.MyProp = true;
Console.WriteLine("After: {0}", c.Name);
Console.ReadKey();
}
}
结果:
Before:
After: SomeClasss.set_MyProp
使用提供的代码,您可以将 属性 名称传递给 OnPropertyChanged 方法,这可能会有所帮助。但是我不确定你的意图是什么,所以它可能不完全符合你的需求。