private Func <TypeDefinition[]> DefineClrHandleStructInternal(ClangStructInfo structInfo, int?bits = null) { if (structInfo.Size > 0) { throw new NotImplementedException(); } var structName = structInfo.Name; if (TypeRedirects.TryGetValue(structName, out var rename)) { structName = rename; } if (Module.GetType(structName)?.Resolve() != null) { return(null); } // handle type var handleDef = Module.DefineType(structName, PublicSealedStructTypeAttributes, size: 0); handleDef.SetCustomAttribute(() => new BinderGeneratedAttribute()); //handleDef.SetCustomAttribute(StructLayoutSequentialAttributeInfo); var handleInterface = IHandleGtd.MakeGenericInstanceType(handleDef); handleDef.AddInterfaceImplementation(handleInterface); var handlePointerType = handleDef.MakePointerType(); var inputTypes = bits == null ? TypeArrayOfSingularVoidPointer : bits == 64 ? TypeArrayOfSingularULong : TypeArrayOfSingularUInt; var castMethod = handleDef.DefineMethod("Cast", PublicStaticMethodAttributes, handlePointerType, inputTypes); SetMethodInliningAttributes(castMethod); castMethod.GenerateIL(il => { il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ret); }); var handleType = handleDef.CreateType(); return(() => new[] { handleType }); }
private bool IsHandleType(TypeReference t) { TypeReference interfaceType; if (t.IsPrimitive() || t.IsIndirect()) { return(false); } try { interfaceType = IHandleGtd.MakeGenericInstanceType(t); } catch { return(false); } var result = t.GetInterfaces().Contains(interfaceType); //.Any( i => i.Is(interfaceType)); return(result); }