本文整理汇总了C#中System.Type.IsGenericType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsGenericType方法的具体用法?C# Type.IsGenericType怎么用?C# Type.IsGenericType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsGenericType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanConvert
public override bool CanConvert(Type objectType)
{
var canConvert = objectType.IsGenericType() &&
objectType.GetGenericTypeDefinition() == typeof(GroupedResult<,>);
return canConvert;
}
示例2: FindIEnumerable
private static Type FindIEnumerable(Type seqType)
{
if (seqType == null || seqType == typeof(string))
return null;
if (seqType.IsArray)
return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType());
if (seqType.IsGenericType())
{
foreach (Type arg in seqType.GetGenericArguments())
{
Type ienum = typeof(IEnumerable<>).MakeGenericType(arg);
if (ienum.IsAssignableFrom(seqType))
return ienum;
}
}
var ifaces = seqType.GetInterfaces();
if (ifaces != null && ifaces.Any())
{
foreach (Type iface in ifaces)
{
Type ienum = FindIEnumerable(iface);
if (ienum != null)
return ienum;
}
}
if (seqType.BaseType() != null && seqType.BaseType() != typeof(object))
return FindIEnumerable(seqType.BaseType());
return null;
}
示例3: GetElementType
public static Type GetElementType(Type enumerableType, IEnumerable enumerable)
{
if (enumerableType.HasElementType)
{
return enumerableType.GetElementType();
}
if (enumerableType.IsGenericType() &&
enumerableType.GetGenericTypeDefinition() == typeof (IEnumerable<>))
{
return enumerableType.GetTypeInfo().GenericTypeArguments[0];
}
Type ienumerableType = GetIEnumerableType(enumerableType);
if (ienumerableType != null)
{
return ienumerableType.GetTypeInfo().GenericTypeArguments[0];
}
if (typeof (IEnumerable).IsAssignableFrom(enumerableType))
{
var first = enumerable?.Cast<object>().FirstOrDefault();
return first?.GetType() ?? typeof (object);
}
throw new ArgumentException($"Unable to find the element type for type '{enumerableType}'.", nameof(enumerableType));
}
示例4: GetFullNameWithoutVersionInformation
/// <summary>
/// Gets the full name without version information.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns></returns>
public static string GetFullNameWithoutVersionInformation(Type entityType)
{
string result;
var localFullName = fullnameCache;
if (localFullName.TryGetValue(entityType, out result))
return result;
var asmName = new AssemblyName(entityType.Assembly().FullName).Name;
if (entityType.IsGenericType())
{
var genericTypeDefinition = entityType.GetGenericTypeDefinition();
var sb = new StringBuilder(genericTypeDefinition.FullName);
sb.Append("[");
foreach (var genericArgument in entityType.GetGenericArguments())
{
sb.Append("[")
.Append(GetFullNameWithoutVersionInformation(genericArgument))
.Append("]");
}
sb.Append("], ")
.Append(asmName);
result = sb.ToString();
}
else
{
result = entityType.FullName + ", " + asmName;
}
fullnameCache = new Dictionary<Type, string>(localFullName)
{
{entityType, result}
};
return result;
}
示例5: IsGenericTypeOf
public static bool IsGenericTypeOf(this Type t, Type genericDefinition, out Type[] genericParameters)
{
genericParameters = new Type[] { };
if (!genericDefinition.IsGenericType())
{
return false;
}
var isMatch = t.IsGenericType() && t.GetGenericTypeDefinition() == genericDefinition.GetGenericTypeDefinition();
if (!isMatch && t.GetBaseType() != null)
{
isMatch = IsGenericTypeOf(t.GetBaseType(), genericDefinition, out genericParameters);
}
if (!isMatch && genericDefinition.IsInterface() && t.GetInterfaces().Any())
{
foreach (var i in t.GetInterfaces())
{
if (i.IsGenericTypeOf(genericDefinition, out genericParameters))
{
isMatch = true;
break;
}
}
}
if (isMatch && !genericParameters.Any())
{
genericParameters = t.GetGenericArguments();
}
return isMatch;
}
示例6: InvocationWithGenericDelegateContributor
public InvocationWithGenericDelegateContributor(Type delegateType, MetaMethod method, Reference targetReference)
{
Debug.Assert(delegateType.IsGenericType(), "delegateType.IsGenericType");
this.delegateType = delegateType;
this.method = method;
this.targetReference = targetReference;
}
示例7: JsonArrayContract
/// <summary>
/// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
public JsonArrayContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Array;
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
{
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
}
else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
_genericCollectionDefinitionType = typeof (IEnumerable<>);
CollectionItemType = underlyingType.GetGenericArguments()[0];
}
else
{
CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
}
if (CollectionItemType != null)
_isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
if (IsTypeGenericCollectionInterface(UnderlyingType))
{
CreatedType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType);
}
}
示例8: IsTypeExcluded
/// <summary>
/// Returns true if the given type will be excluded from the default model validation.
/// </summary>
public bool IsTypeExcluded(Type type)
{
Type[] actualTypes;
if (type.IsGenericType() &&
type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
{
actualTypes = type.GenericTypeArguments;
}
else
{
actualTypes = new Type[] { type };
}
foreach (var actualType in actualTypes)
{
var underlyingType = Nullable.GetUnderlyingType(actualType) ?? actualType;
if (!IsSimpleType(underlyingType))
{
return false;
}
}
return true;
}
示例9: MakeTypePartiallyGenericUpToLevel
// This method takes generic type and returns a 'partially' generic type definition of that same type,
// where all generic arguments up to the given nesting level. This allows us to group generic types
// by their partial generic type definition, which allows a much nicer user experience.
internal static Type MakeTypePartiallyGenericUpToLevel(Type type, int nestingLevel)
{
if (nestingLevel > 100)
{
// Stack overflow prevention
throw new ArgumentException("nesting level bigger than 100 too high. Type: " +
type.ToFriendlyName(), nameof(nestingLevel));
}
// example given type: IEnumerable<IQueryProcessor<MyQuery<Alpha>, int[]>>
// nestingLevel 4 returns: IEnumerable<IQueryHandler<MyQuery<Alpha>, int[]>
// nestingLevel 3 returns: IEnumerable<IQueryHandler<MyQuery<Alpha>, int[]>
// nestingLevel 2 returns: IEnumerable<IQueryHandler<MyQuery<T>, int[]>
// nestingLevel 1 returns: IEnumerable<IQueryHandler<TQuery, TResult>>
// nestingLevel 0 returns: IEnumerable<T>
if (!type.IsGenericType())
{
return type;
}
if (nestingLevel == 0)
{
return type.GetGenericTypeDefinition();
}
return MakeTypePartiallyGeneric(type, nestingLevel);
}
示例10: JsonArrayContract
/// <summary>
/// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
/// </summary>
/// <param name="underlyingType">The underlying type for the contract.</param>
public JsonArrayContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Array;
if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection<>), out _genericCollectionDefinitionType))
{
CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0];
}
else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
_genericCollectionDefinitionType = typeof (IEnumerable<>);
CollectionItemType = underlyingType.GetGenericArguments()[0];
}
else
{
CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType);
}
if (CollectionItemType != null)
_isCollectionItemTypeNullableType = ReflectionUtils.IsNullableType(CollectionItemType);
if (IsTypeGenericCollectionInterface(UnderlyingType))
CreatedType = ReflectionUtils.MakeGenericType(typeof(List<>), CollectionItemType);
#if !(PORTABLE || NET20 || NET35 || WINDOWS_PHONE)
else if (IsTypeGenericSetnterface(UnderlyingType))
CreatedType = ReflectionUtils.MakeGenericType(typeof(HashSet<>), CollectionItemType);
#endif
IsMultidimensionalArray = (UnderlyingType.IsArray && UnderlyingType.GetArrayRank() > 1);
}
示例11: Bind
/// <summary>
/// Bind to the given model type
/// </summary>
/// <param name="context">Current context</param>
/// <param name="modelType">Model type to bind to</param>
/// <param name="instance">Optional existing instance</param>
/// <param name="configuration">The <see cref="BindingConfig" /> that should be applied during binding.</param>
/// <param name="blackList">Blacklisted property names</param>
/// <returns>Bound model</returns>
public object Bind(NancyContext context, Type modelType, object instance, BindingConfig configuration,
params string[] blackList)
{
Type genericType = null;
if (modelType.IsArray() || modelType.IsCollection() || modelType.IsEnumerable())
{
//make sure it has a generic type
if (modelType.IsGenericType())
{
genericType = modelType.GetGenericArguments().FirstOrDefault();
}
else
{
var implementingIEnumerableType =
modelType.GetInterfaces().Where(i => i.IsGenericType()).FirstOrDefault(
i => i.GetGenericTypeDefinition() == typeof (IEnumerable<>));
genericType = implementingIEnumerableType == null ? null : implementingIEnumerableType.GetGenericArguments().FirstOrDefault();
}
if (genericType == null)
{
throw new ArgumentException("When modelType is an enumerable it must specify the type", "modelType");
}
}
var bindingContext =
CreateBindingContext(context, modelType, instance, configuration, blackList, genericType);
var bodyDeserializedModel = DeserializeRequestBody(bindingContext);
return (instance as IEnumerable<string>) ?? bodyDeserializedModel;
}
示例12: IsNullableType
/// <summary>Checks whether the specified type is a generic nullable type.</summary>
/// <param name="type">Type to check.</param>
/// <returns>true if <paramref name="type"/> is nullable; false otherwise.</returns>
internal static bool IsNullableType(Type type)
{
DebugUtils.CheckNoExternalCallers();
//// This is a copy of WebUtil.IsNullableType from the product.
return type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
示例13: GetClosedContainerControlledItemsFor
private ContainerControlledItem[] GetClosedContainerControlledItemsFor(Type serviceType)
{
var items = this.GetItemsFor(serviceType);
return serviceType.IsGenericType()
? Helpers.GetClosedGenericImplementationsFor(serviceType, items)
: items.ToArray();
}
示例14: InvocationWithDelegateContributor
public InvocationWithDelegateContributor(Type delegateType, Type targetType, MetaMethod method,
INamingScope namingScope)
{
Debug.Assert(delegateType.IsGenericType() == false, "delegateType.IsGenericType == false");
this.delegateType = delegateType;
this.targetType = targetType;
this.method = method;
this.namingScope = namingScope;
}
示例15: FormatType
private string FormatType(Type type)
{
var typeName = type.Name;
if (!type.IsGenericType()) return typeName;
typeName = typeName.Substring(0, typeName.IndexOf('`'));
var genericArgTypes = type.GetGenericArguments().Select(x => FormatType(x));
return string.Format("{0}<{1}>", typeName, string.Join(", ", genericArgTypes.ToArray()));
}