This is my class:
class SampleExpression
{
public static void SampleEnum(IEnumerator<string> ien)
{
while (ien.MoveNext())
{
Console.WriteLine(ien.Current);
}
}
}
This is how i am trying to invoke the SampleEnum
method:
public void Main(string[] args)
{
ParameterExpression param2 = Expression.Parameter(typeof(SampleExpression), "args");
var lstConstant = "1,2,3,4,".Split(new string[] { "," },
StringSplitOptions.RemoveEmptyEntries).ToList();
Expression ep = Expression.Constant(Expression.Constant(lstConstant, typeof(IEnumerable<string>)));
var enummethod = typeof(SampleExpression).GetMethod("SampleEnum");
MethodCallExpression methodCall = Expression.Call
(
enummethod
, ep
);
var e = Expression.Lambda<Func<IEnumerator<string>, string>>(methodCall, param2);
e.Compile()(lstConstant.GetEnumerator());
}
I get the following error in the line which tries to create the method call expression:
Expression of type 'System.Linq.Expressions.TypedConstantExpression' cannot be used for parameter of type 'System.Collections.Generic.IEnumerator
1[System.String]' of method 'Void Enum(System.Collections.Generic.IEnumerator
1[System.String])'
Please help.