Пример #1
0
        protected sealed override Guid?ComputeGuidFromCustomAttributes()
        {
            //
            // Look for a [Guid] attribute. If found, return that.
            //
            foreach (CustomAttributeHandle cah in _typeDefinition.CustomAttributes)
            {
                // We can't reference the GuidAttribute class directly as we don't have an official dependency on System.Runtime.InteropServices.
                // Following age-old CLR tradition, we search for the custom attribute using a name-based search. Since this makes it harder
                // to be sure we won't run into custom attribute constructors that comply with the GuidAttribute(String) signature,
                // we'll check that it does and silently skip the CA if it doesn't match the expected pattern.
                if (cah.IsCustomAttributeOfType(_reader, "System.Runtime.InteropServices", "GuidAttribute"))
                {
                    CustomAttribute ca = cah.GetCustomAttribute(_reader);
                    FixedArgumentHandleCollection.Enumerator fahEnumerator = ca.FixedArguments.GetEnumerator();
                    if (!fahEnumerator.MoveNext())
                    {
                        continue;
                    }
                    FixedArgumentHandle guidStringArgumentHandle = fahEnumerator.Current;
                    if (fahEnumerator.MoveNext())
                    {
                        continue;
                    }
                    FixedArgument guidStringArgument = guidStringArgumentHandle.GetFixedArgument(_reader);
                    if (!(guidStringArgument.Value.ParseConstantValue(_reader) is string guidString))
                    {
                        continue;
                    }
                    return(new Guid(guidString));
                }
            }

            return(null);
        }
Пример #2
0
        // Equals/GetHashCode no need to override (they just implement reference equality but desktop never unified these things.)

        //
        // Helper for parsing custom attribute arguments.
        //
        // If throwIfMissingMetadata is false, returns default(CustomAttributeTypedArgument) rather than throwing a MissingMetadataException.
        //
        private CustomAttributeTypedArgument ParseFixedArgument(MetadataReader reader, FixedArgumentHandle fixedArgumentHandle, bool throwIfMissingMetadata, Func <RuntimeTypeInfo> getTypeFromConstructor)
        {
            FixedArgument   fixedArgument = fixedArgumentHandle.GetFixedArgument(reader);
            RuntimeTypeInfo argumentType  = null;

            if (fixedArgument.Type.IsNull(reader))
            {
                argumentType = getTypeFromConstructor();
                if (argumentType == null)
                {
                    Debug.Assert(!throwIfMissingMetadata);
                    return(default(CustomAttributeTypedArgument));
                }
            }
            else
            {
                Exception exception = null;
                argumentType = fixedArgument.Type.TryResolve(reader, new TypeContext(null, null), ref exception);
                if (argumentType == null)
                {
                    if (throwIfMissingMetadata)
                    {
                        throw exception;
                    }
                    else
                    {
                        return(default(CustomAttributeTypedArgument));
                    }
                }
            }

            Object    value;
            Exception e = fixedArgument.Value.TryParseConstantValue(reader, out value);

            if (e != null)
            {
                if (throwIfMissingMetadata)
                {
                    throw e;
                }
                else
                {
                    return(default(CustomAttributeTypedArgument));
                }
            }
            return(WrapInCustomAttributeTypedArgument(value, argumentType));
        }
        // Equals/GetHashCode no need to override (they just implement reference equality but desktop never unified these things.)

        //
        // Helper for parsing custom attribute arguments.
        //
        // If throwIfMissingMetadata is false, returns default(CustomAttributeTypedArgument) rather than throwing a MissingMetadataException.
        //
        private CustomAttributeTypedArgument ParseFixedArgument(MetadataReader reader, FixedArgumentHandle fixedArgumentHandle, bool throwIfMissingMetadata, Func<RuntimeType> getTypeFromConstructor)
        {
            FixedArgument fixedArgument = fixedArgumentHandle.GetFixedArgument(reader);
            RuntimeType argumentType = null;
            if (fixedArgument.Type.IsNull(reader))
            {
                argumentType = getTypeFromConstructor();
                if (argumentType == null)
                {
                    Debug.Assert(!throwIfMissingMetadata);
                    return default(CustomAttributeTypedArgument);
                }
            }
            else
            {
                Exception exception = null;
                argumentType = _reflectionDomain.TryResolve(reader, fixedArgument.Type, new TypeContext(null, null), ref exception);
                if (argumentType == null)
                {
                    if (throwIfMissingMetadata)
                        throw exception;
                    else
                        return default(CustomAttributeTypedArgument);
                }
            }

            Object value;
            Exception e = fixedArgument.Value.TryParseConstantValue(_reflectionDomain, reader, out value);
            if (e != null)
            {
                if (throwIfMissingMetadata)
                    throw e;
                else
                    return default(CustomAttributeTypedArgument);
            }
            return WrapInCustomAttributeTypedArgument(value, argumentType);
        }