Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created January 28, 2013 14:27
Show Gist options
  • Save hagbarddenstore/4655920 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/4655920 to your computer and use it in GitHub Desktop.
using System;
namespace Test
{
using System.Linq.Expressions;
class Program
{
static void Main(string[] args)
{
var a = new A();
var b = new B();
Console.WriteLine(a.GetPropertyName(x => x.B));
Console.WriteLine(b.GetPropertyName(x => x.C));
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
}
static class Extensions
{
public static string GetPropertyName<T1, T2>(this T1 source, Expression<Func<T1, T2>> expression)
where T1 : A
{
var propertyExpression = expression.Body as MemberExpression;
var name = propertyExpression.Member.Name;
return name;
}
}
class A
{
public string B { get; set; }
}
class B : A
{
public string C { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment