CSharpSourceEmitter.Utils.GetAttributeType C# (CSharp) Метод

GetAttributeType() публичный статический Метод

Determine if the specified attribute is of a special type, and if so return a code representing it. This is usefull for types not alread in IPlatformType
public static GetAttributeType ( ICustomAttribute attr ) : SpecialAttribute
attr ICustomAttribute
Результат SpecialAttribute
    public static SpecialAttribute GetAttributeType(ICustomAttribute attr) {
      // This seems like a big hack
      // Perhaps I should add this as a PlatformType and use AttributeHelper.Contains instead?
      // There's got to be a cleaner way to do this?

      var type = attr.Type;
      var typeName = TypeHelper.GetTypeName(type);
      var attrUnit = TypeHelper.GetDefiningUnitReference(type);
      var mscorlibUnit = TypeHelper.GetDefiningUnitReference(type.PlatformType.SystemObject);

      // mscorlib
      if (UnitHelper.UnitsAreEquivalent(attrUnit, mscorlibUnit) || (attrUnit != null && mscorlibUnit != null && attrUnit.ResolvedUnit == mscorlibUnit.ResolvedUnit)) {
        switch (typeName) {
          case "System.FlagsAttribute": return SpecialAttribute.Flags;
          case "System.Runtime.CompilerServices.FixedBufferAttribute": return SpecialAttribute.FixedBuffer;
          case "System.ParamArrayAttribute": return SpecialAttribute.ParamArray;
          case "System.Reflection.DefaultMemberAttribute": return SpecialAttribute.DefaultMemberAttribute;
          case "System.Reflection.AssemblyKeyFileAttribute": return SpecialAttribute.AssemblyKeyFile;
          case "System.Reflection.AssemblyDelaySignAttribute": return SpecialAttribute.AssemblyDelaySign;
        }
      } else if (attrUnit != null && attrUnit.Name.Value == "System.Core") {
        switch (typeName) {
          case "System.Runtime.CompilerServices.ExtensionAttribute": return SpecialAttribute.Extension;
        }
      }
      return SpecialAttribute.None;
    }

Usage Example

Пример #1
0
        public virtual void PrintTypeDefinitionAttributes(ITypeDefinition typeDefinition)
        {
            Contract.Requires(typeDefinition != null);

            foreach (var attribute in SortAttributes(typeDefinition.Attributes))
            {
                // Skip DefaultMemberAttribute on a class that has an indexer
                var at = Utils.GetAttributeType(attribute);
                if (at == SpecialAttribute.DefaultMemberAttribute &&
                    IteratorHelper.Any(typeDefinition.Properties, p => IteratorHelper.EnumerableIsNotEmpty(p.Parameters)))
                {
                    continue;
                }
                // Skip ExtensionAttribute
                if (at == SpecialAttribute.Extension)
                {
                    continue;
                }

                PrintAttribute(typeDefinition, attribute, true, null);
            }

            if (typeDefinition.Layout != LayoutKind.Auto)
            {
                PrintPseudoCustomAttribute(typeDefinition,
                                           "System.Runtime.InteropServices.StructLayout",
                                           String.Format("System.Runtime.InteropServices.LayoutKind.{0}", typeDefinition.Layout.ToString()),
                                           true, null);
            }
        }
All Usage Examples Of CSharpSourceEmitter.Utils::GetAttributeType