using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace SharpDX { internal class CppObjectVtbl { private readonly List methods; private readonly IntPtr vtbl; /// /// Default Constructor. /// /// number of methods to allocate in the VTBL public CppObjectVtbl(int numberOfCallbackMethods) { // Allocate ptr to vtbl vtbl = Marshal.AllocHGlobal(IntPtr.Size * numberOfCallbackMethods); methods = new List(); } /// /// Gets the pointer to the vtbl. /// public IntPtr Pointer { get { return vtbl; } } /// /// Add a method supported by this interface. This method is typically called from inherited constructor. /// /// the managed delegate method public unsafe void AddMethod(Delegate method) { int index = methods.Count; methods.Add(method); *((IntPtr*) vtbl + index) = Marshal.GetFunctionPointerForDelegate(method); } } }