I am trying to call a parameterized constructor from an expression instead of using the default ctor. this is the code that gets the constructor parameter:
ConstructorInfo ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(bool) }, new ParameterModifier[] { }); ParameterInfo[] paramsInfo = ci.GetParameters(); //create a single param of type object[] ParameterExpression param = Expression.Parameter(typeof(bool), "il"); Expression[] argsExp = new Expression[paramsInfo.Length]; //pick each arg from the params array //and create a typed expression of them for (int i = 0; i < paramsInfo.Length; i++) { Expression index = Expression.Constant(i); Type paramType = paramsInfo[i].ParameterType; Expression paramAccessorExp = param; //Expression.ArrayIndex(param, index); Expression paramCastExp = Expression.Convert(paramAccessorExp, paramType); argsExp[i] = param; } NewExpression ci2 = Expression.New(ci, argsExp);
But if I try to compile a lambda expression, I get the following error:
variable 'il' of type 'System.Boolean' referring to scope '' but not defined
What am I missing? Any help and / or hint appreciated.
source share