public static TypeNameStyle Reduce(this TypeNameStyle style, int steps = 1) { var newStep = (int)style - steps; newStep = newStep.Clamp(0, BEnum.GetValues <TypeNameStyle>().Cast <int>().Max()); return((TypeNameStyle)newStep); }
/// <summary>Checks whether the given type is assignable to the given type name.</summary> /// <param name="type">The type.</param> /// <param name="typeName">Name of the type.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns></returns> public static bool IsAssignableTo(this Type type, string typeName, TypeNameStyle typeNameStyle) { if (typeNameStyle == TypeNameStyle.Name && type.Name == typeName) { return(true); } if (typeNameStyle == TypeNameStyle.FullName && type.FullName == typeName) { return(true); } return(type.InheritsFrom(typeName, typeNameStyle)); }
/// <summary>Checks whether the given type inherits from the given type name.</summary> /// <param name="type">The type.</param> /// <param name="typeName">Name of the type.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns>true if the type inherits from typeName.</returns> public static bool InheritsFrom(this Type type, string typeName, TypeNameStyle typeNameStyle) { var baseType = type.GetTypeInfo().BaseType; while (baseType != null) { if (typeNameStyle == TypeNameStyle.Name && baseType.Name == typeName) { return(true); } if (typeNameStyle == TypeNameStyle.FullName && baseType.FullName == typeName) { return(true); } baseType = baseType.GetTypeInfo().BaseType; } return(false); }
/// <summary>Checks whether the given type is assignable to the given type name.</summary> /// <param name="type">The type.</param> /// <param name="typeName">Name of the type.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns></returns> public static bool IsAssignableToTypeName(this Type type, string typeName, TypeNameStyle typeNameStyle) { if (typeNameStyle == TypeNameStyle.Name && type.Name == typeName) { return(true); } if (typeNameStyle == TypeNameStyle.FullName && type.FullName == typeName) { return(true); } return(type.InheritsFromTypeName(typeName, typeNameStyle) || #if NETSTANDARD1_0 (typeNameStyle == TypeNameStyle.Name && type.GetTypeInfo().ImplementedInterfaces.Any(i => i.Name == typeName)) || (typeNameStyle == TypeNameStyle.FullName && type.GetTypeInfo().ImplementedInterfaces.Any(i => i.FullName == typeName))); #else (typeNameStyle == TypeNameStyle.Name && type.GetInterfaces().Any(i => i.Name == typeName)) || (typeNameStyle == TypeNameStyle.FullName && type.GetInterfaces().Any(i => i.FullName == typeName)); #endif }
/// <summary>Tries to get the first object which is assignable to the given type name.</summary> /// <param name="attributes">The attributes.</param> /// <param name="typeName">Type of the attribute.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns>May return null (not found).</returns> public static T TryGetIfAssignableTo <T>(this IEnumerable <T> attributes, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName) { return(attributes != null? attributes.FirstOrDefault(a => a.GetType().IsAssignableTo(typeName, typeNameStyle)) : default(T)); }
/// <summary>Tries to get the first object of the given type name.</summary> /// <param name="attributes">The attributes.</param> /// <param name="typeName">Type of the attribute.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns>May return null.</returns> public static T TryGetByObjectType <T>(this IEnumerable <T> attributes, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName) { return(attributes.FirstOrDefault(a => a.GetType().FullName == typeName)); }
/// <summary>Tries to get the first object which is assignable to the given type name.</summary> /// <param name="objects">The objects.</param> /// <param name="typeName">Type of the attribute.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns>May return null (not found).</returns> public static T?FirstAssignableToTypeNameOrDefault <T>(this IEnumerable <T>?objects, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName) { return(objects != null? objects.GetAssignableToTypeName(typeName, typeNameStyle).FirstOrDefault() :
/// <summary>Tries to get the first object which is assignable to the given type name.</summary> /// <param name="objects">The objects.</param> /// <param name="typeName">Type of the attribute.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns>The objects which are assignable.</returns> public static IEnumerable <T> GetAssignableToTypeName <T>(this IEnumerable <T> objects, string typeName, TypeNameStyle typeNameStyle = TypeNameStyle.FullName) { return(objects.Where(o => o !.GetType().IsAssignableToTypeName(typeName, typeNameStyle))); }
/// <summary>Checks whether the given type is assignable to the given type name.</summary> /// <param name="type">The type.</param> /// <param name="typeName">Name of the type.</param> /// <param name="typeNameStyle">The type name style.</param> /// <returns></returns> public static bool IsAssignableToTypeName(this CachedType type, string typeName, TypeNameStyle typeNameStyle) { return(type.OriginalType.IsAssignableToTypeName(typeName, typeNameStyle)); }