简体   繁体   English

在C#中获得对“ thismember”的引用

[英]Getting a reference to “thismember” in C#

Is there a way to get an auto reference to a member function or property in c#? 有没有一种方法可以自动引用c#中的成员函数或属性?

I mean something like this: 我的意思是这样的:

class Foo {
    bool prop;
    public bool MyProp
    {
        get { return prop; }
        set {
            prop = value;
            OnPropertyChanged(thismember);
        }
    }
}

And 'thismember' is something that automatically references the calling property ('MyProp'), of type System.Reflection.PropertyInfo or System.Reflection.MemberInfo? “ thismember”是自动引用System.Reflection.PropertyInfo或System.Reflection.MemberInfo类型的调用属性(“ MyProp”)的东西。

Something that comes close to what you want is in prism's BindableBase: 在棱镜的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;
}

it allows you to do : 它可以让你做:

public bool MyProp
{
    get { return prop; }
    set {
        SetProperty(ref prop, value);
    }
}

your viewmodel needs to derive from bindablebase of course. 您的视图模型当然需要从bindablebase派生。

Currently, no. 目前没有。 But considering your example, C# 6 will help you, because nameof operator is coming, take a look on it here https://msdn.microsoft.com/en-us/magazine/dn802602.aspx and here https://roslyn.codeplex.com/discussions/570551 但是考虑到您的示例,C#6将为您提供帮助,因为nameof运算符来了,请在https://msdn.microsoft.com/zh-cn/magazine/dn802602.aspxhttps:// roslyn上进行查看。 codeplex.com/discussions/570551

No, there is no such thing. 不,没有这样的事情。

What has been discussed (but discarded for the time being, as it would be very complicated) is an infoof operator . 所讨论的(但暂时将其丢弃,因为这将非常复杂)是一个infoof运算符 Such an operator could return the MemberInfo for the member passed to the operator. 这样的操作员可以返回传递给该操作员的成员的MemberInfo

The closest thing to what you are looking for may be the upcoming nameof operator from C# 6.0. 与您要寻找的最接近的东西可能是C#6.0中即将出现的nameof运算符 While you still have to explicitly state the member name, you will at least have compile-time checking of the member name, so if you refactor your member by renaming it, the compiler will alert you that you also need to specify the new name where you invoke the nameof operator. 虽然您仍然必须明确声明成员名称,但是至少要在编译时检查成员名称,因此,如果通过重命名成员来重构成员,编译器会警告您还需要在以下位置指定新名称您调用nameof运算符。

I guess you want to somehow automatically call OnPropertyChanged method without specifying what particular property called it. 我猜您想以某种方式自动调用OnPropertyChanged方法,而无需指定调用它的特定属性。 Tough, but you can try another way... 艰难,但您可以尝试其他方式...

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;
        }
    }        
}

And main... 主要...

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();
    }
}

Result: 结果:

Before: 之前:

After: SomeClasss.set_MyProp 之后:SomeClasss.set_MyProp

Using presented code you can pass property name to OnPropertyChanged method which could be helpful. 使用显示的代码,您可以将属性名称传递给OnPropertyChanged方法,这可能会有所帮助。 However I'm not sure what's your intention, so it may not fully fit your needs. 但是,我不确定您的意图是什么,因此它可能无法完全满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM