Quantcast
Channel: How to get a PropertyInfo without property name?
Viewing all articles
Browse latest Browse all 3

How to get a PropertyInfo without property name?

$
0
0
Yes, infoof would be a great feature.
But, in C# 3.0 expression trees can do the trick.

Code Block

using System;
using System.Reflection;
using System.Linq.Expressions;

class Person
{
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        var person = new Person();
        var age = InfoOf(() => person.Age);
        
        Console.WriteLine(age.Name);
    }

    static PropertyInfo InfoOf<T>(Expression<Func<T>> ex)
    {
        return (PropertyInfo)((MemberExpression)ex.Body).Member;
    }
}



Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>