public T GetNativeDel <T>(string funcName) { NativeMethodMap nativeMethod = new NativeMethodMap(funcName); nativeMethod.Resolve(nativeModule); return((T)nativeMethod.GetDelegate <T>()); }
public bool LoadRequestProcs(Type holderType) { //1. load module //2. load procedure if (!string.IsNullOrEmpty(this.loadModuleFileName)) { this.nativeModule = UnsafeMethods.LoadLibrary(this.loadModuleFileName); if (nativeModule == IntPtr.Zero) { return(false); } } //--------------------------------------------------- var allFields = holderType.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); int j = allFields.Length; Type nativeNameAttrType = typeof(NativeFunc); Type nativeFuncPointerType = typeof(UnmanagedFunctionPointerAttribute); for (int i = 0; i < j; ++i) { var field = allFields[i]; var nativeNameAttrs = field.GetCustomAttributes(nativeNameAttrType, false); if (nativeNameAttrs.Length > 0) { //only 1 NativeFunc natName = (NativeFunc)nativeNameAttrs[0]; Type fieldType = field.FieldType; string procName = fieldType.Name; //------------------------------------------------------------ if (!string.IsNullOrEmpty(natName.NativeName)) { //use field name procName = natName.NativeName; } //------------------------------------------------------------ //find-resolve-assign NativeMethodMap nativeMethodMap = new NativeMethodMap(procName); if (nativeMethodMap.Resolve(nativeModule, fieldType)) { importFuncs[procName] = nativeMethodMap; field.SetValue(null, nativeMethodMap.GetCacheDelegate()); } } } return(true); }