Dim res = f.MyMethod(Of DYNAMIC_CLASS_TYPE)("select ID,name from mytable")
我想基于我的查询列创建一个动态类,然后传递该类而不是DYNAMIC_CLASS_TYPE.
我怎样才能做到这一点?
解决方法 我终于成功了.我的代码如下:Public Shared Function CreateClass(ByVal classname As String,ByVal propertIEs As Dictionary(Of String,Type)) As Type Dim myDomain As AppDomain = AppDomain.CurrentDomain Dim myAsmname As New Assemblyname("MyAssembly") Dim myAssembly As AssemblyBuilder = myDomain.defineDynamicAssembly(myAsmname,AssemblyBuilderAccess.Run) Dim myModule As ModuleBuilder = myAssembly.defineDynamicModule("MyModule") Dim myType As TypeBuilder = myModule.defineType(classname,TypeAttributes.Public) myType.defineDefaultConstructor(MethodAttributes.Public) For Each o In propertIEs Dim prop As PropertyBuilder = myType.defineProperty(o.Key,PropertyAttributes.HasDefault,o.Value,nothing) Dim fIEld As FIEldBuilder = myType.defineFIEld("_" + o.Key,GetType(Integer),FIEldAttributes.[Private]) Dim getter As MethodBuilder = myType.defineMethod("get_" + o.Key,MethodAttributes.[Public] Or MethodAttributes.Specialname Or MethodAttributes.HIDeBySig,Type.EmptyTypes) Dim getterIL As ILGenerator = getter.GetILGenerator() getterIL.Emit(OpCodes.Ldarg_0) getterIL.Emit(OpCodes.Ldfld,fIEld) getterIL.Emit(OpCodes.Ret) Dim setter As MethodBuilder = myType.defineMethod("set_" + o.Key,nothing,New Type() {o.Value}) Dim setterIL As ILGenerator = setter.GetILGenerator() setterIL.Emit(OpCodes.Ldarg_0) setterIL.Emit(OpCodes.Ldarg_1) setterIL.Emit(OpCodes.Stfld,fIEld) setterIL.Emit(OpCodes.Ret) prop.Setgetmethod(getter) prop.SetSetMethod(setter) Next Return myType.CreateType()End Function
函数的返回值是我的自定义类的类型.
总结以上是内存溢出为你收集整理的vb.net – 在运行时动态创建类全部内容,希望文章能够帮你解决vb.net – 在运行时动态创建类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)