使用Emit动态创建类后设置属性值失败
问题:从配置文件中获取数据结构,使用Emit动态创建了类,然后为属性赋值时出错,提示“”无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容。“”
EmitHelper emit = new EmitHelper();
emit.CreateType("DynamicAssembly", "Student2");
emit.CreateProperty("Name", typeof(string));
emit.Save();
object ee = emit.CreateInstance();
emit.Set(ee, "Name", "sdasdas");
s = emit.Get(ee, "Name").ToString();
//帮助
public class EmitHelper
{
private TypeBuilder _typeBuilder = null;
private AssemblyBuilder _assemblyBuilder = null;
public Type ThisType { get; set; }
public delegate void SetPropertyValue(object obj, object value);
public delegate object GetPropertyValue(object obj);
private Dictionary<string, SetPropertyValue> _dicSetProperty = new Dictionary<string, SetPropertyValue>();
private Dictionary<string, GetPropertyValue> _dicGetProperty = new Dictionary<string, GetPropertyValue>();
private Dictionary<string, PropertyInfo> _dicProperty = new Dictionary<string, PropertyInfo>();
public void CreateType(string assembly, string type)
{
AssemblyName DemoName = new AssemblyName(assembly);
_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(DemoName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = _assemblyBuilder.DefineDynamicModule(DemoName.Name, DemoName.Name + ".dll");
_typeBuilder = mb.DefineType(type, TypeAttributes.Public);
}
public void CreateProperty(string propertyName, Type propertyType)
{
TypeBuilder tb = _typeBuilder;
var field = tb.DefineField("_" + propertyName, propertyType, FieldAttributes.Private);
var getMethod = tb.DefineMethod("get_" + propertyName, MethodAttributes.Public, propertyType, null);
var setMethod = tb.DefineMethod("set_" + propertyName, MethodAttributes.Public, null, new Type[] { propertyType });
var ilGet = getMethod.GetILGenerator();
ilGet.Emit(OpCodes.Ldarg_0);
ilGet.Emit(OpCodes.Ldfld, field);
ilGet.Emit(OpCodes.Ret);
var ilSet = setMethod.GetILGenerator();
ilSet.Emit(OpCodes.Ldarg_0);
ilSet.Emit(OpCodes.Ldarg_1);
ilSet.Emit(OpCodes.Stfld, field);
ilSet.Emit(OpCodes.Ret);
var property = tb.DefineProperty(propertyName, PropertyAttributes.None, propertyType, null);
property.SetGetMethod(getMethod);
property.SetSetMethod(setMethod);
Save();
var callMethod = ThisType.GetMethod("set_" + propertyName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);
var para = callMethod.GetParameters()[0];
//// 创建动态函数
DynamicMethod method = new DynamicMethod("EmitCallable", null, new Type[] { ThisType, propertyType }, ThisType);
// 获取动态函数的 IL 生成器
var il = method.GetILGenerator();
// 创建一个本地变量,主要用于 Object Type to Propety Type
var local = il.DeclareLocal(para.ParameterType, true);
// 加载第 2 个参数【(T owner, object value)】的 value
il.Emit(OpCodes.Ldarg_1);
if (para.ParameterType.IsValueType)
{
il.Emit(OpCodes.Unbox_Any, para.ParameterType);// 如果是值类型,拆箱 string = (string)object;
}
else
{
il.Emit(OpCodes.Castclass, para.ParameterType);// 如果是引用类型,转换 Class = object as Class
}
il.Emit(OpCodes.Stloc, local);// 将上面的拆箱或转换,赋值到本地变量,现在这个本地变量是一个与目标函数相同数据类型的字段了。
il.Emit(OpCodes.Ldarg_0); // 加载第一个参数 owner
il.Emit(OpCodes.Ldloc, local);// 加载本地参数
il.EmitCall(OpCodes.Callvirt, callMethod, null);//调用函数
il.Emit(OpCodes.Ret); // 返回
_dicSetProperty[propertyName] = method.CreateDelegate(typeof(SetPropertyValue)) as SetPropertyValue;
var dm = new DynamicMethod(name: "EmittedGetter", returnType: typeof(string), parameterTypes: new[] { ThisType }, owner: ThisType);
var type = ThisType;
var prop = type.GetMethod("get_Name");
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, prop);
il.Emit(OpCodes.Ret);
_dicGetProperty[propertyName] = dm.CreateDelegate(typeof(GetPropertyValue)) as GetPropertyValue;
}
public Type Save()
{
ThisType = _typeBuilder.CreateType();
return ThisType;
}
public object CreateInstance()
{
return Activator.CreateInstance(ThisType);
}
public void Set(object obj, string propertyName, object value)
{
_dicSetProperty[propertyName](obj, value);
}
public object Get(object obj, string propertyName)
{
return _dicGetProperty[propertyName](obj);
}
}
在CreateDelegate(typeof(GetPropertyValue)) as GetPropertyValue;这里报错,提示“”“无法绑定到目标方法,因其签名或安全透明度与委托类型的签名或安全透明度不兼容。”
创建类是正确的,通过反射可以看到创建类的源码:

使用反射获取或设置属性没有问题,但使用CreateDelegate的方法就报错。请问这是什么原因呢,怎么修改?
相关推荐
Vue 3 emit参数问题解析[源码]
本文深入分析了Vue 3中emit参数数量不匹配的问题,详细解释了Vue 3的emit机制和TypeScript的类型校验机制。文章提供了四种解决方案:修正调用参数、使用函数重载精确定义类型、运行时验证与类型推导以及使用emits选项对象语法。此外,还介绍了高级模式与最佳实践,包括统一事件管理模式和组合式函数封装,以及常见陷阱与调试技巧。最后总结了Vue 3 emit参数校验的核心要点,帮助开发者构建类型安全且易于维护的组件通信体系。
Python PyQt5运行程序把输出信息展示到GUI图形界面上
概述:最近在赶毕业设计,遇到一个问题,爬虫模块我用PyQt5写了图形界面,为了将所有的输出信息都显示到图形界面上遇到了问题。 先演示一下效果最终效果吧,下面两张图用来镇楼。可以看到我们图形界面和程序运行的返回的信息是一样的,并且成功把数据展示到图形界面。 1.怎么获取输出信息。eg:我们平时用编译器,例如pycharm,运行的时候,会有很多信息,或者直接在cmd窗口输入命令的时候,也有同样信息。例如我启动数据库。 cmd窗口下执行命令返回的信息
提升反射的性能(有用,启发)
你还在为大量使用反射导致性能下降而郁闷吗?你还在为性能考虑而不得不无奈的放弃灵活的反射吗?下了这个库之后,你就不需要担心这些问题了!原来反射也是快速的!
MongoDB中MapReduce编程模型使用实例
作为一个优秀的编程模型,MapReduce在大数据处理中有很大的优势,而mongodb也支持这一编程模型,本文通过简单的单词计数示例论述在mongodb中如何使用MapReduce
Android安卓使用iosocket进行Socket通信简单封装优雅写代码
对iosocket进行简单封装,使得定制代码更加容易,迭代开发十分方便,实现方式十分优雅,作为一个有洁癖的程序员,自然要用最优雅的方式写出高质量的代码啦


