3

I've got a class

public class News : Record
{
    public News()
    {
    }

    public LocaleValues Name { get; set; }
    public LocaleValues Body;
}

And in my LocaleValues class i have:

public class LocaleValues : List<LocalizedText>
{
    public string Method
    {
        get
        {
            var res = System.Reflection.MethodBase.GetCurrentMethod().Name;
            return res;
        }
    }
}

I need the Method property to return a string representation of Name property name when I make a call like this:

var propName = new News().Name.Method;

How can I achieve this? Thank you for your time!

CodeDemen
  • 1,841
  • 3
  • 17
  • 30
  • 2
    http://stackoverflow.com/questions/4657311/reflection-get-property-name – SpaceBison Mar 11 '13 at 07:29
  • Maybe what you want is _[Finding the hosting `PropertyInfo` from the `MethodInfo` of getter/setter](http://stackoverflow.com/questions/520138/)_? – Jeppe Stig Nielsen Mar 11 '13 at 09:47
  • I don't think it's possible. How could an instance of `LocaleValues` know that it was "found" through a property called `Name`? The code you have probably returns `get_Method` every time? Maybe you should make your `Name` setter modify the parameter it takes in (`value`) to record the name? – Jeppe Stig Nielsen Mar 11 '13 at 09:54

1 Answers1

10

If you truly mean the current property (question title):

public static string GetCallerName([CallerMemberName] string name = null) {
    return name;
}
...

public string Foo {
    get {
        ...
        var myName = GetCallerName(); // "Foo"
        ...
    }
    set { ... }
}

this pushes the work to the compiler rather than the runtime, and works regardless of inlining, obfuscation, etc. Note this needs a using directive of using System.Runtime.CompilerServices;, C# 5, and .NET 4.5 or similar.

If you mean the example:

var propName = new News().Name.Method;

then it isn't possible directly from that syntax; .Name.Method() would be invoking something (possibly an extension method) on the result of .Name - but that is just a.n.other object and knows nothing of where it came from (such as a Name property). Ideally to get Name, expression-trees are the simplest approach.

Expression<Func<object>> expr = () => new News().Bar;

var name = ((MemberExpression)expr.Body).Member.Name; // "Bar"

which could be encapsulated as:

public static string GetMemberName(LambdaExpression lambda)
{
    var member = lambda.Body as MemberExpression;
    if (member == null) throw new NotSupportedException(
          "The final part of the lambda is not a member-expression");
    return member.Member.Name;
}

i.e.

Expression<Func<object>> expr = () => new News().Bar;
var name = GetMemberName(expr); // "Bar"
arathorn
  • 2,098
  • 3
  • 20
  • 29
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Maybe I do not understand something.... or posted something wrong in my question, but how could I use it to know the exact name of my property inside `LocaleValues`? I need to make it so, that when I am calling a `News.Name.Method`, than `LocalValues` knows that it is named as `Name`, and when I call `News.Body.Method`, than it is callea as `Body`, The exact same thing with other classes which have property of type `LocalValues`... And I really sorry for my bad English :( – CodeDemen Mar 11 '13 at 09:15
  • This is especially useful for MVVM in WPF as instead of saying `NotifyPropertyChanged("Foo")` you can use this attribute and just say `NotifyPropertyChanged()`. – Sellorio Jul 03 '16 at 23:21