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); } }
protected IAssemblyInfo GetAssembly(Assembly assembly) { IAssemblyInfo wrapper = ReflectionPolicy.LoadAssembly(assembly.GetName()); Assert.IsNotNull(wrapper, "Could not find assembly '{0}'.", assembly); return(wrapper); }
/// <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); } } } }
/// <inheritdoc /> public ITypeInfo GetType(string typeName) { if (typeName == null) { throw new ArgumentNullException("typeName"); } return(getTypeMemoizer.Memoize(typeName, () => ReflectionPolicy.GetAssemblyType(this, typeName))); }
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); } } }
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); }
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(); }
/// <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; })); }
/// <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); } } } } }
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); } } }
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); } } }
/// <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; } } } }
/// <inheritdoc /> protected override IEnumerable <StaticAttributeWrapper> GetCustomAttributes() { return(ReflectionPolicy.GetMemberCustomAttributes(this)); }
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(); }
public TypeGraph(Type targetType, IEnumerable<Assembly> assemblys, ReflectionPolicy policy) : this(targetType, assemblys.SelectMany(a => policy.GetAssemblyTypes(a)), policy) { }
public TypeGraph(Type targetType, ReflectionPolicy policy) : this(targetType, AppDomain.CurrentDomain.GetAssemblies(), policy) { }
/// <inheritdoc /> public IList <ITypeInfo> GetExportedTypes() { return(exportedTypesMemoizer.Memoize(() => new CovariantList <StaticDeclaredTypeWrapper, ITypeInfo>(ReflectionPolicy.GetAssemblyExportedTypes(this)))); }
/// <inheritdoc /> public IList <AssemblyName> GetReferencedAssemblies() { return(referencedAssembliesMemoizer.Memoize(() => new ReadOnlyCollection <AssemblyName>(ReflectionPolicy.GetAssemblyReferences(this)))); }
/// <inheritdoc /> public AssemblyName GetName() { return(assemblyNameMemoizer.Memoize(() => ReflectionPolicy.GetAssemblyName(this))); }
public TypeGraph(Type targetType, IEnumerable <Assembly> assemblys, ReflectionPolicy policy) : this(targetType, assemblys.SelectMany(a => policy.GetAssemblyTypes(a)), policy) { }