internal static Attribute GetCustomAttribute(RuntimeType type)
        {
            if (!StructLayoutAttribute.IsDefined(type))
            {
                return(null);
            }
            int            num            = 0;
            int            size           = 0;
            LayoutKind     layoutKind     = LayoutKind.Auto;
            TypeAttributes typeAttributes = type.Attributes & TypeAttributes.LayoutMask;

            if (typeAttributes != TypeAttributes.NotPublic)
            {
                if (typeAttributes != TypeAttributes.SequentialLayout)
                {
                    if (typeAttributes == TypeAttributes.ExplicitLayout)
                    {
                        layoutKind = LayoutKind.Explicit;
                    }
                }
                else
                {
                    layoutKind = LayoutKind.Sequential;
                }
            }
            else
            {
                layoutKind = LayoutKind.Auto;
            }
            CharSet charSet = CharSet.None;

            typeAttributes = (type.Attributes & TypeAttributes.StringFormatMask);
            if (typeAttributes != TypeAttributes.NotPublic)
            {
                if (typeAttributes != TypeAttributes.UnicodeClass)
                {
                    if (typeAttributes == TypeAttributes.AutoClass)
                    {
                        charSet = CharSet.Auto;
                    }
                }
                else
                {
                    charSet = CharSet.Unicode;
                }
            }
            else
            {
                charSet = CharSet.Ansi;
            }
            type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out num, out size);
            if (num == 0)
            {
                num = 8;
            }
            return(new StructLayoutAttribute(layoutKind, num, size, charSet));
        }
Esempio n. 2
0
        internal static Attribute GetCustomAttribute(RuntimeType type)
        {
            if (!StructLayoutAttribute.IsDefined(type))
            {
                return((Attribute)null);
            }
            int        packSize   = 0;
            int        classSize  = 0;
            LayoutKind layoutKind = LayoutKind.Auto;

            switch (type.Attributes & TypeAttributes.LayoutMask)
            {
            case TypeAttributes.NotPublic:
                layoutKind = LayoutKind.Auto;
                break;

            case TypeAttributes.SequentialLayout:
                layoutKind = LayoutKind.Sequential;
                break;

            case TypeAttributes.ExplicitLayout:
                layoutKind = LayoutKind.Explicit;
                break;
            }
            CharSet charSet = CharSet.None;

            switch (type.Attributes & TypeAttributes.StringFormatMask)
            {
            case TypeAttributes.NotPublic:
                charSet = CharSet.Ansi;
                break;

            case TypeAttributes.UnicodeClass:
                charSet = CharSet.Unicode;
                break;

            case TypeAttributes.AutoClass:
                charSet = CharSet.Auto;
                break;
            }
            type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out packSize, out classSize);
            if (packSize == 0)
            {
                packSize = 8;
            }
            return((Attribute) new StructLayoutAttribute(layoutKind, packSize, classSize, charSet));
        }
Esempio n. 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;
		}
		internal StructLayoutAttribute GetStructLayoutAttribute ()
		{
			LayoutKind kind;

			if (IsLayoutSequential)
				kind = LayoutKind.Sequential;
			else if (IsExplicitLayout)
				kind = LayoutKind.Explicit;
			else
				kind = LayoutKind.Auto;

			StructLayoutAttribute attr = new StructLayoutAttribute (kind);

			if (IsUnicodeClass)
				attr.CharSet = CharSet.Unicode;
			else if (IsAnsiClass)
				attr.CharSet = CharSet.Ansi;
			else
				attr.CharSet = CharSet.Auto;

			if (kind != LayoutKind.Auto)
				GetPacking (out attr.Pack, out attr.Size);

			return attr;
		}
Esempio n. 5
0
		internal StructLayoutAttribute GetStructLayoutAttribute ()
		{
			LayoutKind kind;

			if (IsLayoutSequential)
				kind = LayoutKind.Sequential;
			else if (IsExplicitLayout)
				kind = LayoutKind.Explicit;
			else
				kind = LayoutKind.Auto;

			StructLayoutAttribute attr = new StructLayoutAttribute (kind);

			if (IsUnicodeClass)
				attr.CharSet = CharSet.Unicode;
			else if (IsAnsiClass)
				attr.CharSet = CharSet.Ansi;
			else
				attr.CharSet = CharSet.Auto;

			if (kind != LayoutKind.Auto) {
				int packing;
				GetPacking (out packing, out attr.Size);
				// 0 means no data provided, we end up with default value
				if (packing != 0)
					attr.Pack = packing;
			}

			return attr;
		}
 public static void ValidateAttribute(StructLayoutAttribute atrib, LayoutKind kind, CharSet charset, int pack)
 {
     Assert.Equal(atrib.Value, kind);
     Assert.Equal(atrib.CharSet, charset);
     Assert.Equal(atrib.Pack, pack);
 }