Exemplo n.º 1
0
        internal static MrProperty TryGetProperty(
            MrType declaringType,
            PropertyDefinitionHandle propertyDefinitionHandle,
            bool publicishOnly)
        {
            var propertyDefinition = declaringType.Assembly.Reader.GetPropertyDefinition(propertyDefinitionHandle);

            var propertyAccessors = propertyDefinition.GetAccessors();
            var mrGetter          = TryGetEtter(
                declaringType,
                propertyAccessors.Getter,
                publicishOnly);

            var mrSetter = TryGetEtter(
                declaringType,
                propertyAccessors.Setter,
                publicishOnly);

            if (mrGetter != null || mrSetter != null)
            {
                return(new MrProperty(declaringType, propertyDefinitionHandle, propertyDefinition)
                {
                    Getter = mrGetter,
                    Setter = mrSetter
                });
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The type of the parameter, which might be a generic type parameter (e.g. the T in List of T)
        /// </summary>
        /// <returns></returns>
        public MrType GetParameterType()
        {
            var parameterType = Method.MethodSignature.ParameterTypes[_parameterIndex];

            if (parameterType.IsGenericParameter)
            {
                // E.g. this parameterType is the item's T for List<T>.Add(T item)
                // We want to get the method's type argument that correponds to this type parameter

                // Get the parameter type's unmodified name, as a string.
                // E.g. for the baz in Foo<T>.Bar(T& baz), return "T"
                var parameterTypeName = MrType.GetUnmodifiedTypeName(
                    parameterType.GetName(),
                    out var isArray, out var isReference, out var isPointer);

                // Find the method type argument with the same name as this parameter type.
                var typeArguments = this.Method.DeclaringType.GetGenericArguments();
                foreach (var typeArgument in typeArguments)
                {
                    if (typeArgument.TypeParameterName == parameterTypeName)
                    {
                        // Return the type argument, correctly modified ("T&" rather than "T")
                        parameterType = MrType.Clone(typeArgument, isArray, isReference, isPointer);
                        break;
                    }
                }
            }

            return(parameterType);
        }
Exemplo n.º 3
0
 private MrProperty(
     MrType declaringType,
     PropertyDefinitionHandle propertyDefinitionHandle,
     PropertyDefinition propertyDefinition)
 {
     DeclaringType    = declaringType;
     DefinitionHandle = propertyDefinitionHandle;
     Definition       = propertyDefinition;
 }
 private MrField(
     FieldDefinitionHandle fieldDefinitionHandle,
     FieldDefinition fieldDefinition,
     MrType declaringType)
 {
     DeclaringType    = declaringType;
     DefinitionHandle = fieldDefinitionHandle;
     Definition       = fieldDefinition;
 }
Exemplo n.º 5
0
 private MrEvent(
     EventDefinitionHandle eventDefinitionHandle,
     EventDefinition eventDefinition,
     MrType declaringType)
 {
     DefinitionHandle = eventDefinitionHandle;
     Definition       = eventDefinition;
     DeclaringType    = declaringType;
 }
Exemplo n.º 6
0
        /// <summary>
        /// The property's getter and setter, either of which could be null
        /// </summary>
        static void GetGetterAndSetter(
            PropertyDefinition propertyDefinition,
            MrType declaringType,
            bool publicishOnly,
            out MrMethod getter, out MrMethod setter)
        {
            var propertyAccessors = propertyDefinition.GetAccessors();

            getter = MrMethod.TryGetMethod(propertyAccessors.Getter, declaringType, publicishOnly);
            setter = MrMethod.TryGetMethod(propertyAccessors.Setter, declaringType, publicishOnly);
        }
        static bool AreFieldAttributesPublicish(FieldAttributes attributes, MrType declaringType)
        {
            if (IsPublicFieldAttributes(attributes) ||
                IsProtectedFieldAttributes(attributes) &&
                !declaringType.IsSealed)
            {
                return(true);
            }

            return(false);
        }
        static internal MrField TryCreate(
            FieldDefinitionHandle fieldDefinitionHandle,
            MrType declaringType,
            bool publicishOnly)
        {
            var fieldDefinition = declaringType.Assembly.Reader.GetFieldDefinition(fieldDefinitionHandle);

            if (!publicishOnly || AreFieldAttributesPublicish(fieldDefinition.Attributes, declaringType))
            {
                return(new MrField(fieldDefinitionHandle, fieldDefinition, declaringType));
            }
            return(null);
        }
        /// <summary>
        /// Try to find a type in a specific assembly
        /// </summary>
        public bool TryFindMrType(string fullTypeName, MetadataReader reader, out MrType mrType)
        {
            mrType = null;

            foreach (var assembly in _loadedAssemblies.Values.Union(_implicitAssemblies.Values))
            {
                if (assembly.Reader == reader)
                {
                    return(assembly.TryGetType(fullTypeName, out mrType));
                }
            }

            return(false);
        }
Exemplo n.º 10
0
 private static MrMethod TryGetEtter(
     MrType declaringType,
     MethodDefinitionHandle methodDefinitionHandle,
     bool publicishOnly)
 {
     if (!methodDefinitionHandle.IsNil)
     {
         var methodDefinition = declaringType.Assembly.Reader.GetMethodDefinition(methodDefinitionHandle);
         var attributes       = methodDefinition.Attributes;
         if (!publicishOnly || MrMethod.AreAttributesPublicish(attributes, declaringType))
         {
             return(new MrMethod(methodDefinitionHandle, declaringType, methodDefinition));
         }
     }
     return(null);
 }
Exemplo n.º 11
0
        static internal MrEvent TryGetEvent(
            EventDefinitionHandle eventDefinitionHandle,
            MrType declaringType,
            bool publicishOnly)
        {
            var eventDefinition = declaringType.Assembly.Reader.GetEventDefinition(eventDefinitionHandle);

            if (publicishOnly)
            {
                var eventAccessors  = eventDefinition.GetAccessors();
                var adderDefinition = declaringType.Assembly.Reader.GetMethodDefinition(eventAccessors.Adder);
                if (!MrMethod.AreAttributesPublicish(adderDefinition.Attributes, declaringType))
                {
                    return(null);
                }
            }

            return(new MrEvent(eventDefinitionHandle, eventDefinition, declaringType));
        }
Exemplo n.º 12
0
        internal MrType GetTypeFromName(string fullName)
        {
            if (_nameToMrType != null && _nameToMrType.TryGetValue(fullName, out var type))
            {
                return(type);
            }

            if (IsFakeAssembly)
            {
                type = MrType.CreateFakeType(fullName, this);
                if (_nameToMrType == null)
                {
                    _nameToMrType = new Dictionary <string, MrType>();
                }
                _nameToMrType[fullName] = type;
                return(type);
            }

            throw new Exception("Type not found");
        }
        /// <summary>
        /// Try to find a type in any loaded assembly
        /// </summary>
        public bool TryFindMrType(string fullTypeName, out MrType mrType)
        {
            mrType = null;

            // Convert e.g. Byte[] into Byte
            fullTypeName = MrType.GetUnmodifiedTypeName(fullTypeName, out var arrayRank, out var isReference, out var isPointer);

            foreach (var assembly in _loadedAssemblies.Values.Union(_implicitAssemblies.Values))
            {
                if (assembly.TryGetType(fullTypeName, out mrType))
                {
                    // Convert back if necessary, e.g. Byte into Byte[]
                    if (arrayRank != null || isReference || isPointer)
                    {
                        mrType = MrType.Clone(mrType, arrayRank, isReference, isPointer);
                    }

                    return(true);
                }
            }

            return(false);
        }