C
C#3y ago
PontiacGTX

Incorrect dynamic lambda generation

I have setup a method to dynamically create a lambda to select a parameter and create a new object based on certain fields the code looks like when generated
Param_0 => new MyType() {Amount = Param_0.Amount, Category = Param_0.Category, PaymentMode = Param_0.PaymentMode}
Param_0 => new MyType() {Amount = Param_0.Amount, Category = Param_0.Category, PaymentMode = Param_0.PaymentMode}
but shows an error when using the compiled lambda expression.... here is the code
public Expression<Func<TSource, object>> DynamicLambda<TSource>
(ParameterExpression parameterExpression = null, params string[] properties)
{
var entityType = typeof(TSource);
var props = properties.Select(x => entityType.GetProperty(x)).ToList();
var source = parameterExpression ==null? Expression.Parameter(entityType, "x"): parameterExpression;

// create x=> new myType{ prop1 = x.prop1,...}
var newType = CreateNewType(props);
var binding = props.Select(p => Expression.Bind(newType.GetField(p.Name),
Expression.Property(source, p.Name))).ToList();
var body = Expression.MemberInit(Expression.New(newType), binding);
var selector = Expression.Lambda<Func<TSource, object>>(body, source);
return selector;
}
public static Type CreateNewType(List<PropertyInfo> props)
{
AssemblyName asmName = new AssemblyName("MyAsm");
AssemblyBuilder dynamicAssembly = AssemblyBuilder
.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("MyAsm");
TypeBuilder dynamicAnonymousType = dynamicModule
.DefineType("MyType", TypeAttributes.Public);

foreach (var p in props)
{
dynamicAnonymousType.DefineField(p.Name, p.PropertyType, FieldAttributes.Public);
}
return dynamicAnonymousType.CreateType();
}
public Expression<Func<TSource, object>> DynamicLambda<TSource>
(ParameterExpression parameterExpression = null, params string[] properties)
{
var entityType = typeof(TSource);
var props = properties.Select(x => entityType.GetProperty(x)).ToList();
var source = parameterExpression ==null? Expression.Parameter(entityType, "x"): parameterExpression;

// create x=> new myType{ prop1 = x.prop1,...}
var newType = CreateNewType(props);
var binding = props.Select(p => Expression.Bind(newType.GetField(p.Name),
Expression.Property(source, p.Name))).ToList();
var body = Expression.MemberInit(Expression.New(newType), binding);
var selector = Expression.Lambda<Func<TSource, object>>(body, source);
return selector;
}
public static Type CreateNewType(List<PropertyInfo> props)
{
AssemblyName asmName = new AssemblyName("MyAsm");
AssemblyBuilder dynamicAssembly = AssemblyBuilder
.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("MyAsm");
TypeBuilder dynamicAnonymousType = dynamicModule
.DefineType("MyType", TypeAttributes.Public);

foreach (var p in props)
{
dynamicAnonymousType.DefineField(p.Name, p.PropertyType, FieldAttributes.Public);
}
return dynamicAnonymousType.CreateType();
}
1 Reply
PontiacGTX
PontiacGTXOP3y ago
how i call it
_Type.SetQuery(condition.DeserializeObject<FunctionRule>());
var items = rule.GetProperty(nameof(value)) ;
var values = items.GetEnumeratorList<string>();
var fields = rule.GetProperty(nameof(field)).GetString();
return DynamicLambda<T>(parm,properties: values/*.Append(field)*/.ToArray())
_Type.SetQuery(condition.DeserializeObject<FunctionRule>());
var items = rule.GetProperty(nameof(value)) ;
var values = items.GetEnumeratorList<string>();
var fields = rule.GetProperty(nameof(field)).GetString();
return DynamicLambda<T>(parm,properties: values/*.Append(field)*/.ToArray())
the json
{
"functions": [
{
"condition": "select",
"rules": [
{
"label": "Category",
"field": "Category",
"parent": "Transaction",
"operator": "groupby",
"type": "string",
"value": [ "Amount", "Category", "PaymentMode" ]
}
]
}
]

}
{
"functions": [
{
"condition": "select",
"rules": [
{
"label": "Category",
"field": "Category",
"parent": "Transaction",
"operator": "groupby",
"type": "string",
"value": [ "Amount", "Category", "PaymentMode" ]
}
]
}
]

}
rule base type
public class Rule
{
[JsonProperty("condition")]
public string Condition { get; set; }
[JsonProperty("rules")]
public RuleType[] Type { get; set; }
}
public class Rule
{
[JsonProperty("condition")]
public string Condition { get; set; }
[JsonProperty("rules")]
public RuleType[] Type { get; set; }
}
public class RuleType
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("field")]
public string Field { get; set; }
[JsonProperty("operator")]
public string Operator { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public object Value { get; set; }
[JsonProperty("parent")]
public string Parent { get; set; }
}
public class RuleType
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("field")]
public string Field { get; set; }
[JsonProperty("operator")]
public string Operator { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public object Value { get; set; }
[JsonProperty("parent")]
public string Parent { get; set; }
}
ok i found that i need to use my assembly name for it to work
Want results from more Discord servers?
Add your server