1

Is there a way to get an auto reference to a member function or property in 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?

4 Answers 4

2

Something that comes close to what you want is in prism's 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.

1

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. Such an operator could return the MemberInfo for the member passed to the operator.

The closest thing to what you are looking for may be the upcoming nameof operator from C# 6.0. 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.

1

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

1

I guess you want to somehow automatically call OnPropertyChanged method without specifying what particular property called it. 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

Using presented code you can pass property name to OnPropertyChanged method which could be helpful. However I'm not sure what's your intention, so it may not fully fit your needs.

Not the answer you're looking for? Browse other questions tagged or ask your own question.