follogwing代码生成我的Setter Func,我可以通过字符串设置对象的枚举属性!
public static Action<TObject, TProperty> GetPropSetter<TObject, TProperty>(PropertyInfo pinfo)
{
ParameterExpression paramExpression = Expression.Parameter(typeof(TObject));
ParameterExpression paramExpression2 = Expression.Parameter(typeof(TProperty), pinfo.Name);
var prop2 = Expression.Convert(Expression.Call(typeof (Enum).GetMethod("Parse", new Type[] {typeof (Type), typeof (string), typeof(bool)}), Expression.Constant(pinfo.PropertyType), paramExpression2, Expression.Constant(true)), pinfo.PropertyType);
MemberExpression propertyExpression = Expression.Property(paramExpression, pinfo.Name);
Action<TObject, TProperty> result = Expression.Lambda<Action<TObject, TProperty>>
(
Expression.Assign(propertyExpression, prop2), paramExpression, paramExpression2
).Compile();
return result;
}
这在WPF中完美运行!但是当我在Silverlight中运行它时出现了这个错误:
Fehler beim Versuch von Methode “DynamicClass.lambda_method(System.Runtime.CompilerServices.Closure, MCC.ConnectorsV2.WMSConnectorServiceInterfaces.DTO.TransportLabelDTO, System.String)“,auf Typ”System.RuntimeType“zuzugreifen。