0

我一直在尝试自己编写这个非常简单的代码,但我发现它有点困难(Reflection.Emit 对我来说很新),所以我来了。我需要的是一个从超类继承并覆盖一个方法的整个类——所有这些都被编写为在运行时发出和创建。

我的超类 ClassA 是一个空类,除了实现 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 时,我找不到如何编写继承代码。另外,我不确定在 OPCode 中为 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

任何人?

4

1 回答 1

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

类构建器构建可以处置的类,这正是我想要的 :)

于 2013-09-02T13:53:12.580 回答