Esempio n. 1
0
            private MetadataAttributeValue DecodeValue(MetadataTypeReference type, object value)
            {
                if (value == null)
                {
                    return(ConstantAttributeValue.Null);
                }
                if (type is PrimitiveTypeReference)
                {
                    return(new ConstantAttributeValue(MetadataConstantValue.FromObject(value)));
                }
                if (value is MetadataTypeReference typeValue)
                {
                    return(new TypeAttributeValue(typeValue));
                }
                if (type is ArrayTypeReference arrayType)
                {
                    var items  = (ImmutableArray <CustomAttributeTypedArgument <MetadataTypeReference> >)value;
                    var values = new MetadataAttributeValue[items.Length];

                    for (var i = 0; i < values.Length; i++)
                    {
                        var item = items[i];
                        values[i] = DecodeValue(item.Type, item.Value);
                    }

                    return(new ArrayAttributeValue(arrayType, values));
                }

                // The only other thing DecodeValue can give us is an enum
                return(new EnumAttributeValue(type, MetadataConstantValue.FromObject(value)));
            }
 public ReaderParameter(MetadataReader reader, TypeReferenceTypeProvider typeProvider, Parameter definition, GenericContext genericContext, MetadataTypeReference parameterType)
 {
     this.reader         = reader;
     this.typeProvider   = typeProvider;
     this.definition     = definition;
     this.genericContext = genericContext;
     ParameterType       = parameterType;
 }
 public bool TryGetIsDelegateType(MetadataTypeReference typeReference, out bool isDelegateType)
 {
     if (TryGetCachedInfo(typeReference, out var cachedInfo))
     {
         isDelegateType = cachedInfo.IsDelegateType;
         return(true);
     }
     isDelegateType = default;
     return(false);
 }
 public bool TryGetEnumInfo(MetadataTypeReference typeReference, out EnumInfo info)
 {
     if (TryGetCachedInfo(typeReference, out var cachedInfo) && cachedInfo.EnumInfo != null)
     {
         info = cachedInfo.EnumInfo.Value;
         return(true);
     }
     info = default;
     return(false);
 }
            private bool ParameterIsNamedType(MetadataTypeReference type)
            {
                switch (type)
                {
                case TopLevelTypeReference _:
                case NestedTypeReference _:
                    return(true);

                case GenericInstantiationTypeReference instantiation:
                    return(ParameterIsNamedType(instantiation.TypeDefinition));

                default:
                    return(false);
                }
            }
        private bool TryGetCachedInfo(MetadataTypeReference typeReference, out CachedInfo cachedInfo)
        {
            var(assemblyReference, typeName) = NameSpec.FromMetadataTypeReference(typeReference);

            if (assemblyReference == null)
            {
                if (currentAssemblyLoader == null)
                {
                    currentAssemblyLoader = new AssemblyLazyLoader(currentAssemblyStreamFactory.Invoke());
                }

                if (currentAssemblyLoader.TryGetInfo(typeName, this, out cachedInfo))
                {
                    return(true);
                }

                // Attribute values containing enum type references serialize the string.
                // So far all mscorlib enum references I've seen include the assembly name,
                // but this is just in case.

                if (currentAssemblyMscorlibReference == null)
                {
                    currentAssemblyMscorlibReference = GetMscorlibReference(currentAssemblyStreamFactory.Invoke());
                }

                assemblyReference = currentAssemblyMscorlibReference;
            }

            var fullName = assemblyReference.FullName;

            if (!assemblyLoadersByFullName.TryGetValue(fullName, out var loader))
            {
                loader = assemblyResolver.TryGetAssemblyPath(assemblyReference, out var path) ? new AssemblyLazyLoader(File.OpenRead(path)) : null;
                assemblyLoadersByFullName.Add(fullName, loader);
            }

            if (loader == null)
            {
                // We couldn't locate the assembly.
                cachedInfo = default;
                return(false);
            }

            return(loader.TryGetInfo(typeName, this, out cachedInfo));
        }
Esempio n. 7
0
 public ManualMetadataAttribute(MetadataTypeReference attributeType)
 {
     AttributeType = attributeType ?? throw new ArgumentNullException(nameof(attributeType));
 }
            private void VisitTypeReference(TextNode referencePath, MetadataTypeReference type)
            {
                for (;;)
                {
                    switch (type)
                    {
                    case TopLevelTypeReference topLevel:
                        VisitTypeName(referencePath, topLevel.Namespace, topLevel.Name, Array.Empty <string>());
                        return;

                    case NestedTypeReference nested:
                        for (var nestedNames = new List <string>(); ;)
                        {
                            nestedNames.Add(nested.Name);
                            if (nested.DeclaringType is NestedTypeReference next)
                            {
                                nested = next;
                            }
                            else
                            {
                                var topLevel = (TopLevelTypeReference)nested.DeclaringType;
                                nestedNames.Reverse();
                                VisitTypeName(referencePath, topLevel.Namespace, topLevel.Name, nestedNames);
                                return;
                            }
                        }

                    case GenericInstantiationTypeReference genericInstantiation:
                        foreach (var argument in genericInstantiation.GenericTypeArguments)
                        {
                            VisitTypeReference(referencePath, argument);
                        }
                        type = genericInstantiation.TypeDefinition;
                        continue;

                    case ArrayTypeReference array:
                        type = array.ElementType;
                        continue;

                    case ByRefTypeReference byref:
                        type = byref.ElementType;
                        continue;

                    case PointerTypeReference pointer:
                        type = pointer.ElementType;
                        continue;

                    case GenericParameterTypeReference genericParameter:
                        foreach (var constraint in genericParameter.TypeParameter.TypeConstraints)
                        {
                            VisitTypeReference(referencePath + " generic constraint", constraint);
                        }
                        return;

                    case PrimitiveTypeReference _:
                        return;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
 public ByRefTypeReference(MetadataTypeReference elementType)
 {
     ElementType = elementType;
 }
Esempio n. 10
0
 public NestedTypeReference(MetadataTypeReference declaringType, string name)
 {
     DeclaringType = declaringType;
     Name          = name;
 }
Esempio n. 11
0
 public TypeAttributeValue(MetadataTypeReference value)
 {
     Value = value ?? throw new ArgumentNullException(nameof(value));
 }
            public static (MetadataAssemblyReference assemblyReference, NameSpec typeName) FromMetadataTypeReference(MetadataTypeReference typeReference)
            {
                if (typeReference is GenericInstantiationTypeReference genericInstantiation)
                {
                    typeReference = genericInstantiation.TypeDefinition;
                }

                var nestedNames = (List <string>)null;

                for (; typeReference is NestedTypeReference nested; typeReference = nested.DeclaringType)
                {
                    if (nestedNames == null)
                    {
                        nestedNames = new List <string>();
                    }
                    nestedNames.Add(nested.Name);
                }
                nestedNames?.Reverse();

                if (!(typeReference is TopLevelTypeReference topLevel))
                {
                    throw new ArgumentException($"Type reference must either be a {nameof(TopLevelTypeReference)} or a {nameof(NestedTypeReference)}.", nameof(typeReference));
                }

                return(
                    topLevel.Assembly,
                    new NameSpec(
                        topLevel.Namespace,
                        topLevel.Name,
                        nestedNames?.ToArray()));
            }
 public PointerTypeReference(MetadataTypeReference elementType)
 {
     ElementType = elementType;
 }
 public ArrayTypeReference(MetadataTypeReference elementType, int rank)
 {
     ElementType = elementType;
     Rank        = rank;
 }
 public EnumAttributeValue(MetadataTypeReference enumType, IMetadataConstantValue underlyingValue)
 {
     EnumType        = enumType ?? throw new ArgumentNullException(nameof(enumType));
     UnderlyingValue = underlyingValue ?? throw new ArgumentNullException(nameof(underlyingValue));
 }