简体   繁体   中英

Get parameters from lambda Expression

I am not very familiar with lambda expressions. So I have the following expression:

EnabledPropertySelector = l => l.FranchiseInfo.ExternalSystemType ==  ExternalSystemTypes.Master

And two properties:

public string VisibilityPropertyName { get; set; }
public object VisibilityPropertyValue { get; set; }

I want to extract some data from the expression so in the end I can get the values of the two properties:

VisibilityPropertyName == 'FranchiseInfo.ExternalSystemType';
VisibilityPropertyValue == ExternalSystemTypes.Master;

VisibilityPropertyName is always a string. This is the name of the property. VisibilityPropertyValue can be of any type.

EDIT:

I have a lot of properties. Some of them are dependent on other properties. For every single property I have to manually write the name and the value of the parent property:

{ VisibilityPropertyName = 'FranchiseInfo.ExternalSystemType', VisibilityPropertyValue = ExternalSystemTypes.Master, EnabledPropertySelector = l => l.FranchiseInfo.ExternalSystemType ==  ExternalSystemTypes.Master}

Instead of writing all this I want to write only the expression and populate the properties from it.

This is the declaration of the expresion:

Expression<Func<TEntity, bool?>> EnabledPropertySelector

First off all, you need an Expression. What's the type of EnabledPropertySelector? It'll need to be something like Expression<Func<T, bool>> where T is whatever the type of "l" in your example is.

If you already have an Expression then you can use the Expression API to extract whatever you need:-

var body = EnabledPropertySelector.Body as BinaryExpression;

var left = body.Left as PropertyExpression;
var outerMemberName = left.Member.Name;
var innerMemberName = (left.Expression as PropertyExpression).Member.Name

VisibilityPropertyName = innerMemberName + "." + outerMemberName;

var right = body.Right as PropertyExpression;
var rightValueDelegate = Expression.Lambda<Func<object>>(right).Compile();

VisibilityPropertyValue = rightValueDelegate();

etc.

I really recommend doing some reading to properly grok the expression API before diving in though; there are a lot of corner cases depending on how flexible you need to be. Eg is the expression always of the form parameter.Property.Property == constant ? It gets really complicated really quickly, so you'll want a solid understanding of the fundamentals before trying to handle any real-world cases.

There's a reasonable introduction to expression trees on MSDN , but some focused googling might get you a better understanding quicker.

You can use Funciton and Action class, I'm not very sure of what you want be able to do, but I can give an tip. Functions returns a value:

Function<InputType1,InputType2,ResultType> functionVariableName;

Usage:

functionVariableName = (param1, param2) => {
                                 //...process both params
                                 return result;
                          };

Actions, do not return values:

Action<InputType1,InputType2> actionVariableName;

Usage:

actionVariableName= (param1, param2) => {
                                 //...process both params                                     
                          };

If the lambda expression is simple (one line, with out if expression) you can make the lambda with out {} :

functionVariableName = (param1, param2) => //process values and result;

Hope this helps...

if you want to create an IEnumerable where the two properties are equal:

var results = EnabledPropertySelector.Where(l => l.FranchiseInfo.ExternalSystemType ==       
ExternalSystemTypes.Master.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM