public static CustomAttribute GetMethodDllImportAttribute(MethodDefinition method)
        {
            MethodReference attributeConstructor = Utilities.GetEmptyConstructor(typeof(System.Runtime.InteropServices.DllImportAttribute),
                                                                                 method.DeclaringType.Module, new Type[] { typeof(string) });
            CustomAttribute dllImportAttr = new CustomAttribute(attributeConstructor);

            string                  dllName             = method.PInvokeInfo.Module.Name;
            TypeReference           dllNameArgumentType = method.DeclaringType.Module.TypeSystem.String;
            CustomAttributeArgument dllNameArg          = new CustomAttributeArgument(dllNameArgumentType, dllName);

            dllImportAttr.ConstructorArguments.Add(dllNameArg);

            CreateAndAddBestFitMappingFieldArgument(method, dllImportAttr);

            CreateAndAddCallingConventionFieldArgument(method, dllImportAttr);

            CreateAndAddCharSetFieldArgument(method, dllImportAttr);

            CreateAndAddEntryPointFieldArgument(method, dllImportAttr);

            CreateAndAddExactSpellingFieldArgument(method, dllImportAttr);

            CreateAndAddPreserveSigFieldArgument(method, dllImportAttr);

            CreateAndAddSetLastErrorFieldArgument(method, dllImportAttr);

            CreateAndAddThrowOnUnmappableCharFieldArgument(method, dllImportAttr);

            return(dllImportAttr);
        }
        public static CustomAttribute GetTypeExplicitLayoutAttribute(TypeDefinition type)
        {
            MethodReference         attCtor          = Utilities.GetEmptyConstructor(typeof(StructLayoutAttribute), type.Module, new Type[] { typeof(LayoutKind) });
            CustomAttribute         structLayoutAttr = new CustomAttribute(attCtor);
            TypeReference           layoutTypeRef    = JustDecompiler.Decompiler.Utilities.GetCorlibTypeReference(typeof(LayoutKind), type.Module);
            CustomAttributeArgument layoutType       = new CustomAttributeArgument(layoutTypeRef, (int)(LayoutKind.Explicit));

            structLayoutAttr.ConstructorArguments.Add(layoutType);

            return(structLayoutAttr);
        }
        public static CustomAttribute GetFieldFieldOffsetAttribute(FieldDefinition field)
        {
            MethodReference         attrCtor        = Utilities.GetEmptyConstructor(typeof(FieldOffsetAttribute), field.DeclaringType.Module, new Type[] { typeof(int) });
            CustomAttribute         fieldOffsetAttr = new CustomAttribute(attrCtor);
            TypeReference           fieldOffsetType = field.FieldType.Module.TypeSystem.Int32;
            CustomAttributeArgument offsetArg       = new CustomAttributeArgument(fieldOffsetType, field.Offset);

            fieldOffsetAttr.ConstructorArguments.Add(offsetArg);

            return(fieldOffsetAttr);
        }
        public static CustomAttribute GetMethodImplAttribute(MethodDefinition method)
        {
            Type[]          argumentTypes = DoesMethodHaveMethodImplOptions(method) ? new Type[] { typeof(MethodImplOptions) } : new Type[0];
            MethodReference constructor   = Utilities.GetEmptyConstructor(typeof(MethodImplAttribute), method.DeclaringType.Module, argumentTypes);
            CustomAttribute attribute     = new CustomAttribute(constructor);

            AddMethodImplOptions(method, attribute);
            AddMethodCodeType(method, attribute);

            return(attribute);
        }
        public static CustomAttribute GetExportedTypeAttribute(ExportedType exportedType, ModuleDefinition module)
        {
            MethodReference ctor = Utilities.GetEmptyConstructor(typeof(System.Runtime.CompilerServices.TypeForwardedToAttribute), module, new[] { typeof(System.Type) });

            CustomAttribute exportedTypeAttribute = new CustomAttribute(ctor);
            TypeReference   systemType            = Utilities.GetCorlibTypeReference(typeof(Type), module);
            TypeReference   type = exportedType.CreateReference();

            exportedTypeAttribute.ConstructorArguments.Add(new CustomAttributeArgument(systemType, type));

            return(exportedTypeAttribute);
        }
        public static CustomAttribute GetTypeSerializableAttribute(TypeDefinition type)
        {
            MethodReference attCtor = Utilities.GetEmptyConstructor(typeof(SerializableAttribute), type.Module);

            return(new CustomAttribute(attCtor));
        }
        public static CustomAttribute GetFieldNotSerializedAttribute(FieldDefinition field)
        {
            MethodReference attCtor = Utilities.GetEmptyConstructor(typeof(NonSerializedAttribute), field.DeclaringType.Module);

            return(new CustomAttribute(attCtor));
        }