protected override bool ReleaseHandle() { _deleteInstance(this.handle); //makes GC live easier _newInstance = null; _deleteInstance = null; return true; }
public DatabaseHandle(LibraryHandle libraryHandle) : base(true) { LibraryHandle = libraryHandle; _newInstance = libraryHandle.GetUnmanagedDelegate<NewInstanceDelegate>(); _deleteInstance = libraryHandle.GetUnmanagedDelegate<DeleteInstanceDelegate>(); this.handle = _newInstance(LibraryHandle); if (IsInvalid) { throw new EjdbException("Unable to create ejdb instance"); } }
//Creates new instance of ejdb. Don't know what' is it, but it looks it should be done before opening database public unsafe DatabaseFunctions(LibraryHandle handle) { NewInstance = handle.GetUnmanagedDelegate <NewInstanceDelegate>(); DeleteInstance = handle.GetUnmanagedDelegate <DeleteInstanceDelegate>(); OpenDatabase = handle.GetUnmanagedDelegate <OpenDatabaseDelegate>(); CloseDatabase = handle.GetUnmanagedDelegate <CloseDatabaseDelegate>(); IsOpen = handle.GetUnmanagedDelegate <IsOpenDelegate>(); GetErrorCode = handle.GetUnmanagedDelegate <GetErrorCodeDelegate>(); GetMetadata = handle.GetUnmanagedDelegate <GetMetaDelegate>(); Command = handle.GetUnmanagedDelegate <CommandDelegate>(); Sync = handle.GetUnmanagedDelegate <SyncDelegate>(); }
private CtorAccessor(Type type) { if (type.IsValueType) { DynamicMethod dynamicNewInstance = new DynamicMethod("$NewInstance", typeofObject, zeroTypeArray, type, true); ILGenerator newInstanceGen = dynamicNewInstance.GetILGenerator(); LocalBuilder v = newInstanceGen.DeclareLocal(type); newInstanceGen.Emit(OpCodes.Ldloca_S, v); newInstanceGen.Emit(OpCodes.Initobj, type); newInstanceGen.Emit(OpCodes.Ldloc_S, v); newInstanceGen.Emit(OpCodes.Box, type); newInstanceGen.Emit(OpCodes.Ret); newInstanceDelegate = (NewInstanceDelegate)dynamicNewInstance.CreateDelegate(typeofNewInstanceDelegate); return; } BindingFlags bindingflags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; ConstructorInfo ctor = type.GetConstructor(bindingflags, null, zeroTypeArray, zeroParameterModifierArray); if (ctor != null) { DynamicMethod dynamicNewInstance = new DynamicMethod("$NewInstance", typeofObject, zeroTypeArray, type, true); ILGenerator newInstanceGen = dynamicNewInstance.GetILGenerator(); newInstanceGen.Emit(OpCodes.Newobj, ctor); newInstanceGen.Emit(OpCodes.Ret); newInstanceDelegate = (NewInstanceDelegate)dynamicNewInstance.CreateDelegate(typeofNewInstanceDelegate); } else { ConstructorInfo[] ctors = type.GetConstructors(bindingflags); Array.Sort(ctors, 0, ctors.Length, new ConstructorComparator()); for (int i = 0, length = ctors.Length; i < length; i++) { try { DynamicMethod dynamicNewInstance = new DynamicMethod("$NewInstance", typeofObject, zeroTypeArray, type, true); ParameterInfo[] pi = ctors[i].GetParameters(); int piLength = pi.Length; ILGenerator newInstanceGen = dynamicNewInstance.GetILGenerator(); for (int j = 0; j < piLength; j++) { Type parameterType = pi[j].ParameterType; if (parameterType == typeofInt8 || parameterType == typeofBoolean || parameterType == typeofUInt8 || parameterType == typeofInt16 || parameterType == typeofUInt16 || parameterType == typeofChar || parameterType == typeofInt32 || parameterType == typeofUInt32) { newInstanceGen.Emit(OpCodes.Ldc_I4_0); } else if (parameterType == typeofInt64 || parameterType == typeofUInt64) { newInstanceGen.Emit(OpCodes.Ldc_I8, (long)0); } else if (parameterType == typeofSingle) { newInstanceGen.Emit(OpCodes.Ldc_R4, (float)0); } else if (parameterType == typeofDouble) { newInstanceGen.Emit(OpCodes.Ldc_R8, (double)0); } else if (parameterType.IsValueType) { LocalBuilder v = newInstanceGen.DeclareLocal(parameterType); newInstanceGen.Emit(OpCodes.Ldloca_S, v); newInstanceGen.Emit(OpCodes.Initobj, parameterType); newInstanceGen.Emit(OpCodes.Ldloc_S, v); } else { newInstanceGen.Emit(OpCodes.Ldnull); } } newInstanceGen.Emit(OpCodes.Newobj, ctors[i]); if (type.IsValueType) { newInstanceGen.Emit(OpCodes.Box, type); } newInstanceGen.Emit(OpCodes.Ret); newInstanceDelegate = (NewInstanceDelegate)dynamicNewInstance.CreateDelegate(typeofNewInstanceDelegate); newInstanceDelegate(); break; } catch (Exception e) { Console.WriteLine(e); newInstanceDelegate = null; } } } if (newInstanceDelegate == null) { throw new NotSupportedException(); } }