示例#1
0
        void AddAttributes(IKVM.Reflection.Type typeDefinition, IUnresolvedTypeDefinition targetEntity)
        {
            // SerializableAttribute
            if (typeDefinition.IsSerializable)
                targetEntity.Attributes.Add(serializableAttribute);

            // ComImportAttribute
            if (typeDefinition.IsImport)
                targetEntity.Attributes.Add(comImportAttribute);

            #region StructLayoutAttribute
            LayoutKind layoutKind = LayoutKind.Auto;
            switch (typeDefinition.Attributes & TypeAttributes.LayoutMask) {
                case TypeAttributes.SequentialLayout:
                layoutKind = LayoutKind.Sequential;
                break;
                case TypeAttributes.ExplicitLayout:
                layoutKind = LayoutKind.Explicit;
                break;
            }
            CharSet charSet = CharSet.None;
            switch (typeDefinition.Attributes & TypeAttributes.StringFormatMask) {
                case TypeAttributes.AnsiClass:
                charSet = CharSet.Ansi;
                break;
                case TypeAttributes.AutoClass:
                charSet = CharSet.Auto;
                break;
                case TypeAttributes.UnicodeClass:
                charSet = CharSet.Unicode;
                break;
            }

            int packingSize;
            int typeSize;
            if (typeDefinition.__GetLayout (out packingSize, out typeSize)) {
                LayoutKind defaultLayoutKind = (typeDefinition.IsValueType && !typeDefinition.IsEnum) ? LayoutKind.Sequential: LayoutKind.Auto;
                if (layoutKind != defaultLayoutKind || charSet != CharSet.Ansi || packingSize > 0 || typeSize > 0) {
                    var structLayout = new DefaultUnresolvedAttribute(structLayoutAttributeTypeRef, new[] { layoutKindTypeRef });
                    structLayout.PositionalArguments.Add(CreateSimpleConstantValue(layoutKindTypeRef, (int)layoutKind));
                    if (charSet != CharSet.Ansi) {
                        structLayout.AddNamedFieldArgument("CharSet", CreateSimpleConstantValue(charSetTypeRef, (int)charSet));
                    }
                    if (packingSize > 0) {
                        structLayout.AddNamedFieldArgument("Pack", CreateSimpleConstantValue(KnownTypeReference.Int32, packingSize));
                    }
                    if (typeSize > 0) {
                        structLayout.AddNamedFieldArgument("Size", CreateSimpleConstantValue(KnownTypeReference.Int32, typeSize));
                    }
                    targetEntity.Attributes.Add(interningProvider.Intern(structLayout));
                }
            }
            #endregion

            AddCustomAttributes(typeDefinition.CustomAttributes, targetEntity.Attributes);

            if (typeDefinition.Attributes.HasFlag (TypeAttributes.HasSecurity)) {
                AddSecurityAttributes(CustomAttributeData.__GetDeclarativeSecurity(typeDefinition), targetEntity.Attributes);
            }
        }