/// <summary>
        /// Creates a wrapper.
        /// </summary>
        /// <param name="policy">The reflection policy.</param>
        /// <param name="handle">The underlying reflection object.</param>
        /// <param name="declaringType">The declaring type.</param>
        /// <param name="reflectedType">The reflected type.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="policy"/>, <paramref name="handle"/>,
        /// <paramref name="declaringType"/>, or <paramref name="reflectedType"/> is null.</exception>
        protected StaticReflectedMemberWrapper(StaticReflectionPolicy policy, object handle, StaticDeclaredTypeWrapper declaringType,
            StaticDeclaredTypeWrapper reflectedType)
            : base(policy, handle, declaringType)
        {
            if (declaringType == null)
                throw new ArgumentNullException("declaringType");
            if (reflectedType == null)
                throw new ArgumentNullException("reflectedType");

            this.reflectedType = reflectedType;
        }
 /// <summary>
 /// Creates a wrapper.
 /// </summary>
 /// <param name="policy">The reflection policy.</param>
 /// <param name="handle">The underlying reflection object.</param>
 /// <param name="declaringType">The declaring type.</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="policy"/>, <paramref name="handle"/>,
 /// or <paramref name="declaringType"/> is null.</exception>
 public StaticConstructorWrapper(StaticReflectionPolicy policy, object handle, StaticDeclaredTypeWrapper declaringType)
     : base(policy, handle, declaringType, declaringType)
 {
 }
 /// <summary>
 /// Gets the nested types of a type.
 /// Only includes declared nested types, not inherited ones.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's nested types.</returns>
 protected internal abstract IEnumerable<StaticTypeWrapper> GetTypeNestedTypes(StaticDeclaredTypeWrapper type);
 /// <summary>
 /// Gets the methods of a type including accessor methods for properties and events.
 /// Only includes declared methods, not inherited ones.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <param name="reflectedType">The reflected type, not null.</param>
 /// <returns>The type's methods.</returns>
 protected internal abstract IEnumerable<StaticMethodWrapper> GetTypeMethods(StaticDeclaredTypeWrapper type,
     StaticDeclaredTypeWrapper reflectedType);
 /// <summary>
 /// Gets the generic parameters of a type, including all generic parameters
 /// of its declaring types if it is nested enumerated from outside in.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's generic parameters.</returns>
 protected internal abstract IList<StaticGenericParameterWrapper> GetTypeGenericParameters(StaticDeclaredTypeWrapper type);
 /// <summary>
 /// Gets the constructors of a type.
 /// Only includes declared methods, not inherited ones.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's constructors.</returns>
 protected internal abstract IEnumerable<StaticConstructorWrapper> GetTypeConstructors(StaticDeclaredTypeWrapper type);
 /// <summary>
 /// Gets the attributes of a type.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type attributes.</returns>
 protected internal abstract TypeAttributes GetTypeAttributes(StaticDeclaredTypeWrapper type);
Ejemplo n.º 8
0
 protected override string GetTypeNamespace(StaticDeclaredTypeWrapper type)
 {
     ITypeElement typeHandle = (ITypeElement)type.Handle;
     if (!typeHandle.IsValid())
         return "";
     return typeHandle.GetContainingNamespace().QualifiedName;
 }
Ejemplo n.º 9
0
        private void BuildTypeGenericParameters(StaticDeclaredTypeWrapper ownerType, ITypeElement typeHandle, List<StaticGenericParameterWrapper> genericParameters)
        {
            ITypeElement declaringType = typeHandle.GetContainingType();
            if (declaringType != null)
                BuildTypeGenericParameters(ownerType, declaringType, genericParameters);

            foreach (ITypeParameter parameterHandle in typeHandle.TypeParameters)
                genericParameters.Add(StaticGenericParameterWrapper.CreateGenericTypeParameter(this, parameterHandle, ownerType));
        }
Ejemplo n.º 10
0
        protected override IList<StaticGenericParameterWrapper> GetTypeGenericParameters(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                return EmptyArray<StaticGenericParameterWrapper>.Instance;

            var genericParameters = new List<StaticGenericParameterWrapper>();
            BuildTypeGenericParameters(type, typeHandle, genericParameters);
            return genericParameters;
        }
Ejemplo n.º 11
0
        protected override IList<StaticDeclaredTypeWrapper> GetTypeInterfaces(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                return EmptyArray<StaticDeclaredTypeWrapper>.Instance;

            List<StaticDeclaredTypeWrapper> interfaces = new List<StaticDeclaredTypeWrapper>();

            foreach (IDeclaredType superTypeHandle in SafeGetSuperTypes(typeHandle))
            {
                IInterface @interface = superTypeHandle.GetTypeElement() as IInterface;
                if (@interface != null)
                    interfaces.Add(MakeDeclaredType(superTypeHandle));
            }

            return interfaces;
        }
Ejemplo n.º 12
0
        protected override StaticDeclaredTypeWrapper GetTypeBaseType(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                return null;

            if (!(typeHandle is IInterface))
            {
                foreach (IDeclaredType superTypeHandle in SafeGetSuperTypes(typeHandle))
                {
                    IClass @class = superTypeHandle.GetTypeElement() as IClass;
                    if (@class != null)
                    {
                        StaticDeclaredTypeWrapper baseType = MakeDeclaredType(superTypeHandle);

                        // Handles an edge case where the base type is also the containing type of the original type.
                        // This can occur when the original type is nested within its own basetype.
                        // In that case, the containing type should be parameterized by the generic type parameters
                        // of the nested that apply to it.
                        int containingTypeParamCount = baseType.GenericArguments.Count;
                        if (containingTypeParamCount != 0 && IsContainingType(@class, typeHandle))
                        {
                            var containingTypeArgs = new ITypeInfo[containingTypeParamCount];
                            IList<ITypeInfo> genericArgs = type.GenericArguments;
                            for (int i = 0; i < containingTypeParamCount; i++)
                                containingTypeArgs[i] = genericArgs[i];

                            baseType = baseType.MakeGenericType(containingTypeArgs);
                        }

                        return baseType;
                    }
                }
            }

            return null;
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates a wrapper.
 /// </summary>
 /// <param name="policy">The reflection policy.</param>
 /// <param name="handle">The underlying reflection object.</param>
 /// <param name="declaringType">The declaring type.</param>
 /// <param name="reflectedType">The reflected type.</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="policy"/>, <paramref name="handle"/>,
 /// <paramref name="declaringType"/>, or <paramref name="reflectedType"/> is null.</exception>
 public StaticEventWrapper(StaticReflectionPolicy policy, object handle, StaticDeclaredTypeWrapper declaringType,
     StaticDeclaredTypeWrapper reflectedType)
     : base(policy, handle, declaringType, reflectedType)
 {
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a wrapper.
 /// </summary>
 /// <param name="policy">The reflection policy.</param>
 /// <param name="handle">The underlying reflection object.</param>
 /// <param name="declaringType">The declaring type.</param>
 /// <param name="reflectedType">The reflected type, or null if none.</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="policy"/>, <paramref name="handle"/>,
 /// <paramref name="declaringType"/>, or <paramref name="reflectedType"/> is null.</exception>
 protected StaticFunctionWrapper(StaticReflectionPolicy policy, object handle, StaticDeclaredTypeWrapper declaringType,
     StaticDeclaredTypeWrapper reflectedType)
     : base(policy, handle, declaringType, reflectedType)
 {
 }
Ejemplo n.º 15
0
        protected override TypeAttributes GetTypeAttributes(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement) type.Handle;

            TypeAttributes flags = 0;
            ReflectorFlagsUtils.AddFlagIfTrue(ref flags, TypeAttributes.Interface, typeHandle is IInterface);

            IModifiersOwner modifiers = typeHandle as IModifiersOwner;
            if (modifiers != null)
            {
                ReflectorFlagsUtils.AddFlagIfTrue(ref flags, TypeAttributes.Abstract, modifiers.IsAbstract);
                ReflectorFlagsUtils.AddFlagIfTrue(ref flags, TypeAttributes.Sealed, modifiers.IsSealed);

                bool isNested = typeHandle.GetContainingType() != null;

                switch (modifiers.GetAccessRights())
                {
                    case AccessRights.PUBLIC:
                        flags |= isNested ? TypeAttributes.NestedPublic : TypeAttributes.Public;
                        break;
                    case AccessRights.PRIVATE:
                        flags |= isNested ? TypeAttributes.NestedPrivate : TypeAttributes.NotPublic;
                        break;
                    case AccessRights.NONE:
                    case AccessRights.INTERNAL:
                        flags |= isNested ? TypeAttributes.NestedAssembly : TypeAttributes.NotPublic;
                        break;
                    case AccessRights.PROTECTED:
                        flags |= isNested ? TypeAttributes.NestedFamily : TypeAttributes.NotPublic;
                        break;
                    case AccessRights.PROTECTED_AND_INTERNAL:
                        flags |= isNested ? TypeAttributes.NestedFamANDAssem : TypeAttributes.NotPublic;
                        break;
                    case AccessRights.PROTECTED_OR_INTERNAL:
                        flags |= isNested ? TypeAttributes.NestedFamORAssem : TypeAttributes.NotPublic;
                        break;
                }
            }

            if (typeHandle is IDelegate || typeHandle is IEnum || typeHandle is IStruct)
                flags |= TypeAttributes.Sealed;

            return flags;
        }
Ejemplo n.º 16
0
        protected override IEnumerable<StaticConstructorWrapper> GetTypeConstructors(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            bool foundDefault = false;
            foreach (IConstructor constructorHandle in typeHandle.Constructors)
            {
                if (constructorHandle.IsValid())
                {
                    if (constructorHandle.IsDefault)
                    {
                        if (typeHandle is IStruct)
                            continue; // Note: Default constructors for structs are not visible via reflection
                        foundDefault = true;
                    }

                    yield return new StaticConstructorWrapper(this, constructorHandle, type);
                }
            }

            if (!foundDefault)
            {
                IClass classHandle = typeHandle as IClass;
                if (classHandle != null && !classHandle.IsStatic)
                    yield return new StaticConstructorWrapper(this, new DefaultConstructor(typeHandle), type);

                IDelegate delegateHandle = typeHandle as IDelegate;
                if (delegateHandle != null)
                    yield return new StaticConstructorWrapper(this, new DelegateConstructor(delegateHandle), type);
            }
        }
Ejemplo n.º 17
0
 protected override StaticAssemblyWrapper GetTypeAssembly(StaticDeclaredTypeWrapper type)
 {
     ITypeElement typeHandle = (ITypeElement)type.Handle;
     return WrapModule(GetModule(typeHandle));
 }
Ejemplo n.º 18
0
        protected override IEnumerable<StaticMethodWrapper> GetTypeMethods(StaticDeclaredTypeWrapper type,
            StaticDeclaredTypeWrapper reflectedType)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            foreach (IMethod methodHandle in typeHandle.Methods)
            {
                if (methodHandle.IsValid())
                    yield return new StaticMethodWrapper(this, methodHandle, type, reflectedType, type.Substitution);
            }

            foreach (IOperator operatorHandle in typeHandle.Operators)
            {
                if (operatorHandle.IsValid())
                    yield return new StaticMethodWrapper(this, operatorHandle, type, reflectedType, type.Substitution);
            }

            foreach (StaticPropertyWrapper property in GetTypeProperties(type, reflectedType))
            {
                if (property.GetMethod != null)
                    yield return property.GetMethod;
                if (property.SetMethod != null)
                    yield return property.SetMethod;
            }

            foreach (StaticEventWrapper @event in GetTypeEvents(type, reflectedType))
            {
                if (@event.AddMethod != null)
                    yield return @event.AddMethod;
                if (@event.RemoveMethod != null)
                    yield return @event.RemoveMethod;
                if (@event.RaiseMethod != null)
                    yield return @event.RaiseMethod;
            }
        }
 /// <summary>
 /// Gets the assembly that contains a type.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's assembly.</returns>
 protected internal abstract StaticAssemblyWrapper GetTypeAssembly(StaticDeclaredTypeWrapper type);
Ejemplo n.º 20
0
        protected override IEnumerable<StaticPropertyWrapper> GetTypeProperties(StaticDeclaredTypeWrapper type,
            StaticDeclaredTypeWrapper reflectedType)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            foreach (IProperty propertyHandle in typeHandle.Properties)
            {
                if (propertyHandle.IsValid())
                    yield return new StaticPropertyWrapper(this, propertyHandle, type, reflectedType);
            }
        }
 /// <summary>
 /// Gets the base type of atype.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The base type.</returns>
 protected internal abstract StaticDeclaredTypeWrapper GetTypeBaseType(StaticDeclaredTypeWrapper type);
Ejemplo n.º 22
0
        protected override IEnumerable<StaticFieldWrapper> GetTypeFields(StaticDeclaredTypeWrapper type,
            StaticDeclaredTypeWrapper reflectedType)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            IClass classHandle = typeHandle as IClass;
            if (classHandle != null)
            {
                foreach (IField fieldHandle in classHandle.Fields)
                {
                    if (fieldHandle.IsValid())
                        yield return new StaticFieldWrapper(this, fieldHandle, type, reflectedType);
                }

                foreach (IField fieldHandle in classHandle.Constants)
                {
                    if (fieldHandle.IsValid())
                        yield return new StaticFieldWrapper(this, fieldHandle, type, reflectedType);
                }
            }
            else
            {
                IStruct structHandle = typeHandle as IStruct;
                if (structHandle != null)
                {
                    foreach (IField fieldHandle in structHandle.Fields)
                    {
                        if (fieldHandle.IsValid())
                            yield return new StaticFieldWrapper(this, fieldHandle, type, reflectedType);
                    }

                    foreach (IField fieldHandle in structHandle.Constants)
                    {
                        if (fieldHandle.IsValid())
                            yield return new StaticFieldWrapper(this, fieldHandle, type, reflectedType);
                    }
                }
            }
        }
 /// <summary>
 /// Gets the events of a type.
 /// Only includes declared methods, not inherited ones.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <param name="reflectedType">The reflected type, not null.</param>
 /// <returns>The type's events.</returns>
 protected internal abstract IEnumerable<StaticEventWrapper> GetTypeEvents(StaticDeclaredTypeWrapper type,
     StaticDeclaredTypeWrapper reflectedType);
Ejemplo n.º 24
0
        protected override IEnumerable<StaticEventWrapper> GetTypeEvents(StaticDeclaredTypeWrapper type,
            StaticDeclaredTypeWrapper reflectedType)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            foreach (IEvent eventHandle in typeHandle.Events)
            {
                if (eventHandle.IsValid())
                    yield return new StaticEventWrapper(this, eventHandle, type, reflectedType);
            }
        }
 /// <summary>
 /// Gets the interfaces directly implemented by a type.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's interfaces.</returns>
 protected internal abstract IList<StaticDeclaredTypeWrapper> GetTypeInterfaces(StaticDeclaredTypeWrapper type);
Ejemplo n.º 26
0
        protected override IEnumerable<StaticTypeWrapper> GetTypeNestedTypes(StaticDeclaredTypeWrapper type)
        {
            ITypeElement typeHandle = (ITypeElement)type.Handle;
            if (!typeHandle.IsValid())
                yield break;

            foreach (ITypeElement nestedTypeHandle in typeHandle.NestedTypes)
            {
                if (nestedTypeHandle.IsValid())
                    yield return new StaticDeclaredTypeWrapper(this, nestedTypeHandle, type, type.Substitution);
            }
        }
 /// <summary>
 /// Gets the namespace that contains a type.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <returns>The type's namespace, or an empty string if it has none.</returns>
 protected internal abstract string GetTypeNamespace(StaticDeclaredTypeWrapper type);
Ejemplo n.º 28
0
        private StaticDeclaredTypeWrapper MakeDeclaredType(ITypeElement typeElementHandle, ISubstitution substitutionHandle)
        {
            if (typeElementHandle is ITypeParameter)
                throw new ArgumentException("This method should never be called with a generic parameter as input.",
                    "typeElementHandle");

            ITypeElement declaringTypeElementHandle = typeElementHandle.GetContainingType();
            StaticDeclaredTypeWrapper type;
            if (declaringTypeElementHandle != null)
            {
                StaticDeclaredTypeWrapper declaringType = MakeDeclaredType(declaringTypeElementHandle,
                    substitutionHandle);
                type = new StaticDeclaredTypeWrapper(this, typeElementHandle, declaringType, declaringType.Substitution);
            }
            else
            {
                type = new StaticDeclaredTypeWrapper(this, typeElementHandle, null, StaticTypeSubstitution.Empty);
            }

#if ! RESHARPER_50_OR_NEWER
            var typeParameterHandles = new List<ITypeParameter>(typeElementHandle.AllTypeParameters);
#else
            var typeParameterHandles = new List<ITypeParameter>(typeElementHandle.GetAllTypeParameters());
#endif
#if RESHARPER_31 || RESHARPER_40 || RESHARPER_41
            if (substitutionHandle.IsIdempotent(typeParameterHandles))
#else
            if (substitutionHandle.IsIdempotentAll(typeParameterHandles))
#endif
            {
                return type;
            }

        ITypeInfo[] genericArguments = GenericCollectionUtils.ConvertAllToArray<ITypeParameter, ITypeInfo>(typeParameterHandles, delegate(ITypeParameter typeParameterHandle)
            {
                IType substitutedType = substitutionHandle.Apply(typeParameterHandle);
                if (substitutedType.IsUnknown)
                    return MakeGenericParameterType(typeParameterHandle);

                return MakeType(substitutedType);
            });
            return type.MakeGenericType(genericArguments);
        }
 /// <summary>
 /// Gets the properties of a type.
 /// Only includes declared methods, not inherited ones.
 /// </summary>
 /// <param name="type">The type, not null.</param>
 /// <param name="reflectedType">The reflected type, not null.</param>
 /// <returns>The type's properties.</returns>
 protected internal abstract IEnumerable<StaticPropertyWrapper> GetTypeProperties(StaticDeclaredTypeWrapper type,
     StaticDeclaredTypeWrapper reflectedType);
Ejemplo n.º 30
0
 /// <summary>
 /// Creates a wrapper.
 /// </summary>
 /// <param name="policy">The reflection policy.</param>
 /// <param name="handle">The underlying reflection object.</param>
 /// <param name="declaringType">The declaring type, or null if none.</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="policy"/> or <paramref name="handle"/> is null.</exception>
 protected StaticMemberWrapper(StaticReflectionPolicy policy, object handle, StaticDeclaredTypeWrapper declaringType)
     : base(policy, handle)
 {
     this.declaringType = declaringType;
 }