C # Expression Tree Tips

I am working on a method that takes an expression tree as a parameter along with the type (or instance) of a class.

The basic idea is that this method will add certain things to the collection that will be used for validation.

public interface ITestInterface
{
    //Specify stuff here.
}

private static void DoSomething<T>(Expression<Func<T, object>> expression, params IMyInterface[] rule)
{
    // Stuff is done here.
}

The method is called as follows:

class TestClass
{
    public int MyProperty { get; set; }
}

class OtherTestClass  : ITestInterface
{
    // Blah Blah Blah.
}

static void Main(string[] args)
{
    DoSomething<TestClass>(t => t.MyProperty, 
        new OtherTestClass());
}

I do it this way because I would like the names of the properties that were passed to be strong.

A few things I struggle with.

  • DoSomething PropertyInfo ( ) T []. .Body [propertyname] "Convert. ([Propertyname])" , , . . ?
  • , ?
  • , , , / #, .

!

:

, expression.Body.ToString() DoSomething, , "Convert (t.MyProperty)", .

, , , .

!

+3
3

PropertyInfo Expression.Body .

+2

, , , , .

, - , " ".

, , - , URL- MVC, -, , ... URL-. URL- , . , , , . , .

, , , , , , . , , .

-, , - (, , ) tress JavaScript. , JavaScript, .

, , , , , , - , , . , - .

, .

System.Reflection.Emit , , , , . , .

, , - - , , . , !

+3

, . . :

DoSomething("MyProperty", new OtherClass());

- , . , , - , , , . , . "DoSomething".

, , - , , .

[DoSomething(typeof(OtherClass), typeof(OtherClass2))]
public int MyProperty
{
  get;
  set;
}

(, ?) OtherClass OtherClass2 .

+2

Source: https://habr.com/ru/post/1697755/


All Articles