private static Type CreateDelegateType(BasicSignature signature) { string delTypeName = signature.UniqueName; TypeAttributes typeAttr = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass; TypeBuilder del = CppLibrary.interopModule.DefineType (delTypeName, typeAttr, typeof(MulticastDelegate)); if (signature.CallingConvention.HasValue) { ConstructorInfo ufpa = typeof (UnmanagedFunctionPointerAttribute).GetConstructor (new Type [] { typeof (CallingConvention) }); CustomAttributeBuilder unmanagedPointer = new CustomAttributeBuilder (ufpa, new object [] { signature.CallingConvention.Value }); del.SetCustomAttribute (unmanagedPointer); } MethodAttributes ctorAttr = MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public; ConstructorBuilder ctor = del.DefineConstructor (ctorAttr, CallingConventions.Standard, new Type[] { typeof(object), typeof(System.IntPtr) }); ctor.SetImplementationFlags (MethodImplAttributes.Runtime | MethodImplAttributes.Managed); Type [] parameterTypes = signature.ParameterTypes.ToArray (); MethodAttributes methodAttr = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual; MethodBuilder invokeMethod = del.DefineMethod ("Invoke", methodAttr, signature.ReturnType, parameterTypes); invokeMethod.SetImplementationFlags (MethodImplAttributes.Runtime | MethodImplAttributes.Managed); return del.CreateType (); }
public static Type GetDelegateType(BasicSignature signature) { Type delegateType; if (type_cache == null) type_cache = new Dictionary<BasicSignature, Type> (); if (!type_cache.TryGetValue (signature, out delegateType)) { delegateType = CreateDelegateType (signature); type_cache.Add (signature, delegateType); } return delegateType; }
public bool IsCompatibleWith(BasicSignature other) { return CallingConvention == other.CallingConvention && ((ParameterTypes == null && other.ParameterTypes == null) || ParameterTypes.SequenceEqual (other.ParameterTypes)) && ReturnType.Equals (other.ReturnType); }