Created
January 28, 2013 14:27
-
-
Save hagbarddenstore/4655920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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