private IEnumerable <StaticTypeWrapper> EnumerateNestedTypes(bool includePublicTypes, bool includeNonPublicTypes)
        {
            // Handle an interesting edge case of .Net reflection.  Enumeration of nested types
            // within generic types discards any generic type arguments bound to it and has as its
            // declaring type the generic type definition.
            StaticDeclaredTypeWrapper unspecializedType = IsGenericType ? GenericTypeDefinition : this;

            foreach (StaticTypeWrapper nestedType in ReflectionPolicy.GetTypeNestedTypes(unspecializedType))
            {
                if (nestedType.IsNestedPublic)
                {
                    if (!includePublicTypes)
                    {
                        continue;
                    }
                }
                else
                {
                    if (!includeNonPublicTypes)
                    {
                        continue;
                    }
                }

                yield return(nestedType);
            }
        }
Exemplo n.º 2
0
        protected IAssemblyInfo GetAssembly(Assembly assembly)
        {
            IAssemblyInfo wrapper = ReflectionPolicy.LoadAssembly(assembly.GetName());

            Assert.IsNotNull(wrapper, "Could not find assembly '{0}'.", assembly);
            return(wrapper);
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public IEnumerable <IAttributeInfo> GetAttributeInfos(ITypeInfo attributeType, bool inherit)
        {
            Dictionary <ITypeInfo, AttributeUsageAttribute> attributeUsages =
                inherit ? new Dictionary <ITypeInfo, AttributeUsageAttribute>() : null;

            // Yield all attributes declared by the member itself.
            // Keep track of which types were seen so we can resolve inherited but non-multiple attributes later.
            string qualifiedTypeName = attributeType != null ? attributeType.FullName : null;

            foreach (IAttributeInfo attribute in GetAllCustomAttributes())
            {
                ITypeInfo type = attribute.Type;
                if (qualifiedTypeName == null || ReflectionUtils.IsDerivedFrom(type, qualifiedTypeName))
                {
                    yield return(attribute);

                    if (inherit)
                    {
                        attributeUsages[type] = null;
                    }
                }
            }

            // Now loop over the inherited member declarations to find inherited attributes.
            // If we see an attribute of a kind we have seen before, then we check whether
            // multiple instances of it are allowed and discard it if not.
            if (inherit)
            {
                foreach (ICodeElementInfo inheritedMember in GetInheritedElements())
                {
                    foreach (IAttributeInfo attribute in inheritedMember.GetAttributeInfos(attributeType, false))
                    {
                        ITypeInfo inheritedAttributeType = attribute.Type;

                        AttributeUsageAttribute attributeUsage;
                        bool seenBefore = attributeUsages.TryGetValue(inheritedAttributeType, out attributeUsage);

                        if (attributeUsage == null)
                        {
                            attributeUsage = ReflectionPolicy.GetAttributeUsage(inheritedAttributeType);
                            attributeUsages[inheritedAttributeType] = attributeUsage;
                        }

                        if (!attributeUsage.Inherited)
                        {
                            continue;
                        }

                        if (seenBefore && !attributeUsage.AllowMultiple)
                        {
                            continue;
                        }

                        yield return(attribute);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public ITypeInfo GetType(string typeName)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }

            return(getTypeMemoizer.Memoize(typeName, () =>
                                           ReflectionPolicy.GetAssemblyType(this, typeName)));
        }
Exemplo n.º 5
0
        private void FindReferencesRecursively(List <object> path, object obj, object targetObj, HashSet <object> findCache, Action <string> callback)
        {
            if (obj == null)
            {
                return;
            }

            var type       = obj.GetType();
            var targetType = targetObj.GetType();

            if (type == targetType && obj.Equals(targetObj))
            {
                callback(PathToString(path));
                return;
            }

            if (ReflectionPolicy.IsAtom(type))
            {
                return;
            }

            var layout = mGraph.GetLayout(type);

            if (layout == null)
            {
                return;
            }

            if (findCache.Contains(obj))
            {
                return;
            }
            findCache.Add(obj);

            if (type.IsArray)
            {
                var a = obj as Array;
                for (int i = 0, length = a.Length; i < length; ++i)
                {
                    path.Add(i);
                    FindReferencesRecursively(path, a.GetValue(i), targetObj, findCache, callback);
                    path.RemoveAt(path.Count - 1);
                }
            }
            else
            {
                foreach (var member in layout.Members)
                {
                    path.Add(member.Key);
                    FindReferencesRecursively(path, TryGetMemberValue(path, obj, member.Value), targetObj, findCache, callback);
                    path.RemoveAt(path.Count - 1);
                }
            }
        }
 private IEnumerable <StaticFieldWrapper> EnumerateFields(BindingFlags bindingFlags,
                                                          StaticDeclaredTypeWrapper reflectedType)
 {
     foreach (StaticFieldWrapper field in ReflectionPolicy.GetTypeFields(this, reflectedType))
     {
         if (MatchesBindingFlags(bindingFlags, field.IsPublic, field.IsStatic))
         {
             yield return(field);
         }
     }
 }
 private IEnumerable <StaticMethodWrapper> EnumerateDeclaredMethods(BindingFlags bindingFlags,
                                                                    StaticDeclaredTypeWrapper reflectedType)
 {
     foreach (StaticMethodWrapper method in ReflectionPolicy.GetTypeMethods(this, reflectedType))
     {
         if (MatchesBindingFlags(bindingFlags, method.IsPublic, method.IsStatic))
         {
             yield return(method);
         }
     }
 }
Exemplo n.º 8
0
 private TypeLayout BuildLayout(Type type)
 {
     if (type.IsArray || ReflectionPolicy.IsAtom(type))
     {
         return(new TypeLayout(Enumerable.Empty <PropertyInfo>(), Enumerable.Empty <FieldInfo>()));
     }
     else
     {
         return(new TypeLayout(
                    Policy.GetInstanceProperties(type).Where(p => mType2Node[p.PropertyType].Related),
                    Policy.GetInstanceFields(type).Where(f => mType2Node[f.FieldType].Related)));
     }
 }
        /// <inheritdoc />
        public override IList <IConstructorInfo> GetConstructors(BindingFlags bindingFlags)
        {
            List <IConstructorInfo> result = new List <IConstructorInfo>();

            foreach (StaticConstructorWrapper constructor in ReflectionPolicy.GetTypeConstructors(this))
            {
                if (MatchesBindingFlags(bindingFlags, constructor.IsPublic, constructor.IsStatic))
                {
                    result.Add(constructor);
                }
            }

            return(result);
        }
Exemplo n.º 10
0
        public TypeGraph(Type targetType, IEnumerable <Type> types, ReflectionPolicy policy)
        {
            TargetType = targetType;
            Policy     = policy;

            foreach (var type in types)
            {
                GetOrAddNode(type);
            }

            var node = GetOrAddNode(targetType);

            node.Related = true;
            mNewRelatedNodes.Push(node);
            MarkDerivedNodesAsRelated();
        }
Exemplo n.º 11
0
        /// <inheritdoc />
        public override CodeLocation GetCodeLocation()
        {
            return(codeLocationMemoizer.Memoize(() =>
            {
                CodeLocation
                location = ReflectionPolicy.GetMemberSourceLocation(this);
                if (location == CodeLocation.Unknown && declaringType != null)
                {
                    location = DeclaringType.GetCodeLocation();
                    if (location != CodeLocation.Unknown)
                    {
                        location = new CodeLocation(location.Path, 0, 0);
                    }
                }

                return location;
            }));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the events that this one overrides or hides.
        /// Only includes overrides that appear on class types, not interfaces.
        /// </summary>
        /// <param name="overridesOnly">If true, only returns overrides.</param>
        public IEnumerable <StaticEventWrapper> GetOverridenOrHiddenEvents(bool overridesOnly)
        {
            StaticMethodWrapper discriminator = GetDiscriminatorMethod(this);

            if (overridesOnly && !discriminator.IsOverride)
            {
                yield break;
            }

            string eventName = Name;

            foreach (StaticDeclaredTypeWrapper baseType in DeclaringType.GetAllBaseTypes())
            {
                foreach (StaticEventWrapper other in ReflectionPolicy.GetTypeEvents(baseType, ReflectedType))
                {
                    if (eventName == other.Name)
                    {
                        if (overridesOnly)
                        {
                            StaticMethodWrapper otherDiscriminator = GetDiscriminatorMethod(other);
                            if (otherDiscriminator == null)
                            {
                                yield break;
                            }

                            if (discriminator.HidesMethod(otherDiscriminator))
                            {
                                yield return(other);

                                if (!otherDiscriminator.IsOverride)
                                {
                                    yield break;
                                }
                                break;
                            }
                        }
                        else
                        {
                            yield return(other);
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        private IEnumerable <StaticPropertyWrapper> EnumerateProperties(BindingFlags bindingFlags,
                                                                        StaticDeclaredTypeWrapper reflectedType)
        {
            foreach (StaticPropertyWrapper property in ReflectionPolicy.GetTypeProperties(this, reflectedType))
            {
                IMethodInfo getMethod = property.GetMethod;
                IMethodInfo setMethod = property.SetMethod;

                bool isPublic = getMethod != null && getMethod.IsPublic ||
                                setMethod != null && setMethod.IsPublic;
                bool isStatic = getMethod != null && getMethod.IsStatic ||
                                setMethod != null && setMethod.IsStatic;

                if (MatchesBindingFlags(bindingFlags, isPublic, isStatic))
                {
                    yield return(property);
                }
            }
        }
Exemplo n.º 14
0
        private IEnumerable <StaticEventWrapper> EnumerateEvents(BindingFlags bindingFlags,
                                                                 StaticDeclaredTypeWrapper reflectedType)
        {
            foreach (StaticEventWrapper @event in ReflectionPolicy.GetTypeEvents(this, reflectedType))
            {
                IMethodInfo addMethod    = @event.AddMethod;
                IMethodInfo removeMethod = @event.RemoveMethod;
                IMethodInfo raiseMethod  = @event.RaiseMethod;

                bool isPublic = addMethod != null && addMethod.IsPublic ||
                                removeMethod != null && removeMethod.IsPublic ||
                                raiseMethod != null && raiseMethod.IsPublic;
                bool isStatic = addMethod != null && addMethod.IsStatic ||
                                removeMethod != null && removeMethod.IsStatic ||
                                raiseMethod != null && raiseMethod.IsStatic;

                if (MatchesBindingFlags(bindingFlags, isPublic, isStatic))
                {
                    yield return(@event);
                }
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the methods that this one overrides or hides.
        /// Only includes overrides that appear on class types, not interfaces.
        /// </summary>
        /// <param name="overridesOnly">If true, only returns overrides.</param>
        public IEnumerable <StaticMethodWrapper> GetOverridenOrHiddenMethods(bool overridesOnly)
        {
            if (overridesOnly && !IsOverride)
            {
                yield break;
            }

            foreach (StaticDeclaredTypeWrapper baseType in DeclaringType.GetAllBaseTypes())
            {
                foreach (StaticMethodWrapper other in ReflectionPolicy.GetTypeMethods(baseType, ReflectedType))
                {
                    if (HidesMethod(other))
                    {
                        yield return(other);

                        if (overridesOnly && !other.IsOverride)
                        {
                            yield break;
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 16
0
 /// <inheritdoc />
 protected override IEnumerable <StaticAttributeWrapper> GetCustomAttributes()
 {
     return(ReflectionPolicy.GetMemberCustomAttributes(this));
 }
Exemplo n.º 17
0
        public TypeGraph(Type targetType, IEnumerable<Type> types, ReflectionPolicy policy)
        {
            TargetType = targetType;
            Policy = policy;

            foreach (var type in types) GetOrAddNode(type);

            var node = GetOrAddNode(targetType);
            node.Related = true;
            mNewRelatedNodes.Push(node);
            MarkDerivedNodesAsRelated();
        }
Exemplo n.º 18
0
 public TypeGraph(Type targetType, IEnumerable<Assembly> assemblys, ReflectionPolicy policy)
     : this(targetType, assemblys.SelectMany(a => policy.GetAssemblyTypes(a)), policy)
 {
 }
Exemplo n.º 19
0
 public TypeGraph(Type targetType, ReflectionPolicy policy)
     : this(targetType, AppDomain.CurrentDomain.GetAssemblies(), policy)
 {
 }
Exemplo n.º 20
0
 /// <inheritdoc />
 public IList <ITypeInfo> GetExportedTypes()
 {
     return(exportedTypesMemoizer.Memoize(() =>
                                          new CovariantList <StaticDeclaredTypeWrapper, ITypeInfo>(ReflectionPolicy.GetAssemblyExportedTypes(this))));
 }
Exemplo n.º 21
0
 /// <inheritdoc />
 public IList <AssemblyName> GetReferencedAssemblies()
 {
     return(referencedAssembliesMemoizer.Memoize(() =>
                                                 new ReadOnlyCollection <AssemblyName>(ReflectionPolicy.GetAssemblyReferences(this))));
 }
Exemplo n.º 22
0
 /// <inheritdoc />
 public AssemblyName GetName()
 {
     return(assemblyNameMemoizer.Memoize(() => ReflectionPolicy.GetAssemblyName(this)));
 }
Exemplo n.º 23
0
 public TypeGraph(Type targetType, IEnumerable <Assembly> assemblys, ReflectionPolicy policy) :
     this(targetType, assemblys.SelectMany(a => policy.GetAssemblyTypes(a)), policy)
 {
 }
Exemplo n.º 24
0
 public TypeGraph(Type targetType, ReflectionPolicy policy) :
     this(targetType, AppDomain.CurrentDomain.GetAssemblies(), policy)
 {
 }