internal void SetAttribute(CustomAttributeBuilder cab)
        {
            Universe u    = cab.Constructor.Module.universe;
            Type     type = cab.Constructor.DeclaringType;

            if (copyright == null && type == u.System_Reflection_AssemblyCopyrightAttribute)
            {
                copyright = (string)cab.GetConstructorArgument(0);
            }
            else if (trademark == null && type == u.System_Reflection_AssemblyTrademarkAttribute)
            {
                trademark = (string)cab.GetConstructorArgument(0);
            }
            else if (product == null && type == u.System_Reflection_AssemblyProductAttribute)
            {
                product = (string)cab.GetConstructorArgument(0);
            }
            else if (company == null && type == u.System_Reflection_AssemblyCompanyAttribute)
            {
                company = (string)cab.GetConstructorArgument(0);
            }
            else if (description == null && type == u.System_Reflection_AssemblyDescriptionAttribute)
            {
                description = (string)cab.GetConstructorArgument(0);
            }
            else if (title == null && type == u.System_Reflection_AssemblyTitleAttribute)
            {
                title = (string)cab.GetConstructorArgument(0);
            }
            else if (informationalVersion == null && type == u.System_Reflection_AssemblyInformationalVersionAttribute)
            {
                informationalVersion = (string)cab.GetConstructorArgument(0);
            }
            else if (culture == null && type == u.System_Reflection_AssemblyCultureAttribute)
            {
                culture = (string)cab.GetConstructorArgument(0);
            }
            else if (fileVersion == null && type == u.System_Reflection_AssemblyFileVersionAttribute)
            {
                fileVersion = (string)cab.GetConstructorArgument(0);
            }
        }
예제 #2
0
		internal void SetAttribute(CustomAttributeBuilder cab)
		{
			Universe u = cab.Constructor.Module.universe;
			Type type = cab.Constructor.DeclaringType;
			if (copyright == null && type == u.System_Reflection_AssemblyCopyrightAttribute)
			{
				copyright = (string)cab.GetConstructorArgument(0);
			}
			else if (trademark == null && type == u.System_Reflection_AssemblyTrademarkAttribute)
			{
				trademark = (string)cab.GetConstructorArgument(0);
			}
			else if (product == null && type == u.System_Reflection_AssemblyProductAttribute)
			{
				product = (string)cab.GetConstructorArgument(0);
			}
			else if (company == null && type == u.System_Reflection_AssemblyCompanyAttribute)
			{
				company = (string)cab.GetConstructorArgument(0);
			}
			else if (description == null && type == u.System_Reflection_AssemblyDescriptionAttribute)
			{
				description = (string)cab.GetConstructorArgument(0);
			}
			else if (title == null && type == u.System_Reflection_AssemblyTitleAttribute)
			{
				title = (string)cab.GetConstructorArgument(0);
			}
			else if (informationalVersion == null && type == u.System_Reflection_AssemblyInformationalVersionAttribute)
			{
				informationalVersion = (string)cab.GetConstructorArgument(0);
			}
			else if (culture == null && type == u.System_Reflection_AssemblyCultureAttribute)
			{
				culture  = (string)cab.GetConstructorArgument(0);
			}
			else if (fileVersion == null && type == u.System_Reflection_AssemblyFileVersionAttribute)
			{
				fileVersion = (string)cab.GetConstructorArgument(0);
			}
		}
예제 #3
0
		private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			object val = customBuilder.GetConstructorArgument(0);
			LayoutKind layout;
			if (val is short)
			{
				layout = (LayoutKind)(short)val;
			}
			else
			{
				layout = (LayoutKind)val;
			}
			StructLayoutAttribute attr = new StructLayoutAttribute(layout);
			attr.Pack = (int?)customBuilder.GetFieldValue("Pack") ?? 0;
			attr.Size = (int?)customBuilder.GetFieldValue("Size") ?? 0;
			attr.CharSet = customBuilder.GetFieldValue<CharSet>("CharSet") ?? CharSet.None;
			attribs &= ~TypeAttributes.LayoutMask;
			switch (attr.Value)
			{
				case LayoutKind.Auto:
					attribs |= TypeAttributes.AutoLayout;
					break;
				case LayoutKind.Explicit:
					attribs |= TypeAttributes.ExplicitLayout;
					break;
				case LayoutKind.Sequential:
					attribs |= TypeAttributes.SequentialLayout;
					break;
			}
			attribs &= ~TypeAttributes.StringFormatMask;
			switch (attr.CharSet)
			{
				case CharSet.None:
				case CharSet.Ansi:
					attribs |= TypeAttributes.AnsiClass;
					break;
				case CharSet.Auto:
					attribs |= TypeAttributes.AutoClass;
					break;
				case CharSet.Unicode:
					attribs |= TypeAttributes.UnicodeClass;
					break;
			}
			pack = (short)attr.Pack;
			size = attr.Size;
			hasLayout = pack != 0 || size != 0;
		}
		private static int WriteMarshallingDescriptor(ModuleBuilder module, CustomAttributeBuilder attribute)
		{
			UnmanagedType unmanagedType;
			object val = attribute.GetConstructorArgument(0);
			if (val is short)
			{
				unmanagedType = (UnmanagedType)(short)val;
			}
			else if (val is int)
			{
				unmanagedType = (UnmanagedType)(int)val;
			}
			else
			{
				unmanagedType = (UnmanagedType)val;
			}

			ByteBuffer bb = new ByteBuffer(5);
			bb.WriteCompressedUInt((int)unmanagedType);

			if (unmanagedType == UnmanagedType.LPArray)
			{
				UnmanagedType arraySubType = attribute.GetFieldValue<UnmanagedType>("ArraySubType") ?? NATIVE_TYPE_MAX;
				bb.WriteCompressedUInt((int)arraySubType);
				int? sizeParamIndex = attribute.GetFieldValue<short>("SizeParamIndex");
				int? sizeConst = attribute.GetFieldValue<int>("SizeConst");
				if (sizeParamIndex != null)
				{
					bb.WriteCompressedUInt(sizeParamIndex.Value);
					if (sizeConst != null)
					{
						bb.WriteCompressedUInt(sizeConst.Value);
						bb.WriteCompressedUInt(1); // flag that says that SizeParamIndex was specified
					}
				}
				else if (sizeConst != null)
				{
					bb.WriteCompressedUInt(0); // SizeParamIndex
					bb.WriteCompressedUInt(sizeConst.Value);
					bb.WriteCompressedUInt(0); // flag that says that SizeParamIndex was not specified
				}
			}
			else if (unmanagedType == UnmanagedType.SafeArray)
			{
				VarEnum? safeArraySubType = attribute.GetFieldValue<VarEnum>("SafeArraySubType");
				if (safeArraySubType != null)
				{
					bb.WriteCompressedUInt((int)safeArraySubType);
					Type safeArrayUserDefinedSubType = (Type)attribute.GetFieldValue("SafeArrayUserDefinedSubType");
					if (safeArrayUserDefinedSubType != null)
					{
						WriteType(module, bb, safeArrayUserDefinedSubType);
					}
				}
			}
			else if (unmanagedType == UnmanagedType.ByValArray)
			{
				bb.WriteCompressedUInt(attribute.GetFieldValue<int>("SizeConst") ?? 1);
				UnmanagedType? arraySubType = attribute.GetFieldValue<UnmanagedType>("ArraySubType");
				if (arraySubType != null)
				{
					bb.WriteCompressedUInt((int)arraySubType);
				}
			}
			else if (unmanagedType == UnmanagedType.ByValTStr)
			{
				bb.WriteCompressedUInt(attribute.GetFieldValue<int>("SizeConst").Value);
			}
			else if (unmanagedType == UnmanagedType.Interface
				|| unmanagedType == UnmanagedType.IDispatch
				|| unmanagedType == UnmanagedType.IUnknown)
			{
				int? iidParameterIndex = attribute.GetFieldValue<int>("IidParameterIndex");
				if (iidParameterIndex != null)
				{
					bb.WriteCompressedUInt(iidParameterIndex.Value);
				}
			}
			else if (unmanagedType == UnmanagedType.CustomMarshaler)
			{
				bb.WriteCompressedUInt(0);
				bb.WriteCompressedUInt(0);
				string marshalType = (string)attribute.GetFieldValue("MarshalType");
				if (marshalType != null)
				{
					WriteString(bb, marshalType);
				}
				else
				{
					WriteType(module, bb, (Type)attribute.GetFieldValue("MarshalTypeRef"));
				}
				WriteString(bb, (string)attribute.GetFieldValue("MarshalCookie") ?? "");
			}

			return module.Blobs.Add(bb);
		}
예제 #5
0
		private void SetMethodImplAttribute(CustomAttributeBuilder customBuilder)
		{
			MethodImplOptions opt;
			switch (customBuilder.Constructor.ParameterCount)
			{
				case 0:
					opt = 0;
					break;
				case 1:
					{
						object val = customBuilder.GetConstructorArgument(0);
						if (val is short)
						{
							opt = (MethodImplOptions)(short)val;
						}
						else if (val is int)
						{
							opt = (MethodImplOptions)(int)val;
						}
						else
						{
							opt = (MethodImplOptions)val;
						}
						break;
					}
				default:
					throw new NotSupportedException();
			}
			MethodCodeType? type = customBuilder.GetFieldValue<MethodCodeType>("MethodCodeType");
			implFlags = (MethodImplAttributes)opt;
			if (type.HasValue)
			{
				implFlags |= (MethodImplAttributes)type;
			}
		}
예제 #6
0
		private void SetDllImportPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			CallingConvention? callingConvention = customBuilder.GetFieldValue<CallingConvention>("CallingConvention");
			CharSet? charSet = customBuilder.GetFieldValue<CharSet>("CharSet");
			SetDllImportPseudoCustomAttribute((string)customBuilder.GetConstructorArgument(0),
				(string)customBuilder.GetFieldValue("EntryPoint"),
				callingConvention,
				charSet,
				(bool?)customBuilder.GetFieldValue("BestFitMapping"),
				(bool?)customBuilder.GetFieldValue("ThrowOnUnmappableChar"),
				(bool?)customBuilder.GetFieldValue("SetLastError"),
				(bool?)customBuilder.GetFieldValue("PreserveSig"),
				(bool?)customBuilder.GetFieldValue("ExactSpelling"));
		}
예제 #7
0
        private static int WriteMarshallingDescriptor(ModuleBuilder module, CustomAttributeBuilder attribute)
        {
            UnmanagedType unmanagedType;
            object        val = attribute.GetConstructorArgument(0);

            if (val is short)
            {
                unmanagedType = (UnmanagedType)(short)val;
            }
            else if (val is int)
            {
                unmanagedType = (UnmanagedType)(int)val;
            }
            else
            {
                unmanagedType = (UnmanagedType)val;
            }

            ByteBuffer bb = new ByteBuffer(5);

            bb.WriteCompressedInt((int)unmanagedType);

            if (unmanagedType == UnmanagedType.LPArray)
            {
                UnmanagedType arraySubType = attribute.GetFieldValue <UnmanagedType>("ArraySubType") ?? NATIVE_TYPE_MAX;
                bb.WriteCompressedInt((int)arraySubType);
                int?sizeParamIndex = attribute.GetFieldValue <short>("SizeParamIndex");
                int?sizeConst      = attribute.GetFieldValue <int>("SizeConst");
                if (sizeParamIndex != null)
                {
                    bb.WriteCompressedInt(sizeParamIndex.Value);
                    if (sizeConst != null)
                    {
                        bb.WriteCompressedInt(sizeConst.Value);
                        bb.WriteCompressedInt(1);                         // flag that says that SizeParamIndex was specified
                    }
                }
                else if (sizeConst != null)
                {
                    bb.WriteCompressedInt(0);                     // SizeParamIndex
                    bb.WriteCompressedInt(sizeConst.Value);
                    bb.WriteCompressedInt(0);                     // flag that says that SizeParamIndex was not specified
                }
            }
            else if (unmanagedType == UnmanagedType.SafeArray)
            {
                VarEnum?safeArraySubType = attribute.GetFieldValue <VarEnum>("SafeArraySubType");
                if (safeArraySubType != null)
                {
                    bb.WriteCompressedInt((int)safeArraySubType);
                    Type safeArrayUserDefinedSubType = (Type)attribute.GetFieldValue("SafeArrayUserDefinedSubType");
                    if (safeArrayUserDefinedSubType != null)
                    {
                        WriteType(module, bb, safeArrayUserDefinedSubType);
                    }
                }
            }
            else if (unmanagedType == UnmanagedType.ByValArray)
            {
                bb.WriteCompressedInt(attribute.GetFieldValue <int>("SizeConst") ?? 1);
                UnmanagedType?arraySubType = attribute.GetFieldValue <UnmanagedType>("ArraySubType");
                if (arraySubType != null)
                {
                    bb.WriteCompressedInt((int)arraySubType);
                }
            }
            else if (unmanagedType == UnmanagedType.ByValTStr)
            {
                bb.WriteCompressedInt(attribute.GetFieldValue <int>("SizeConst").Value);
            }
            else if (unmanagedType == UnmanagedType.Interface ||
                     unmanagedType == UnmanagedType.IDispatch ||
                     unmanagedType == UnmanagedType.IUnknown)
            {
                int?iidParameterIndex = attribute.GetFieldValue <int>("IidParameterIndex");
                if (iidParameterIndex != null)
                {
                    bb.WriteCompressedInt(iidParameterIndex.Value);
                }
            }
            else if (unmanagedType == UnmanagedType.CustomMarshaler)
            {
                bb.WriteCompressedInt(0);
                bb.WriteCompressedInt(0);
                string marshalType = (string)attribute.GetFieldValue("MarshalType");
                if (marshalType != null)
                {
                    WriteString(bb, marshalType);
                }
                else
                {
                    WriteType(module, bb, (Type)attribute.GetFieldValue("MarshalTypeRef"));
                }
                WriteString(bb, (string)attribute.GetFieldValue("MarshalCookie") ?? "");
            }

            return(module.Blobs.Add(bb));
        }
예제 #8
0
		public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			Universe u = this.Module.universe;
			if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_FieldOffsetAttribute)
			{
				customBuilder = customBuilder.DecodeBlob(this.Module.Assembly);
				SetOffset((int)customBuilder.GetConstructorArgument(0));
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
			{
				MarshalSpec.SetMarshalAsAttribute(typeBuilder.ModuleBuilder, pseudoToken, customBuilder);
				attribs |= FieldAttributes.HasFieldMarshal;
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_NonSerializedAttribute)
			{
				attribs |= FieldAttributes.NotSerialized;
			}
			else if (customBuilder.Constructor.DeclaringType == u.System_Runtime_CompilerServices_SpecialNameAttribute)
			{
				attribs |= FieldAttributes.SpecialName;
			}
			else
			{
				typeBuilder.ModuleBuilder.SetCustomAttribute(pseudoToken, customBuilder);
			}
		}
예제 #9
0
		private void SetStructLayoutPseudoCustomAttribute(CustomAttributeBuilder customBuilder)
		{
			object val = customBuilder.GetConstructorArgument(0);
			LayoutKind layout;
			if (val is short)
			{
				layout = (LayoutKind)(short)val;
			}
			else
			{
				layout = (LayoutKind)val;
			}
			int? pack = (int?)customBuilder.GetFieldValue("Pack");
			int? size = (int?)customBuilder.GetFieldValue("Size");
			if (pack.HasValue || size.HasValue)
			{
				ClassLayoutTable.Record rec = new ClassLayoutTable.Record();
				rec.PackingSize = (short)(pack ?? 0);
				rec.ClassSize = size ?? 0;
				rec.Parent = token;
				this.ModuleBuilder.ClassLayout.AddOrReplaceRecord(rec);
			}
			attribs &= ~TypeAttributes.LayoutMask;
			switch (layout)
			{
				case LayoutKind.Auto:
					attribs |= TypeAttributes.AutoLayout;
					break;
				case LayoutKind.Explicit:
					attribs |= TypeAttributes.ExplicitLayout;
					break;
				case LayoutKind.Sequential:
					attribs |= TypeAttributes.SequentialLayout;
					break;
			}
			CharSet? charSet = customBuilder.GetFieldValue<CharSet>("CharSet");
			attribs &= ~TypeAttributes.StringFormatMask;
			switch (charSet ?? CharSet.None)
			{
				case CharSet.None:
				case CharSet.Ansi:
					attribs |= TypeAttributes.AnsiClass;
					break;
				case CharSet.Auto:
					attribs |= TypeAttributes.AutoClass;
					break;
				case CharSet.Unicode:
					attribs |= TypeAttributes.UnicodeClass;
					break;
			}
		}