Shorten the secondary name method of the reflection property?

I want a static helper method to remove magic lines. Of course, I could package the property method inside the TestContainer, which would eliminate the need to provide the TestContainer as an argument. It's done beautifully here .

But I would like to have a helper method in one static class, in case I later decide to optimize my code and remove this method. I was able to get this through, but its a bit ugly (having to provide a string type doesn't look good).

Any "god of expression" who knows the best way. Keep in mind that the static class must be generic and know nothing about the TestContainer (otherwise it would be as simple as the link)

internal class PropertyNameResolving
{
    internal class TestContainer
    {
        public string LastName { get; set; }
    }

    internal static class BindingHelper
    {
        public static string PropertyName<TObject, TValue>(Expression<Func<TObject, TValue>> propertySelector)
        {
            var memberExpression = propertySelector.Body as MemberExpression;
            if (memberExpression != null) return memberExpression.Member.Name;
            else
            throw new Exception("Something went wrong");
        }
    }

    internal static void Test()
    {
        var t = new TestContainer {LastName = "Hans"};
        Console.WriteLine(BindingHelper.PropertyName<TestContainer, string>(x => x.LastName));
        Console.ReadLine();

    }
}

      

Btw, the output is "LastName" and can be used to set bindings.

And one more question: is it safe to remove NULLs?

+2


source to share


2 answers


3 options for you:

  • make the class generic and use method inference for the value
  • use an example object for inference by method
  • completely forget about the object type and just use the example


All of this is shown below. Re null

security - I would be tempted to check the type of the expression (you can handle methods too, btw) - another code shown here .

using System;
using System.Linq.Expressions;
internal class TestContainer
{
    public string LastName { get; set; }
}
static class Program
{
    static void Main()
    {
        var t = new TestContainer {LastName = "Hans"};            
        string s1 = BindingHelper<TestContainer>
                      .PropertyName(x => x.LastName);            
        string s2 = BindingHelper.PropertyName(t, x => x.LastName);
        string s3 = BindingHelper.PropertyName(() => t.LastName);
    }
}
internal static class BindingHelper
{
    public static string PropertyName<TObject, TValue>(TObject template,
        Expression<Func<TObject, TValue>> propertySelector)
    {
        var memberExpression = propertySelector.Body as MemberExpression;
        if (memberExpression != null) return memberExpression.Member.Name;
        else
            throw new Exception("Something went wrong");
    }
    public static string PropertyName<TValue>(
         Expression<Func<TValue>> propertySelector)
    {
        var memberExpression = propertySelector.Body as MemberExpression;
        if (memberExpression != null) return memberExpression.Member.Name;
        else
            throw new Exception("Something went wrong");
    }
}
internal static class BindingHelper<TObject>
{
    public static string PropertyName<TValue>(
        Expression<Func<TObject, TValue>> propertySelector)
    {
        var memberExpression = propertySelector.Body as MemberExpression;
        if (memberExpression != null) return memberExpression.Member.Name;
        else
            throw new Exception("Something went wrong");
    }
}

      

+3


source


Since the compiler can imply type arguments, you don't need to provide the Test () method at all without changing anything.

Use BindingHelper.PropertyName (x => x.LastName) and it should work fine as it is.



You cannot remove the null check safely, since your expression can be anything, include a method call that will not be considered MemberExpression.

+1


source







All Articles