我正在尝试制作动态表达式并将 lambda 分配给它。结果,我得到了异常:System.ArgumentException:“Test.ItsTrue”类型的表达式不能用于分配给“System.Linq.Expressions.Expression`1[Test.ItsTrue]”类型
怎么了?
public delegate bool ItsTrue();
public class Sample
{
public Expression<ItsTrue> ItsTrue { get; set; }
}
[TestClass]
public class MyTest
{
[TestMethod]
public void TestPropertySetWithExpressionOfDelegate()
{
Expression<ItsTrue> itsTrue = () => true;
// this works at compile time
new Sample().ItsTrue = itsTrue;
// this does not work ad runtime
var new_ = Expression.New(typeof (Sample));
var result = Expression.Assign(
Expression.Property(new_, typeof (Sample).GetProperties()[0]),
itsTrue);
}
}