我很反思。我需要创建一个继承父类的类。我需要创建一个只读属性。此属性通过传递参数25来调用父类中的现有函数。
一切正常,但我无法将值25传递给被调用的函数。下面是生成类的代码。请协助。感谢。
Public Shared Function GetDynamicClass() As Type
Dim asmName As New AssemblyName
asmName.Name = "MyAssm"
Dim asmBuilder As AssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly (asmName, AssemblyBuilderAccess.RunAndSave)
Dim mdlBuilder As ModuleBuilder = asmBuilder.DefineDynamicModule("MyDynModule")
Dim TypeBldr As TypeBuilder = mdlBuilder.DefineType("MyDynClass", TypeAttributes.[Public] Or TypeAttributes.[Class])
TypeBldr.SetParent(GetType(MyParent))
Dim PropertyName As String = ""
Dim PropBldr As PropertyBuilder = Nothing
Dim GetSetAttr As MethodAttributes = Nothing
Dim currGetPropMthdBldr As MethodBuilder = Nothing
Dim currGetIL As ILGenerator = Nothing
Dim mi As MethodInfo = Nothing
PropertyName = "SurveyDate"
PropBldr = TypeBldr.DefineProperty(PropertyName, PropertyAttributes.None, GetType(Object), New Type() {GetType(Object)})
GetSetAttr = MethodAttributes.[Public] Or MethodAttributes.HideBySig
currGetPropMthdBldr = TypeBldr.DefineMethod("get_value", GetSetAttr, GetType(Object), Type.EmptyTypes)
currGetIL = currGetPropMthdBldr.GetILGenerator()
mi = GetType(MyParent).GetMethod("GetProgress")
currGetIL.DeclareLocal(GetType(Object))
currGetIL.Emit(OpCodes.Ldarg_0)
currGetIL.Emit(OpCodes.Ldc_I4_0)
currGetIL.Emit(OpCodes.Conv_I8)
currGetIL.Emit(OpCodes.Call, mi)
currGetIL.Emit(OpCodes.Ret)
PropBldr.SetGetMethod(currGetPropMthdBldr)
Return TypeBldr.CreateType
End Function
答案 0 :(得分:5)
假设你改变了这个:
currGetIL.Emit(OpCodes.Ldc_I4_0)
currGetIL.Emit(OpCodes.Conv_I8)
进入这个:
currGetIL.Emit(OpCodes.Ldc_I4, 25)
currGetIL.Emit(OpCodes.Conv_I8)
LDC_I4_0是加载值“0”的操作码。另一方面,LDC_I4允许您自己指定实际参数。
(警告:未经测试,通过阅读文档获得此信息)