我一直在尝试自己写这个非常简单的代码,但我发现它有点困难(Reflection.Emit对我来说很新),所以我在这里。 我需要的是一个继承自超类并覆盖方法的整个类 - 所有这些都是在运行时发出和创建的。
我的超类ClassA是一个没有任何代码的空类,除了Implement IDispose:
Namespace Basic
Public Class ClassA
Implements IDisposable
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
End Namespace
我的ClassB是在运行时创建的。它应该是这样的:
Namespace Basic
Public Class ClassB
Inherits ClassA
Protected Overrides Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
End Class
End Namespace
我一直在搜索很多,在创建ClassB时我无法找到如何编写继承代码。 另外,我不确定在OPCB for ClassB
中编写调用段.method family strict virtual instance void
Dispose(bool disposing) cil managed
{
// Code size 11 (0xb)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: call instance void WindowsApplication1.Basic.ClassA::Dispose(bool)
IL_0008: nop
IL_0009: nop
IL_000a: ret
} // end of method ClassB::Dispose
任何?
答案 0 :(得分:0)
这就是我所要做的。 这个类包含iDisposable:
Public Class Super
Implements IDisposable
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
For Each prop As PropertyInfo In Me.GetType.GetProperties
If prop.CanWrite Then
prop.SetValue(Me, Nothing, Nothing)
End If
Next
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Overridable Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
...这是我继承自Super
的类构建器 Public Class ClassCreator
Public Shared Function CreateClass() As Object
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")
Dim name As String = "DefineMethodOverrideExample"
Dim myAssemblyName As New AssemblyName(name)
Dim myAssemblyBuilder As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave)
Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule(name, name & ".dll")
Dim myTypebuilder As TypeBuilder = myModuleBuilder.DefineType("C", TypeAttributes.Public, GetType(Super))
Dim myMethodBuilder As MethodBuilder = myTypebuilder.DefineMethod("Dispose",
MethodAttributes.Public Or MethodAttributes.ReuseSlot Or MethodAttributes.Virtual Or MethodAttributes.HideBySig,
Nothing,
Type.EmptyTypes)
Dim il As ILGenerator = myMethodBuilder.GetILGenerator()
il = myMethodBuilder.GetILGenerator()
il.Emit(OpCodes.Nop)
il.Emit(OpCodes.Ldarg_0)
il.Emit(OpCodes.Call, GetType(Super).GetMethod("Dispose"))
il.Emit(OpCodes.Nop)
il.Emit(OpCodes.Nop)
il.Emit(OpCodes.Ret)
Dim tc As Type = myTypebuilder.CreateType()
myAssemblyBuilder.Save(name & ".dll")
Dim test As Object = Activator.CreateInstance(tc)
Return test
End Function
End Class
类构建器构建可以是Disposed的类,这就是我想要的:)