public static IEnumerable<Type> TypesToGenerateForType(Type type) { foreach (var interfaceType in type.FindInterfaces((ignored, data) => true, null)) { yield return interfaceType; } yield return type; }
public RoleSet(Type _Class) { TypeFilter filter = new TypeFilter(RoleFilter); Type[] Roles = _Class.FindInterfaces(filter, (object)__RoleTypes); _Set = new HashSet<Type>(); foreach (Type r in Roles) { _Set.Add(r); } }
/// <summary> /// Returns an array of System.Type objects representing a filtered list of interfaces // implemented or inherited by the specified type. public static bool ImplementsInterface(this System.Type type, System.Type interfaceType) { if (!interfaceType.IsInterface) { throw new Exception("Expected an interface type."); } return(type.FindInterfaces((i, j) => Equals(i, j) || i.IsSubclassOf((System.Type)j), interfaceType).Length != 0); }
public static bool TypeImplementsInterface(Type type, Type typeToImplement) { Type[] interfaces = type.FindInterfaces( (typ, obj) => typ == typeToImplement, type); if (interfaces != null && interfaces.Length > 0) return true; return false; }
/// <summary> /// InterfacePresentInClass check that an inteface is implimented for the specific dll type /// </summary> /// <param name="checkType">The type to check i.e the class</param> /// <param name="compareType">The interface type</param> /// <returns>Returns true if the interface is implemented in the class</returns> public static bool InterfacePresentInType(Type checkType, Type compareType) { TypeFilter interfaceFilter = InterfaceFilter; Type[] list = checkType.FindInterfaces(interfaceFilter, compareType); if (list != null && list.Length > 0) return true; return false; }
private static Type FindCollectionInterface(Type type) { Type[] array = type.FindInterfaces(new TypeFilter(CollectionUtils.IsCollectionInterface), null); if (array.Length == 1) { return array[0]; } return null; }
public static bool ImplementsInterface <T>(this Type type) { Type interfaceType = typeof(T); if (!interfaceType.IsInterface) { throw new InvalidOperationException("The type specified is not an interface type."); } Type[] interfaces = type.FindInterfaces(new TypeFilter((a, b) => a == interfaceType), null); return(interfaces != null && interfaces.Length != 0); }
internal static System.Type GetGenericInterfaceElementType(System.Type type, TypeFilter typeFilter) { if (typeFilter(type, null)) { return(type.GetGenericArguments()[0]); } System.Type[] typeArray = type.FindInterfaces(typeFilter, null); if ((typeArray != null) && (typeArray.Length == 1)) { return(typeArray[0].GetGenericArguments()[0]); } return(null); }
private Type GetInterfaceType(Type type) { lock (_cacheLock) { Type interfaceType; if (!_cache.TryGetValue(type, out interfaceType)) { _cache[type] = interfaceType = type.FindInterfaces((t, o) => ((Type[])o).All(c => t == c || !t.IsAssignableFrom(c)), type.GetInterfaces()).Single(); } return interfaceType; } }
private static Type ElementType(Type collectionType) { Debug.Assert(collectionType != null); Debug.Assert(typeof(IEnumerable).IsAssignableFrom(collectionType)); Type[] stronglyTypedInterfaces = collectionType.FindInterfaces((m, c) => m.IsConstructedGenericType && m.GetGenericTypeDefinition() == typeof(IEnumerable<>), null); if (stronglyTypedInterfaces == null || stronglyTypedInterfaces.Length == 0 || stronglyTypedInterfaces.Length > 1) { return null; // not a strongly typed collection, or a strongly typed collection of more than one thing (yikes) } return stronglyTypedInterfaces[0].GetGenericArguments()[0]; }
public static Type FindGenericInterface(Type gType, Type gInterface) { // maybe gType is generic interface? if (gType.IsGenericType && gType.GetGenericTypeDefinition() == gInterface) return gType; string interfaceName = gInterface.Name; Type[] foundInterfaces = gType.FindInterfaces(Module.FilterTypeName, interfaceName); if (foundInterfaces != null) for (int i = 0; i < foundInterfaces.Length; i++) { Type deff = foundInterfaces[i].GetGenericTypeDefinition(); if (deff == gInterface) return foundInterfaces[i]; } return null; }
public static PropertyInfo[] GetProperties(Type IType, Type clazz) { PropertyInfo[] pi = IType.GetProperties(); Type[] interfaces = clazz.FindInterfaces(new TypeFilter(MyInterfaceFilter), IType); if (interfaces.Length == 0) return pi; PropertyInfo[] piChild = interfaces[0].GetProperties(); if (piChild.Length == 0) return pi; PropertyInfo[] res = new PropertyInfo[pi.Length + piChild.Length]; pi.CopyTo(res, 0); piChild.CopyTo(res, pi.Length); return res; }
/// <summary> /// true if type is array, IEnumerable, etc /// </summary> /// <param name="type"></param> /// <returns></returns> public static bool IsCollectionType(Type type) { if (type.IsArray) return true; if (type == typeof(IList)) return true; if (type == typeof(string)) return false; if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof (IEnumerable<>)) return true; var types = type.FindInterfaces((typ, cri) => ( typ.IsGenericType && typ.GetGenericTypeDefinition() == typeof(IEnumerable<>)), null); return types.Length != 0; } return false; }
/// <summary> /// 泛型匹配 /// </summary> private static bool MatchGeneric(MicrosoftSystem.Type findType, MicrosoftSystem.Type type) { if (findType.IsGenericTypeDefinition == false) { return(false); } var definition = findType.GetGenericTypeDefinition(); foreach (var implementedInterface in type.FindInterfaces((filter, criteria) => true, null)) { if (implementedInterface.IsGenericType == false) { continue; } return(definition.IsAssignableFrom(implementedInterface.GetGenericTypeDefinition())); } return(false); }
protected void CollectMethodsToProxy(ArrayList methodList, Type type, bool onlyVirtuals) { CollectMethods(methodList, type, onlyVirtuals); if (type.IsInterface) { Type[] typeChain = type.FindInterfaces(new TypeFilter(NoFilter), null); foreach(Type interType in typeChain) { CollectMethods(methodList, interType, onlyVirtuals); } } }
private static MethodInfo FindInterfaceMethod( Type targetType, Type interfaceType, string name, Type[] parameterTypes ) { if ( targetType.GetIsInterface() ) { return targetType.FindInterfaces( ( type, _ ) => type == interfaceType, null ).Single().GetMethod( name, parameterTypes ); } var map = targetType.GetInterfaceMap( interfaceType ); #if !SILVERLIGHT int index = Array.FindIndex( map.InterfaceMethods, method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) ); #else int index = map.InterfaceMethods.FindIndex( method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) ); #endif if ( index < 0 ) { #if DEBUG Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join( ", ", parameterTypes.Select( t => t.ToString() ).ToArray() ) + ") is not found in " + targetType ); #endif return null; } else { return map.TargetMethods[ index ]; } }
/// <summary> /// Returns the "T" in the IQueryable of T implementation of type. /// </summary> /// <param name="type">Type to check.</param> /// <param name="typeFilter">filter against which the type is checked</param> /// <returns> /// The element type for the generic IQueryable interface of the type, /// or null if it has none or if it's ambiguous. /// </returns> internal static Type GetGenericInterfaceElementType(Type type, TypeFilter typeFilter) { Debug.Assert(type != null, "type != null"); Debug.Assert(!type.IsGenericTypeDefinition, "!type.IsGenericTypeDefinition"); if (typeFilter(type, null)) { return type.GetGenericArguments()[0]; } Type[] queriables = type.FindInterfaces(typeFilter, null); if (queriables != null && queriables.Length == 1) { return queriables[0].GetGenericArguments()[0]; } else { return null; } }
/// <summary> /// Selects the range of all the lists that are keyed on the type /// name given or the name of any parent class or interface. For /// example, giving typeof(string) will return any for /// "System.String" or "System.Object" and so on. /// </summary> public IList Select(Type type) { // Create a list ArrayList list = new ArrayList(); // Ignore nulls (since that happens at the top-most object) if (type == null) return list; // Add this type's keys IList l = (IList) lists[type.FullName]; if (l != null) list.AddRange(l); // Add the base types list.AddRange(Select(type.BaseType)); // Add the interfaces, if any Type [] interfaces = type.FindInterfaces(new TypeFilter(FindAllTypes), null); foreach (Type iType in interfaces) list.AddRange(Select(iType)); // Return the list return list; }
/// <summary> /// Finds and returns the constructed type of an interface /// generic type definition if the type implements it. Otherwise /// returns a <c>null</c> reference. /// </summary> /// <exception cref="System.ArgumentNullException"> /// Either <paramref name="type"/> or <paramref name="genericTypeDefinition"/> /// is a null reference. /// </exception> internal static Type FindConstructionOfGenericInterfaceDefinition(Type type, Type genericTypeDefinition) { if (type == null) throw new ArgumentNullException("type"); if (genericTypeDefinition == null) throw new ArgumentNullException("genericTypeDefinition"); Type[] interfaces = type.FindInterfaces(new TypeFilter(IsConstructionOfGenericInterfaceDefinition), genericTypeDefinition); return interfaces.Length == 0 ? null : interfaces[0]; }
/// <summary> /// Registers the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="locator">The locator.</param> public void Register(Type type, IServiceLocator locator) { var interfaces = type.FindInterfaces((t, obj) => _binding.Invoke(t, type), null); if (interfaces.Length > 0) Register(interfaces[0], type, locator); }
private static Type GetListItemType(Type listType) { var listInterface = listType.FindInterfaces((type, args) => type.IsGenericType && typeof (IList<>) == type.GetGenericTypeDefinition(), null).FirstOrDefault(); if (null == listInterface) { return null; } return listInterface.GetGenericArguments()[0]; }
private static MethodInfo FindInterfaceMethod( Type targetType, Type interfaceType, string name, Type[] parameterTypes ) { if ( targetType.GetIsInterface() ) { return targetType.FindInterfaces( ( type, _ ) => type == interfaceType, null ).Single().GetMethod( name, parameterTypes ); } var map = targetType.GetInterfaceMap( interfaceType ); #if !SILVERLIGHT || WINDOWS_PHONE int index = Array.FindIndex( map.InterfaceMethods, method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) ); #else int index = map.InterfaceMethods.FindIndex( method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) ); #endif if ( index < 0 ) { #if DEBUG && !UNITY #if !NETFX_35 Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join<Type>( ", ", parameterTypes ) + ") is not found in " + targetType ); #else Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join( ", ", parameterTypes.Select( t => t.ToString() ).ToArray() ) + ") is not found in " + targetType ); #endif // !NETFX_35 #endif // DEBUG && !UNITY // ReSharper disable once HeuristicUnreachableCode return null; } return map.TargetMethods[ index ]; }
public static bool InterfacePresentInType(Type checkType, Type compareType) { TypeFilter filter = new TypeFilter(ReflectionHelper.InterfaceFilter); Type[] typeArray = checkType.FindInterfaces(filter, compareType); return ((typeArray != null) && (typeArray.Length > 0)); }
private static Type GetAppControlType(Type launchableType, IEnumerable<Assembly> assemblies) { var types = launchableType.FindInterfaces(filter, null).Where(type => type.Assembly == typeof(IListViewData).Assembly); //TODO: instead of assuming the first... look for a hierarchy // if (types.Count() != 1) throw new Exception("Currently only supporting a DataProcessor to implement a single AppControlInterface"); var appControlInterfaceType = types.First(); //from there, look in assemblies for appControlInterfaceType return assemblies .Select(assembly => GetAppControlType(assembly, appControlInterfaceType, launchableType)) .SingleOrDefault(); }
private static bool IsTypeOfIControlMapper(Type mapperType) { return mapperType != null && mapperType.FindInterfaces ((type, filterCriteria) => type == typeof (IControlMapper), "").Length > 0; }
protected virtual bool DoesTypeImplementOpenGeneric(Type type, Type openGeneric) { try { var genericTypeDefinition = openGeneric.GetGenericTypeDefinition(); foreach (var implementedInterface in type.FindInterfaces((objType, objCriteria) => true, null)) { if (!implementedInterface.IsGenericType) continue; var isMatch = genericTypeDefinition.IsAssignableFrom(implementedInterface.GetGenericTypeDefinition()); return isMatch; } return false; }catch { return false; } }
protected void CollectPropertyMethodsToProxy( ArrayList methodList, Type type, bool onlyVirtuals, ClassEmitter emitter, out PropertyToGenerate[] propsToGenerate) { if (type.IsInterface) { ArrayList toGenerateList = new ArrayList(); toGenerateList.AddRange(CollectProperties(methodList, type, onlyVirtuals, emitter)); Type[] typeChain = type.FindInterfaces(new TypeFilter(NoFilter), null); foreach(Type interType in typeChain) { toGenerateList.AddRange(CollectProperties(methodList, interType, onlyVirtuals, emitter)); } propsToGenerate = (PropertyToGenerate[]) toGenerateList.ToArray(typeof(PropertyToGenerate)); } else { propsToGenerate = CollectProperties(methodList, type, onlyVirtuals, emitter); } }
private static bool HasInterface(Type type, string name) { TypeFilter typeFilter = new TypeFilter(InterfaceFilter); Type[] interfaces = type.FindInterfaces(typeFilter, name); if (interfaces.Length != 1) return false; if (! interfaces[0].IsInterface) // should not occur return false; return true; }
private void CollectEventMethodsToProxy( ArrayList methodList, Type type, bool onlyVirtuals, ClassEmitter emitter, out EventToGenerate[] eventsToGenerates) { if (type.IsInterface) { ArrayList toGenerateList = new ArrayList(); toGenerateList.AddRange(CollectEvents(methodList, type, onlyVirtuals, emitter)); Type[] typeChain = type.FindInterfaces(new TypeFilter(NoFilter), null); foreach(Type interType in typeChain) { toGenerateList.AddRange(CollectEvents(methodList, interType, onlyVirtuals, emitter)); } eventsToGenerates = (EventToGenerate[]) toGenerateList.ToArray(typeof(EventToGenerate)); } else { eventsToGenerates = CollectEvents(methodList, type, onlyVirtuals, emitter); } }
/// <summary> /// Iterates over the interfaces and generate implementation /// for each method in it. /// </summary> /// <param name="type">Type class</param> /// <param name="ignoreInterfaces">if true, we inspect the /// type for implemented interfaces</param> protected void GenerateTypeImplementation(Type type, bool ignoreInterfaces) { if (_generated.Contains(type)) return; if (Context.ShouldSkip(type)) return; _generated.Add(type); if (!ignoreInterfaces) { Type[] baseInterfaces = type.FindInterfaces(new TypeFilter(NoFilterImpl), type); GenerateInterfaceImplementation(baseInterfaces); } EasyProperty[] properties = GenerateProperties(type); GenerateMethods(type, properties); }
/// <summary> /// Iterates over the interfaces and generate implementation /// for each method in it. /// </summary> /// <param name="type">Type class</param> /// <param name="ignoreInterfaces">Interface type</param> protected void GenerateTypeImplementation(Type type, bool ignoreInterfaces) { if (m_generated.Contains(type)) { return; } else { m_generated.Add(type); } if (!ignoreInterfaces) { Type[] baseInterfaces = type.FindInterfaces(new TypeFilter(NoFilterImpl), type); GenerateInterfaceImplementation(baseInterfaces); } PropertyBuilder[] propertiesBuilder = GenerateProperties(type); GenerateMethods(type, propertiesBuilder); }
public static Type GetIEnumerableImpl(Type type) { // Get IEnumerable implementation. Either type is IEnumerable<T> for some T, // or it implements IEnumerable<T> for some T. We need to find the interface. if (IsIEnumerable(type)) { return type; } var interfaces = type.FindInterfaces((m, o) => IsIEnumerable(m), null); return interfaces.First(); }
private static Type[] findGenericTypesForDefinition(Type type, Type genericTypeDefinition) { if (type == null) throw new ArgumentNullException("type"); if (genericTypeDefinition == null) throw new ArgumentNullException("genericTypeDefinition"); if (!genericTypeDefinition.IsGenericTypeDefinition) { throw new ArgumentException( string.Format( "The type '{0}' is not a generic type definition.", genericTypeDefinition.FullName), "genericTypeDefinition"); } Type matchingBaseType = getTypeHierarchy(type) .Reverse() .Where(t => t.IsGenericType && genericTypeDefinition == t.GetGenericTypeDefinition()) .FirstOrDefault(); if (matchingBaseType != null) { return new[] { matchingBaseType }; } TypeFilter filter = (i, o) => i.IsGenericType && genericTypeDefinition == i.GetGenericTypeDefinition(); return type .FindInterfaces(filter, null) .ToArray(); }
private static HashSet<Type> HandlerMessages(Type self) { var findInterfaces = self.FindInterfaces((@interface, nomatter) => @interface.IsGenericType && @interface.GetGenericTypeDefinition() == typeof (IHandler<>), null); var messages = findInterfaces.Select(x => x.GetGenericArguments()[0]); return new HashSet<Type>(messages); }