public void TestFieldInstanceIgnoreCase() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.IgnoreCase; AnimalInstanceFieldNames.Select(s => s.ToUpper()).Select(s => typeof(Animal).Field(s, flags)).ForEach(Assert.IsNotNull); LionInstanceFieldNames.Select(s => s.ToUpper()).Select(s => typeof(Lion).Field(s, flags)).ForEach(Assert.IsNotNull); }
public void TestPropertyStaticDeclaredOnly() { FasterflectFlags flags = FasterflectFlags.StaticAnyVisibility | FasterflectFlags.DeclaredOnly; AnimalStaticPropertyNames.Select(s => typeof(Animal).Property(s, flags)).ForEach(Assert.IsNotNull); AnimalStaticPropertyNames.Select(s => typeof(Lion).Property(s, flags)).ForEach(Assert.IsNull); }
public void TestWithExcludeHiddenMembers() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExcludeHiddenMembers; IList <PropertyInfo> properties = typeof(Manager).Properties("Name"); Assert.AreEqual(2, properties.Count); Assert.AreEqual(typeof(Employee), properties.First().DeclaringType); properties = typeof(Manager).Properties(flags, "Name"); Assert.AreEqual(1, properties.Count); Assert.AreEqual(typeof(Employee), properties.First().DeclaringType); MemberTypes memberTypes = MemberTypes.Method | MemberTypes.Field | MemberTypes.Property; IList <MemberInfo> members = typeof(Manager).Members(memberTypes); Assert.AreEqual(13, members.Count); Assert.AreEqual(typeof(Manager), members.First().DeclaringType); members = typeof(Manager).Members(memberTypes, flags); Assert.AreEqual(7, members.Count); Assert.AreEqual(typeof(Manager), members.First().DeclaringType); members = typeof(Manager).Members(memberTypes, flags | FasterflectFlags.ExcludeBackingMembers); Assert.AreEqual(4, members.Count); Assert.AreEqual(typeof(Manager), members.First().DeclaringType); }
/// <summary> /// This method applies name filtering to a set of members. /// </summary> public static List <T> Filter <T>(this IEnumerable <T> members, FasterflectFlags bindingFlags, params string[] names) where T : MemberInfo { List <T> result = new List <T>(); bool ignoreCase = bindingFlags.IsSet(FasterflectFlags.IgnoreCase); bool isPartial = bindingFlags.IsSet(FasterflectFlags.PartialNameMatch); bool trimExplicit = bindingFlags.IsSet(FasterflectFlags.TrimExplicitlyImplemented); foreach (T member in members) { string memberName = trimExplicit ? member.Name.TrimExplicitlyImplementedName() : member.Name; for (int j = 0; j < names.Length; j++) { string name = names[j]; StringComparison comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; bool match = isPartial ? memberName.Contains(name) : memberName.Equals(name, comparison); if (match) { result.Add(member); break; } } } return(result); }
public void TestPropertyInstanceDeclaredOnly() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.DeclaredOnly; AnimalInstancePropertyNames.Select(s => typeof(Animal).Property(s, flags)).ForEach(Assert.IsNotNull); LionDeclaredInstancePropertyNames.Select(s => typeof(Lion).Property(s, flags)).ForEach(Assert.IsNotNull); }
public MemberCallInfo(Type type, string name, MemberTypes memberTypes, FasterflectFlags bindingFlags) { TargetType = type; BindingFlags = bindingFlags; MemberName = name; MemberType = memberTypes; }
public void TestFindMethodsWithParameterList() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility; IList <MethodInfo> methods = typeof(Animal).Methods(null, flags); CollectionAssert.AreEquivalent(AnimalInstanceMethodNames, methods.Select(m => m.Name).ToList()); // find methods with no arguments methods = typeof(Animal).Methods(new Type[0]); Assert.IsNotNull(methods); Assert.AreEqual(AnimalInstanceMethodNames.Where(n => n.StartsWith("get_")).Count(), methods.Count); methods = typeof(Animal).Methods(new Type[0], flags); Assert.AreEqual(AnimalInstanceMethodNames.Where(n => n.StartsWith("get_")).Count(), methods.Count); methods = typeof(Animal).Methods(new Type[0], flags, null); Assert.AreEqual(AnimalInstanceMethodNames.Where(n => n.StartsWith("get_")).Count(), methods.Count); methods = typeof(Animal).Methods(new Type[0], flags, new string[0]); Assert.AreEqual(AnimalInstanceMethodNames.Where(n => n.StartsWith("get_")).Count(), methods.Count); // find methods with single argument methods = typeof(Snake).Methods(new[] { typeof(Animal) }); Assert.IsNotNull(methods); Assert.AreEqual(1, methods.Count); methods = typeof(Snake).Methods(new[] { typeof(Animal) }, flags); Assert.AreEqual(1, methods.Count); methods = typeof(Snake).Methods(new[] { typeof(Animal) }, flags, "B"); Assert.AreEqual(0, methods.Count); methods = typeof(Snake).Methods(new[] { typeof(Animal) }, flags, "Bite"); Assert.AreEqual(1, methods.Count); }
public MapCallInfo(Type sourceType, Type targetType, FasterflectFlags flags, IList <string> sourceNames, IList <string> targetNames) { SourceType = sourceType; TargetType = targetType; Sources = sourceNames; Targets = targetNames ?? sourceNames; Flags = flags; }
public CallInfo(Type targetType, string name, FasterflectFlags bindingFlags, Type[] genericTypes, Type[] parameterTypes) { TargetType = targetType; Name = name; BindingFlags = bindingFlags; ParameterTypes = parameterTypes; GenericTypes = genericTypes; }
public void TestFieldStatic() { FasterflectFlags flags = FasterflectFlags.StaticAnyVisibility; AnimalInstanceFieldNames.Select(s => typeof(Animal).Field(s, flags)).ForEach(Assert.IsNull); AnimalStaticFieldNames.Select(s => typeof(Animal).Field(s, flags)).ForEach(Assert.IsNotNull); AnimalStaticFieldNames.Select(s => typeof(Lion).Field(s, flags)).ForEach(Assert.IsNotNull); }
public void TestPropertyInstanceIgnoreCase() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.IgnoreCase; AnimalInstancePropertyNames.Select(s => s.ToLower()).Select(s => typeof(Animal).Property(s)).ForEach(Assert.IsNull); AnimalInstancePropertyNames.Select(s => s.ToLower()).Select(s => typeof(Animal).Property(s, flags)).ForEach(Assert.IsNotNull); LionInstancePropertyNames.Select(s => s.ToLower()).Select(s => typeof(Lion).Property(s)).ForEach(Assert.IsNull); LionInstancePropertyNames.Select(s => s.ToLower()).Select(s => typeof(Lion).Property(s, flags)).ForEach(Assert.IsNotNull); }
public void TestFindMethodInstanceWithDeclaredOnly() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.DeclaredOnly; AnimalInstanceMethodNames.Select(s => typeof(Animal).Method(s, flags)).ForEach(Assert.IsNotNull); ReptileDeclaredInstanceMethodNames.Select(s => typeof(Reptile).Method(s, flags)).ForEach(Assert.IsNotNull); ReptileInstanceMethodNames.Where(s => !ReptileDeclaredInstanceMethodNames.Contains(s)).Select(s => typeof(Reptile).Method(s, flags)).ForEach(Assert.IsNull); SnakeDeclaredInstanceMethodNames.Select(s => typeof(Snake).Method(s, flags)).ForEach(Assert.IsNotNull); SnakeInstanceMethodNames.Where(s => !SnakeDeclaredInstanceMethodNames.Contains(s)).Select(s => typeof(Snake).Method(s, flags)).ForEach(Assert.IsNull); }
public void TestFindMethodInstanceWithExactBinding() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExactBinding; Assert.IsNull(typeof(Snake).Method("Bite", new Type[] { }, flags)); Assert.IsNull(typeof(Snake).Method("Bite", new[] { typeof(object) }, flags)); Assert.IsNotNull(typeof(Snake).Method("Bite", null, flags)); // should ignore flag when null is passed Assert.IsNotNull(typeof(Snake).Method("Bite", new[] { typeof(Animal) }, flags)); Assert.IsNull(typeof(Snake).Method("Bite", new[] { typeof(Mammal) }, flags)); Assert.IsNull(typeof(Snake).Method("Bite", new[] { typeof(Lion) }, flags)); }
/// <summary> /// Gets all types in the given <paramref name="assembly"/> matching the specified /// <paramref name="bindingFlags"/> and the optional list <paramref name="names"/>. /// </summary> /// <param name="assembly">The assembly in which to look for types.</param> /// <param name="bindingFlags">The <see cref="BindingFlags"/> used to customize how results /// are filters. If the <see cref="FasterflectFlags.PartialNameMatch"/> option is specified any name /// comparisons will use <see cref="String.Contains(String)"/> instead of <see cref="String.Equals(string)"/>.</param> /// <param name="names">An optional list of names against which to filter the result. If this is /// <see langword="null"/> or left empty, all types are returned.</param> /// <returns>A list of all matching types. This method never returns null.</returns> public static IList <Type> Types(this Assembly assembly, FasterflectFlags bindingFlags, params string[] names) { Type[] types = assembly.GetTypes(); bool hasNames = names != null && names.Length > 0; bool partialNameMatch = bindingFlags.IsSet(FasterflectFlags.PartialNameMatch); return(hasNames ? types.Where(t => names.Any(n => partialNameMatch ? t.Name.Contains(n) : t.Name == n)).ToArray() : types); }
public void TestPropertiesWithExcludeBackingMembers() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExcludeBackingMembers; IList <PropertyInfo> properties = typeof(Snake).Properties("SlideDistance"); Assert.AreEqual(2, properties.Count); Assert.AreEqual(typeof(Snake), properties.First().DeclaringType); properties = typeof(Snake).Properties(flags, "SlideDistance"); Assert.AreEqual(1, properties.Count); Assert.AreEqual(typeof(Snake), properties.First().DeclaringType); }
public void TestFindMethodInstanceIgnoreCase() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.IgnoreCase; AnimalInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Animal).Method(s)).ForEach(Assert.IsNull); AnimalInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Animal).Method(s, flags)).ForEach(Assert.IsNotNull); ReptileInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Reptile).Method(s)).ForEach(Assert.IsNull); ReptileInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Reptile).Method(s, flags)).ForEach(Assert.IsNotNull); SnakeInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Snake).Method(s)).ForEach(Assert.IsNull); SnakeInstanceMethodNames.Select(s => s.ToLower()).Select(s => typeof(Snake).Method(s, flags)).ForEach(Assert.IsNotNull); }
public void TestFieldWithPartialNameMatchAndExcludeBackingMembers() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.PartialNameMatch; string expectedName = AnimalInstanceFieldNames.Where(s => s.Contains("Movement")).First(); FieldInfo field = typeof(Animal).Field("Movement", flags); Assert.IsNotNull(field); Assert.AreEqual(expectedName, field.Name); field = typeof(Animal).Field("Movement", flags | FasterflectFlags.ExcludeBackingMembers); Assert.IsNull(field); }
/// <summary> /// This method applies flags-based filtering to a set of members. /// </summary> public static List <T> Filter <T>(this IEnumerable <T> members, FasterflectFlags bindingFlags) where T : MemberInfo { List <T> result = new List <T>(); List <string> properties = new List <string>(); bool excludeHidden = bindingFlags.IsSet(FasterflectFlags.ExcludeHiddenMembers); bool excludeBacking = bindingFlags.IsSet(FasterflectFlags.ExcludeBackingMembers); bool excludeExplicit = bindingFlags.IsSet(FasterflectFlags.ExcludeExplicitlyImplemented); foreach (T member in members) { bool exclude = false; if (excludeHidden) { MethodBase method = member as MethodBase; // filter out anything but methods/constructors based on their name only exclude |= method == null && result.Any(m => m.Name == member.Name); // filter out methods that do not have a unique signature (this prevents overloads from being excluded by the ExcludeHiddenMembers flag) exclude |= method != null && result.Where(m => m is MethodBase).Cast <MethodBase>().Any(m => m.Name == member.Name && m.HasParameterSignature(method.GetParameters())); } if (!exclude && excludeBacking) { exclude |= member is FieldInfo && member.Name[0] == '<'; if (member is MethodInfo methodInfo) { // filter out property backing methods exclude |= member.Name.Length > 4 && member.Name.Substring(1, 3) == "et_"; // filter out base implementations when an overrride exists exclude |= result.ContainsOverride(methodInfo); } if (member is PropertyInfo property) { MethodInfo propertyGetter = property.GetGetMethod(true); exclude |= propertyGetter.IsVirtual && properties.Contains(property.Name); if (!exclude) { properties.Add(property.Name); } } } exclude |= excludeExplicit && member.Name.Contains(".") && !member.Name.IsReservedName(); if (exclude) { continue; } result.Add(member); } return(result); }
public void TestFieldByPartialName() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.PartialNameMatch; string expectedName = AnimalInstanceFieldNames.Where(s => s.Contains("i")).First(); FieldInfo field = typeof(Animal).Field("i", flags); Assert.IsNotNull(field); Assert.AreEqual(expectedName, field.Name); expectedName = AnimalInstanceFieldNames.Where(s => s.Contains("bi")).First(); field = typeof(Animal).Field("bi", flags); Assert.IsNotNull(field); Assert.AreEqual(expectedName, field.Name); }
public void TestFindMethodInstanceWithExcludeBackingMembers() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExcludeBackingMembers; AnimalInstanceMethodNames.Select(s => typeof(Animal).Method(s)).ForEach(Assert.IsNotNull); AnimalInstanceMethodNames.Where(s => s.Contains("_")).Select(s => typeof(Animal).Method(s, flags)).ForEach(Assert.IsNull); AnimalInstanceMethodNames.Where(s => !s.Contains("_")).Select(s => typeof(Animal).Method(s, flags)).ForEach(Assert.IsNotNull); ReptileInstanceMethodNames.Select(s => typeof(Reptile).Method(s)).ForEach(Assert.IsNotNull); ReptileInstanceMethodNames.Where(s => s.Contains("_")).Select(s => typeof(Reptile).Method(s, flags)).ForEach(Assert.IsNull); ReptileInstanceMethodNames.Where(s => !s.Contains("_")).Select(s => typeof(Reptile).Method(s, flags)).ForEach(Assert.IsNotNull); SnakeInstanceMethodNames.Select(s => typeof(Snake).Method(s)).ForEach(Assert.IsNotNull); SnakeInstanceMethodNames.Where(s => s.Contains("_")).Select(s => typeof(Snake).Method(s, flags)).ForEach(Assert.IsNull); SnakeInstanceMethodNames.Where(s => !s.Contains("_")).Select(s => typeof(Snake).Method(s, flags)).ForEach(Assert.IsNotNull); }
public void TestFindMethodsWithExcludeBackingMembers() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExcludeBackingMembers; IList <MethodInfo> methods = typeof(Animal).Methods(flags); CollectionAssert.AreEquivalent(AnimalInstanceMethodNames.Where(s => !s.Contains("_")).Distinct().ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Reptile).Methods(flags); CollectionAssert.AreEquivalent(ReptileInstanceMethodNames.Where(s => !s.Contains("_")).Distinct().ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Snake).Methods(flags, "Move"); Assert.AreEqual(1, methods.Count); Assert.AreEqual(typeof(Snake), methods[0].DeclaringType); }
public void TestPropertyWithExcludeExplicitlyImplemented() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.ExcludeExplicitlyImplemented; // using explicit name PropertyInfo property = typeof(Giraffe).Property("FasterflectTest.SampleModel.Animals.Interfaces.ISwim.SwimDistance"); Assert.IsNotNull(property); property = typeof(Giraffe).Property("FasterflectTest.SampleModel.Animals.Interfaces.ISwim.SwimDistance", flags); Assert.IsNull(property); // using short name property = typeof(Giraffe).Property("SwimDistance", FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.TrimExplicitlyImplemented); Assert.IsNotNull(property); property = typeof(Giraffe).Property("SwimDistance", flags | FasterflectFlags.TrimExplicitlyImplemented); Assert.IsNull(property); }
public void TestPropertyByPartialName() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.PartialNameMatch; List <string> expectedNames = AnimalInstancePropertyNames.Where(s => s.Contains("C")).ToList(); PropertyInfo property = typeof(Animal).Property("C", flags); Assert.IsNotNull(property); string name = expectedNames.FirstOrDefault(n => property.Name == n); //Assert.AreEqual(name, property.Name); expectedNames = AnimalInstancePropertyNames.Where(s => s.Contains("B")).ToList(); property = typeof(Animal).Property("B", flags); Assert.IsNotNull(property); name = expectedNames.FirstOrDefault(n => property.Name == n); //Assert.AreEqual(name, property.Name); }
public void TestFindMethodsInstanceWithPartialNameMatch() { FasterflectFlags flags = FasterflectFlags.InstanceAnyVisibility | FasterflectFlags.PartialNameMatch; IList <MethodInfo> methods = typeof(Animal).Methods(flags, "B"); CollectionAssert.AreEquivalent(AnimalInstanceMethodNames.Where(s => s.Contains("B")).ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Reptile).Methods(flags, "et_"); CollectionAssert.AreEquivalent(ReptileInstanceMethodNames.Where(s => s.Contains("et_")).ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Snake).Methods(flags, "get", "C"); CollectionAssert.AreEquivalent(SnakeInstanceMethodNames.Where(s => s.Contains("get") || s.Contains("C")).ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Snake).Methods(flags, "_"); CollectionAssert.AreEquivalent(SnakeInstanceMethodNames.Where(s => s.Contains("_")).ToList(), methods.Select(m => m.Name).ToList()); methods = typeof(Snake).Methods(flags); CollectionAssert.AreEquivalent(SnakeInstanceMethodNames, methods.Select(m => m.Name.TrimExplicitlyImplementedName()).ToList()); }
/// <summary> /// This method applies method parameter type filtering to a set of methods. /// </summary> public static List <T> Filter <T>(this IEnumerable <T> methods, FasterflectFlags bindingFlags, Type[] paramTypes) where T : MethodBase { List <T> result = new List <T>(); bool exact = bindingFlags.IsSet(FasterflectFlags.ExactBinding); foreach (T method in methods) { // verify parameters ParameterInfo[] parameters = method.GetParameters(); if (parameters.Length != paramTypes.Length) { continue; } // verify parameter type compatibility bool match = true; for (int j = 0; j < paramTypes.Length; j++) { Type type = paramTypes[j]; ParameterInfo parameter = parameters[j]; Type parameterType = parameter.ParameterType; bool ignoreParameterModifiers = !exact; if (ignoreParameterModifiers && parameterType.IsByRef) { string name = parameterType.FullName; parameterType = Type.GetType(name.Substring(0, name.Length - 1)) ?? parameterType; } match &= parameterType.IsGenericParameter || parameterType.ContainsGenericParameters || (exact ? type == parameterType : parameterType.IsAssignableFrom(type)); if (!match) { break; } } if (match) { result.Add(method); } } return(result); }
public MultiSetCallInfo(Type targetType, FasterflectFlags bindingFlags, IList <string> members) { TargetType = targetType; Flags = bindingFlags; Members = members; }
/// <summary> /// Gets the field identified by <paramref name="name"/> on the given <paramref name="type"/>. /// Use the <paramref name="bindingFlags"/> parameter to define the scope of the search. /// </summary> /// <returns>A single FieldInfo instance of the first found match or null if no match was found.</returns> public static FieldInfo Field(this Type type, string name, FasterflectFlags bindingFlags) { return(ReflectLookup.Field(type, name, bindingFlags)); }
/// <summary> /// Gets all fields on the given <paramref name="type"/> that match the specified <paramref name="bindingFlags"/>. /// </summary> /// <param name="type">The type on which to reflect.</param> /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> combination used to define /// the search behavior and result filtering.</param> /// <param name="names">The optional list of names against which to filter the result. If this parameter is /// <see langword="null"/> or empty no name filtering will be applied. The default behavior is to check for an exact, /// case-sensitive match. Pass <see cref="FasterflectFlags.ExcludeExplicitlyImplemented"/> to exclude explicitly implemented /// interface members, <see cref="FasterflectFlags.PartialNameMatch"/> to locate by substring, and /// <see cref="FasterflectFlags.IgnoreCase"/> to ignore case.</param> /// <returns>A list of all matching fields on the type. This value will never be null.</returns> public static IList <FieldInfo> Fields(this Type type, FasterflectFlags bindingFlags, params string[] names) { return(ReflectLookup.Fields(type, bindingFlags, names)); }
/// <summary> /// Creates a delegate which can get the value of the field specified by <paramref name="name"/> and /// matching <paramref name="bindingFlags"/> on the given <paramref name="type"/>. /// </summary> public static MemberGetter DelegateForGetFieldValue(this Type type, string name, FasterflectFlags bindingFlags) { return(Reflect.FieldGetter(type, name, bindingFlags)); }
/// <summary> /// Creates a delegate which can set the value of the property specified by <param name="name"/> /// matching <param name="bindingFlags"/> on the given <param name="type"/>. /// </summary> public static MemberSetter DelegateForSetPropertyValue(this Type type, string name, FasterflectFlags bindingFlags) { return(Reflect.PropertySetter(type, name, bindingFlags)); }