Home All Groups Group Topic Archive Search About

CustomAttributeBuilder on the return type in .net 1.1

Author
17 Mar 2006 9:52 AM
Anthony Lloyd
I need to add a custom mashaler to a dynamic delegate I'm constructing.
methodBuilder.DefineParameter in 1.1 does allow you to access the return
type and I can't seem to get SetMarshal to work with a CustomMarshaler.
Does anyone know how you go about doing this in 1.1?
Thanks

        private Type CreateDelegateType(ModuleBuilder modBuilder)
        {   
            // Create a delegate that has the same signature as the method we would
like to hook up to
            TypeBuilder typeBuilder = modBuilder.DefineType("f" + Index + "Delegate",
                            TypeAttributes.Class  | TypeAttributes.Public |
                            TypeAttributes.Sealed,
                            typeof(System.MulticastDelegate));
            ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(
                            MethodAttributes.RTSpecialName | MethodAttributes.HideBySig |
                            MethodAttributes.Public, CallingConventions.Standard,
                            new Type[] { typeof(object), typeof(int) });
            constructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime |
                                                      MethodImplAttributes.Managed );

            Type[] paramTypes = new Type[Parameters.Length];
            for(int i=0;i<paramTypes.Length;i++)
                paramTypes[i]=Parameters[i].DelegateParamType;

            // Define the Invoke method for the delegate
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Invoke",
                            MethodAttributes.Public | MethodAttributes.HideBySig |
                            MethodAttributes.NewSlot | MethodAttributes.Virtual,
                            IsCommand ? typeof(void) : ReturnType.DelegateParamType, // What here
for macro? null or Void ?
                            paramTypes);
            methodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime |
                            MethodImplAttributes.Managed);

            // Set Marshal Attributes for return type
            if (!IsCommand && ReturnType.MarshalAsAttribute != null)
            {                 methodBuilder.SetMarshal(UnmanagedMarshal.DefineUnmanagedMarshal(System.Runtime.InteropServices.UnmanagedType.CustomMarshaler));
                //above line throws and 'not simple type exception
            }

            // ... and the parameters
            for (int i = 1; i <= Parameters.Length; i++)
            {
                CustomAttributeBuilder b = Parameters[i-1].MarshalAsAttribute;
                if (b != null)
                {
                    ParameterBuilder pb = methodBuilder.DefineParameter(i,
ParameterAttributes.None, null);
                    pb.SetCustomAttribute(b);
                }
            }

            // Bake the type and get the delegate
            return typeBuilder.CreateType();
        }

AddThis Social Bookmark Button