本文整理汇总了C#中Type.IsGenericType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsGenericType方法的具体用法?C# Type.IsGenericType怎么用?C# Type.IsGenericType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsGenericType方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSetElementType
// <summary>
// Given a type that might be an IDbSet\IObjectSet, determine if the type implements IDbSet<>\IObjectSet<>, and if
// so return the element type of the IDbSet\IObjectSet. Currently, if the collection implements IDbSet<>\IObjectSet<>
// multiple times with different types, then we will return false since this is not supported.
// </summary>
// <param name="setType"> The type to check. </param>
// <returns> The element type of the IDbSet\IObjectSet, or null if the type does not match. </returns>
private static Type GetSetElementType(Type setType)
{
// We have to check if the type actually is the interface, or if it implements the interface:
try
{
var setInterface =
(setType.IsGenericType() && typeof(IDbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()))
? setType
: setType.GetInterface(typeof(IDbSet<>).FullName);
// We need to make sure the type is fully specified otherwise we won't be able to add element to it.
if (setInterface != null
&& !setInterface.ContainsGenericParameters())
{
return setInterface.GetGenericArguments()[0];
}
}
catch (AmbiguousMatchException)
{
// Thrown if collection type implements IDbSet or IObjectSet<> more than once
}
return null;
}
示例2: GetCastTargetType
// <summary>
// Gets the target type for a CQT cast operation.
// </summary>
// <returns> Appropriate type usage, or null if this is a "no-op" </returns>
private TypeUsage GetCastTargetType(TypeUsage fromType, Type toClrType, Type fromClrType, bool preserveCastForDateTime)
{
// An IQueryable can report its type as ObjectQuery, IQueryable, or IOrderedQueryable depending on how the type and
// expression tree were created. At this point in the translation, unwrapping of the DbQuery to ObjectQuery has already
// happened and checking for something other than ObjectQuery has already been done. Therefore, from a CQT translation
// perspective we can treat all these types as the same and this therefore becomes a no-op.
if (fromClrType != null
&& fromClrType.IsGenericType()
&& toClrType.IsGenericType()
&& (fromClrType.GetGenericTypeDefinition() == typeof(ObjectQuery<>)
|| fromClrType.GetGenericTypeDefinition() == typeof(IQueryable<>)
|| fromClrType.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>))
&& (toClrType.GetGenericTypeDefinition() == typeof(ObjectQuery<>)
|| toClrType.GetGenericTypeDefinition() == typeof(IQueryable<>)
|| toClrType.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>))
&& fromClrType.GetGenericArguments()[0] == toClrType.GetGenericArguments()[0])
{
return null;
}
//ignore System.Enum
if (fromClrType != null
&& TypeSystem.GetNonNullableType(fromClrType).IsEnum
&& toClrType == typeof(Enum))
{
return null;
}
// If the types are the same or the fromType is assignable to toType, return null
// (indicating no cast is required)
TypeUsage toType;
if (TryGetValueLayerType(toClrType, out toType)
&& CanOmitCast(fromType, toType, preserveCastForDateTime))
{
return null;
}
// Check that the cast is supported and adjust the target type as necessary.
toType = ValidateAndAdjustCastTypes(toType, fromType, toClrType, fromClrType);
return toType;
}
示例3: IsTypeNullable
private static bool IsTypeNullable(Type type)
{
return !type.IsValueType() ||
(type.IsGenericType() &&
type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
示例4: AppendCanonicalTypeDescription
private static void AppendCanonicalTypeDescription(
Type type, Dictionary<Type, int> genericArgumentOrdinals, StringBuilder description)
{
int ordinal;
// if this a type argument for the method, substitute
if (null != genericArgumentOrdinals
&& genericArgumentOrdinals.TryGetValue(type, out ordinal))
{
description.Append("T").Append(ordinal.ToString(CultureInfo.InvariantCulture));
return;
}
// always include the name (note: we omit the namespace/assembly; assuming type names do not collide)
description.Append(type.Name);
if (type.IsGenericType())
{
description.Append("<");
var first = true;
foreach (var genericArgument in type.GetGenericArguments())
{
if (first)
{
first = false;
}
else
{
description.Append(", ");
}
AppendCanonicalTypeDescription(genericArgument, genericArgumentOrdinals, description);
}
description.Append(">");
}
}
示例5: GetSubstituteDataContractType
internal static Type GetSubstituteDataContractType(Type type, out bool isQueryable)
{
if (type == s_typeOfIQueryable)
{
isQueryable = true;
return s_typeOfIEnumerable;
}
if (type.IsGenericType() &&
type.GetGenericTypeDefinition() == s_typeOfIQueryableGeneric)
{
isQueryable = true;
return s_typeOfIEnumerableGeneric.MakeGenericType(type.GetGenericArguments());
}
isQueryable = false;
return type;
}
示例6: IsNullableType
static bool IsNullableType(Type type)
{
return type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
示例7: FindGenericType
static Type FindGenericType(Type generic, Type type)
{
while (type != null && type != typeof(object))
{
if (type.IsGenericType() && type.GetGenericTypeDefinition() == generic)
return type;
if (generic.IsInterface())
{
foreach (Type intfType in type.GetInterfaces())
{
Type found = FindGenericType(generic, intfType);
if (found != null) return found;
}
}
type = type.BaseType();
}
return null;
}
示例8: TryGetLoadedType
// <summary>
// Check to see if the type is already loaded - either in the typesInLoading, or ObjectItemCollection or
// in the global cache
// </summary>
private bool TryGetLoadedType(Type clrType, out EdmType edmType)
{
if (SessionData.TypesInLoading.TryGetValue(clrType.FullName, out edmType)
||
TryGetCachedEdmType(clrType, out edmType))
{
// Check to make sure the CLR type we got is the same as the given one
if (edmType.ClrType != clrType)
{
SessionData.EdmItemErrors.Add(
new EdmItemError(
Strings.NewTypeConflictsWithExistingType(
clrType.AssemblyQualifiedName, edmType.ClrType.AssemblyQualifiedName)));
edmType = null;
return false;
}
return true;
}
// Let's check to see if this type is a ref type, a nullable type, or a collection type, these are the types that
// we need to take special care of them
if (clrType.IsGenericType())
{
var genericType = clrType.GetGenericTypeDefinition();
// Try to resolve the element type into a type object
EdmType elementType;
if (!TryGetLoadedType(clrType.GetGenericArguments()[0], out elementType))
{
return false;
}
if (typeof(IEnumerable).IsAssignableFrom(clrType))
{
var entityType = elementType as EntityType;
if (entityType == null)
{
// return null and let the caller deal with the error handling
return false;
}
edmType = entityType.GetCollectionType();
}
else
{
edmType = elementType;
}
return true;
}
edmType = null;
return false;
}
示例9: TrackClosure
private void TrackClosure(Type type)
{
if (SourceAssembly != type.Assembly()
&& !CacheEntry.ClosureAssemblies.Contains(type.Assembly())
&& IsSchemaAttributePresent(type.Assembly())
&& !(type.IsGenericType() &&
(
EntityUtil.IsAnICollection(type) || // EntityCollection<>, List<>, ICollection<>
type.GetGenericTypeDefinition() == typeof(EntityReference<>) ||
type.GetGenericTypeDefinition() == typeof(Nullable<>)
)
)
)
{
CacheEntry.ClosureAssemblies.Add(type.Assembly());
}
if (type.IsGenericType())
{
foreach (var genericArgument in type.GetGenericArguments())
{
TrackClosure(genericArgument);
}
}
}
示例10: LoadType
// <summary>
// Load metadata of the given type - when you call this method, you should check and make sure that the type has
// edm attribute. If it doesn't,we won't load the type and it will be returned as null
// </summary>
private void LoadType(Type clrType)
{
Debug.Assert(clrType.Assembly() == SourceAssembly, "Why are we loading a type that is not in our assembly?");
Debug.Assert(!SessionData.TypesInLoading.ContainsKey(clrType.FullName), "Trying to load a type that is already loaded???");
Debug.Assert(!clrType.IsGenericType(), "Generic type is not supported");
EdmType edmType = null;
var typeAttributes = clrType.GetCustomAttributes<EdmTypeAttribute>(inherit: false);
// the CLR doesn't allow types to have duplicate/multiple attribute declarations
if (typeAttributes.Any())
{
if (clrType.IsNested)
{
SessionData.EdmItemErrors.Add(
new EdmItemError(Strings.NestedClassNotSupported(clrType.FullName, clrType.Assembly().FullName)));
return;
}
var typeAttribute = typeAttributes.First();
var cspaceTypeName = String.IsNullOrEmpty(typeAttribute.Name) ? clrType.Name : typeAttribute.Name;
if (String.IsNullOrEmpty(typeAttribute.NamespaceName)
&& clrType.Namespace == null)
{
SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_TypeHasNoNamespace));
return;
}
var cspaceNamespaceName = String.IsNullOrEmpty(typeAttribute.NamespaceName)
? clrType.Namespace
: typeAttribute.NamespaceName;
if (typeAttribute.GetType() == typeof(EdmEntityTypeAttribute))
{
edmType = new ClrEntityType(clrType, cspaceNamespaceName, cspaceTypeName);
}
else if (typeAttribute.GetType() == typeof(EdmComplexTypeAttribute))
{
edmType = new ClrComplexType(clrType, cspaceNamespaceName, cspaceTypeName);
}
else
{
Debug.Assert(typeAttribute is EdmEnumTypeAttribute, "Invalid type attribute encountered");
// Note that TryGetPrimitiveType() will return false not only for types that are not primitive
// but also for CLR primitive types that are valid underlying enum types in CLR but are not
// a valid Edm primitive types (e.g. ulong)
PrimitiveType underlyingEnumType;
if (!ClrProviderManifest.Instance.TryGetPrimitiveType(clrType.GetEnumUnderlyingType(), out underlyingEnumType))
{
SessionData.EdmItemErrors.Add(
new EdmItemError(
Strings.Validator_UnsupportedEnumUnderlyingType(clrType.GetEnumUnderlyingType().FullName)));
return;
}
edmType = new ClrEnumType(clrType, cspaceNamespaceName, cspaceTypeName);
}
}
else
{
// not a type we are interested
return;
}
Debug.Assert(
!CacheEntry.ContainsType(edmType.Identity), "This type must not be already present in the list of types for this assembly");
// Also add this to the list of the types for this assembly
CacheEntry.TypesInAssembly.Add(edmType);
// Add this to the known type map so we won't try to load it again
SessionData.TypesInLoading.Add(clrType.FullName, edmType);
// Load properties for structural type
if (Helper.IsStructuralType(edmType))
{
//Load base type only for entity type - not sure if we will allow complex type inheritance
if (Helper.IsEntityType(edmType))
{
TrackClosure(clrType.BaseType());
AddTypeResolver(
() => edmType.BaseType = ResolveBaseType(clrType.BaseType()));
}
// Load the properties for this type
LoadPropertiesFromType((StructuralType)edmType);
}
return;
}