private AttributeInfo[] GetAssemblyAttributes()
        {
            return(_metadataReader.CustomAttributes
                   .Select(_metadataReader.GetCustomAttribute)
                   .Where(x => x.Constructor.Kind == HandleKind.MemberReference && x.Parent.Kind == HandleKind.AssemblyDefinition)
                   .Select(customAttribute =>
            {
                var constructorRef = _metadataReader.GetMemberReference((MemberReferenceHandle)customAttribute.Constructor);
                var attributeTypeRefHandle = (TypeReferenceHandle)constructorRef.Parent;
                var typeProvider = new AttributeTypeProvider();
                var attributeTypeName = typeProvider.GetTypeFromReference(_metadataReader, attributeTypeRefHandle, 0);

                try
                {
                    var customAttributeValues = customAttribute.DecodeValue(typeProvider);

                    return (attributeTypeName, customAttributeValues);
                }
                catch (UnknownTypeException)
                {
                    return default((string Name, CustomAttributeValue <string> Args)?);
                }
            })
                   .Where(x => x.HasValue && x.Value.Name.StartsWith("System.Reflection.Assembly"))
                   .Select(x => new AttributeInfo(
                               Regex.Replace(x.Value.Name, @"^System\.Reflection\.Assembly(\w+)Attribute", "$1"),
                               x.Value.Args.FixedArguments.FirstOrDefault().Value.ToString()
                               ))
                   .ToArray());
        }
Ejemplo n.º 2
0
        public AoGISFields(IFields fields)
        {
            if (fields == null)
            {
                throw new ArgumentNullException("fields");
            }

            int length = fields.FieldCount;

            for (int i = 0; i < length; i++)
            {
                IField field = fields.get_Field(i);

                if (field.Type == esriFieldType.esriFieldTypeGeometry)
                {
                    _geometryType = GeometryTypeProvider.GetGeometryType(field.GeometryDef.GeometryType);
                }
                else
                {
                    DataColumn col = AttributeTypeProvider.GetAttributeType(field as IField2);
                    if (col != null)
                    {
                        _attributeFields.Add(col);
                    }
                }
            }

            if (_geometryType == null)
            {
                _attributeFields = null;
                throw new ArgumentException("The geometry field is missing or the geometry type is not supported.", "fields");
            }
        }
        public IEnumerable <AttributeInfo> GetAssemblyAttributes()
        {
            foreach (var attributeHandle in _metadataReader.CustomAttributes)
            {
                var customAttribute = _metadataReader.GetCustomAttribute(attributeHandle);
                if (customAttribute.Constructor.Kind != HandleKind.MemberReference ||
                    customAttribute.Parent.Kind != HandleKind.AssemblyDefinition)
                {
                    continue;
                }

                var constructorRef         = _metadataReader.GetMemberReference((MemberReferenceHandle)customAttribute.Constructor);
                var attributeTypeRefHandle = (TypeReferenceHandle)constructorRef.Parent;

                var typeProvider = new AttributeTypeProvider();

                var attributeTypeName = typeProvider.GetTypeFromReference(_metadataReader, attributeTypeRefHandle, 0);

                AttributeInfo?attrInfo = null;
                try
                {
                    var customAttributeValues = customAttribute.DecodeValue(typeProvider);

                    attrInfo = new AttributeInfo(
                        attributeTypeName,
                        customAttributeValues.FixedArguments.ToArray(),
                        customAttributeValues.NamedArguments.ToArray());
                }
                catch (UnknownTypeException)
                {
                    // It could happen that we cannot resolve some argument type (e.g. for custom attributes).
                    // In this case simply omit the attribute and try to read other ones.
                }

                if (attrInfo != null)
                {
                    yield return(attrInfo);
                }
            }
        }