/// <summary>
        /// Populates the type with additional information which can't be loaded when the type is created (due to potential circularities in item dependencies).
        /// </summary>
        /// <param name="underlyingType">The underlying type this instance represents.</param>
#endif
        internal void FinalizeDefinition(GenericParameter underlyingType)
        {
            foreach (var type in underlyingType.Constraints)
            {
                this.Constraints.Add(TypeData.FromType(type));
            }
        }
 internal TypedMemberDataBase(MemberReference member, MemberAccessibility accessibility, TypeReference type, bool isTypeDynamic, MemberFlags flags, DeclaringTypeData declaringType)
     : base(member, accessibility, flags, declaringType)
 {
     this.Type          = TypeData.FromType(type);
     this.IsTypeDynamic = isTypeDynamic;
     Debug.Assert(this.Type != null, "Unable to get the TypeData.");
 }
Example #3
0
        internal ParameterData(ParameterDefinition underlyingParameter, MemberDataBase declaringMember)
        {
            var modifer         = ParameterModifier.None;
            var parameterType   = underlyingParameter.ParameterType;
            var byReferenceType = parameterType as ByReferenceType;

            if (byReferenceType != null)
            {
                modifer       = underlyingParameter.IsOut ? ParameterModifier.Out : ParameterModifier.Ref;
                parameterType = byReferenceType.ElementType;
            }

            this.DeclaringMemberKind = declaringMember.MetadataItemKind;
            this.Modifer             = modifer;
            this.Name = underlyingParameter.Name;
            this.Type = TypeData.FromType(parameterType);

            if (underlyingParameter.IsOptional)
            {
                _flags           |= InternalFlags.IsOptional;
                this.DefaultValue = Utilities.PreprocessConstantValue(parameterType, underlyingParameter.GetDefualtValue());
            }

            if (underlyingParameter.CustomAttributes.Any(c => c.AttributeType.EqualsType(typeof(ParamArrayAttribute))))
            {
                _flags |= InternalFlags.IsParamsArray;
            }

            if (underlyingParameter.IsDynamicType())
            {
                _flags |= InternalFlags.IsTypeDynamic;
            }
        }
Example #4
0
        /// <summary>
        /// Gets the <see cref="TypeDefinitionData"/> instance representing the specified type.
        /// </summary>
        /// <returns>The <see cref="TypeDefinitionData"/> instance.</returns>
        public new static TypeDefinitionData FromType(Type t)
        {
            var typeDefinition = TypeData.FromType(t) as TypeDefinitionData;

            if (typeDefinition == null)
            {
                throw new ArgumentException("The specified type is not an externally visible type definition.", "t");
            }

            return(typeDefinition);
        }
Example #5
0
        /// <summary>
        /// Populates the type with additional information which can't be loaded when the type is created (due to potential circularities in item dependencies).
        /// </summary>
        /// <param name="underlyingType">The underlying type this instance represents.</param>
#endif
        internal void FinalizeDefinition(TypeDefinition underlyingType)
        {
            if (underlyingType.IsEnum)
            {
                this.BaseType = (DeclaringTypeData)TypeData.FromType(underlyingType.GetEnumUnderlyingType());
            }
            else if (underlyingType.BaseType != null)
            {
                this.BaseType = (DeclaringTypeData)TypeData.FromType(underlyingType.BaseType);
            }

            this.ImplementedInterfaces = new ImplementedInterfacesCollection(
                underlyingType.Interfaces.Select(i => (DeclaringTypeData)TypeData.FromType(i)).Where(i => i != null)
                );

            if (this.TypeKind == TypeKind.Delegate)
            {
                var invokeMethod = underlyingType.Methods.Single(m => m.Name == "Invoke");
                this.DelegateParameters          = new ParameterCollection(invokeMethod.Parameters, this);
                this.DelegateReturnType          = TypeData.FromType(invokeMethod.ReturnType);
                this.DelegateReturnTypeIsDynamic = invokeMethod.IsReturnTypeDynamic();
            }

            if (underlyingType.HasGenericParameters)
            {
                this.GenericParameters = Utilities.GetGenericParameters(underlyingType.GenericParameters, this);
                Debug.Assert(
                    _constructedGenericTypes == null || _constructedGenericTypes.Values.All(c => c.GenericArguments.Count == this.GenericParameters.Count),
                    "The type arity does not match.");
            }
            else
            {
                this.GenericParameters = GenericTypeParameterData.EmptyList;
                Debug.Assert(_constructedGenericTypes == null, "There should be no constructed generic types.");
            }

            this.NameForComparison = this.GetDisplayName(fullyQualify: true, includeGenericInfo: false);

            Debug.Assert(_constructedGenericTypes == null || _constructedGenericTypes.Keys.All(t => t.Count == this.GenericParameters.Count), "A constructed generic has the wrong type arity.");
        }
Example #6
0
 /// <summary>
 /// Gets the <see cref="TypeDefinitionData"/> instance representing the specified type.
 /// </summary>
 /// <returns>The <see cref="TypeDefinitionData"/> instance.</returns>
 public new static TypeDefinitionData FromType(TypeReference t)
 {
     return((TypeDefinitionData)TypeData.FromType(t));
 }
Example #7
0
        private void LoadFromMetadata(AssemblyDefinition assembly)
        {
            _isLoading = true;

            foreach (var module in assembly.Modules)
            {
                foreach (var type in module.Types)
                {
                    this.RegisterType(type, null);
                }
            }

            // Finalize Definitions
            this.IterateAllTypeDefinitions(assembly,
                                           typeDefinitionAction: type =>
            {
                var typeData = (TypeDefinitionData)this.GetTypeData(type);
                if (typeData == null)
                {
                    return(false);
                }

                typeData.FinalizeDefinition(type);
                return(true);
            },
                                           genericParameterAction: genericParameter =>
            {
                this.GetGenericTypeParameterData(genericParameter).FinalizeDefinition(genericParameter);
            });

            // Populate Members
            this.IterateAllTypeDefinitions(assembly,
                                           typeDefinitionAction: type =>
            {
                var typeData = (TypeDefinitionData)this.GetTypeData(type);
                if (typeData == null)
                {
                    return(false);
                }

                typeData.PopulateMembers(type);
                return(true);
            });

            while (_constructedGenericsToFinalizeAfterLoad.Count != 0)
            {
                var temp = _constructedGenericsToFinalizeAfterLoad.ToArray();
                _constructedGenericsToFinalizeAfterLoad.Clear();
                foreach (var type in temp)
                {
                    type.FinalizeDefiniton();
                }
            }

            foreach (var type in assembly.MainModule.ExportedTypes.Where(e => e.IsForwarder).Select(t => (TypeDefinitionData)TypeData.FromType(t.Resolve())).Where(t => t != null))
            {
                this.AddForwardedType(type);
            }

            foreach (var referenceName in assembly.Modules.SelectMany(m => m.AssemblyReferences))
            {
                AssemblyData reference;
                if (_cachedAssemblyDatas.TryGetValue(referenceName.FullName, out reference))
                {
                    _referencedAssemblies.Add(reference);
                }
            }

            _isLoading = false;
        }
Example #8
0
        /// <summary>
        /// Gets the <see cref="TypeData"/> instance containing the metadata for externally visible types and members of the specified <see cref="Type"/>.
        /// </summary>
        /// <param name="type">The type for which of corresponding to the TypeData to get.</param>
        /// <returns>The TypeData instance containing the metadata for externally visible types and members of the specified Type.</returns>
#endif
        internal TypeData GetTypeData(TypeReference type)
        {
            Debug.Assert(type.GetType() != typeof(TypeReference), "The type has not been resolved yet, and may be from a different assembly.");
            Debug.Assert(type.GetDeclaringAssembly().FullName == this.FullName, "The type belongs to another assembly.");

            var genericParameter = type as GenericParameter;

            if (genericParameter != null)
            {
                return(this.GetGenericTypeParameterData(genericParameter));
            }

            var accessibility = type.GetAccessibility();

            if (accessibility == null)
            {
                return(null);
            }

            var typeDefinition = type as TypeDefinition;

            if (typeDefinition != null)
            {
                return(this.GetTypeDefinitionData(typeDefinition.FullName));
            }

            DeclaringTypeData declaringType = null;

            if (type.DeclaringType != null)
            {
                declaringType = (DeclaringTypeData)this.GetTypeData(type.DeclaringType.Resolve());
            }

            var genericInstance = type as GenericInstanceType;

            if (genericInstance != null)
            {
                return(TypeDefinitionData.FromType(genericInstance.ElementType).GetConstructedGenericTypeData(genericInstance.GenericArguments.Select(a => TypeData.FromType(a))));
            }

            if (type.IsByReference)
            {
                Debug.Fail("We should never create representations for by-ref types. They can only be parameter types and ref or out parameters store the element type and indicate whether they are ref or out.");
                return(null);
            }

            var arrayType = type as ArrayType;

            if (arrayType != null)
            {
                Debug.Assert(declaringType == null, "Types with elements should not be declared within other types.");
                return(TypeData.FromType(arrayType.ElementType).GetArrayType((byte)arrayType.Rank));
            }

            var pointerType = type as PointerType;

            if (pointerType != null)
            {
                return(TypeData.FromType(pointerType.ElementType).GetPointerType());
            }

            var modifierType = type as IModifierType;

            if (modifierType != null)
            {
                return(TypeData.FromType(modifierType.ElementType));
            }

            Debug.Fail("Unknown kind of type.");
            return(TypeData.FromType(type.Resolve()));
        }
 /// <summary>
 /// Gets the derived <see cref="TypeData"/> instance representing the specified type.
 /// </summary>
 /// <param name="t">The type for which to get the <see cref="TypeData"/>.</param>
 /// <returns>The derived <see cref="TypeData"/> instance.</returns>
 public static TypeData FromType(Type t)
 {
     return(TypeData.FromType(
                t.Assembly.ToAssemblyDefinition().MainModule.GetType(t.FullName, runtimeName: true)));
 }
 /// <summary>
 /// Gets the derived <see cref="TypeData"/> instance representing the specified type.
 /// </summary>
 /// <typeparam name="T">The type for which to get the <see cref="TypeData"/>.</typeparam>
 /// <returns>The derived <see cref="TypeData"/> instance.</returns>
 public static TypeData FromType <T>()
 {
     return(TypeData.FromType(typeof(T)));
 }
Example #11
0
 /// <summary>
 /// Gets the derived <see cref="ConstructedGenericTypeData"/> instance representing the specified type.
 /// </summary>
 /// <returns>The derived <see cref="ConstructedGenericTypeData"/> instance.</returns>
 public new static ConstructedGenericTypeData FromType(Type t)
 {
     // TODO_Public: throw an exception if the type is not a constructed generic
     return((ConstructedGenericTypeData)TypeData.FromType(t));
 }