none
lambda expression for passing non-instance property or constant/static field RRS feed

  • Question

  • i can find code sample passing property

    but i can't find way to pass property without an instance of that property object, or just a constant field

    eg:

    class MyC{
      public string P1 {get;set;}
      public const string P2 = "";
    }

    then a method just to : DoSomething( () => MyC.P1 );

    or () => MyC.P2

    noted that MyC is the class name, not an instance variable of the class

    what i intend to workout, within the method, i will get the full qualified name, then do some matching and lookup.

    and i don't need instance of the class


    • Edited by Kelmen Friday, November 24, 2017 6:54 AM
    Thursday, November 23, 2017 10:55 AM

Answers

  • and i don't need instance of the class

    I don't see how that is possible unless there is an instance of the class, an object,  in memory or it's a static class marked as static possibly.

    • Marked as answer by Kelmen Friday, November 24, 2017 7:06 AM
    Thursday, November 23, 2017 12:58 PM
  • looks like i have to resolve to use public static string

    class MyC {
      public static string P3;
    }

    the lambda method

    public string Test<T>(Expression<Func<T>> exp) { //var me = exp.Body as MemberExpression; return null; }

    usage:

    QQ.Test( () => MyC.P3 );


    now i want to capture the exp fully qualified name.

    the exp nodeType is MemberAccess, i think i only can cast it to MemberExpression

    in debug view, i can c a property "DebugView" = MyC.P3, but this is not accessible in actual coding

    how can i extract the FQN from the expression ?


    • Edited by Kelmen Friday, November 24, 2017 6:54 AM
    • Marked as answer by Kelmen Friday, November 24, 2017 7:05 AM
    Friday, November 24, 2017 6:53 AM

All replies

  • and i don't need instance of the class

    I don't see how that is possible unless there is an instance of the class, an object,  in memory or it's a static class marked as static possibly.

    • Marked as answer by Kelmen Friday, November 24, 2017 7:06 AM
    Thursday, November 23, 2017 12:58 PM
  • looks like i have to resolve to use public static string

    class MyC {
      public static string P3;
    }

    the lambda method

    public string Test<T>(Expression<Func<T>> exp) { //var me = exp.Body as MemberExpression; return null; }

    usage:

    QQ.Test( () => MyC.P3 );


    now i want to capture the exp fully qualified name.

    the exp nodeType is MemberAccess, i think i only can cast it to MemberExpression

    in debug view, i can c a property "DebugView" = MyC.P3, but this is not accessible in actual coding

    how can i extract the FQN from the expression ?


    • Edited by Kelmen Friday, November 24, 2017 6:54 AM
    • Marked as answer by Kelmen Friday, November 24, 2017 7:05 AM
    Friday, November 24, 2017 6:53 AM
  • https://stackoverflow.com/questions/36009209/fully-qualified-property-name

    var me = exp.Body as MemberExpression;

    var member = me.Member;
    return string.Format("{0}.{1}", member.DeclaringType.FullName, member.Name);

    Friday, November 24, 2017 7:05 AM