当前位置: 首页>>代码示例>>C#>>正文


C# TypeInfo.GetGenericTypeDefinition方法代码示例

本文整理汇总了C#中System.Reflection.TypeInfo.GetGenericTypeDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# TypeInfo.GetGenericTypeDefinition方法的具体用法?C# TypeInfo.GetGenericTypeDefinition怎么用?C# TypeInfo.GetGenericTypeDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.TypeInfo的用法示例。


在下文中一共展示了TypeInfo.GetGenericTypeDefinition方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: resolveGenericTypeDefinition

        private static TypeInfo resolveGenericTypeDefinition(TypeInfo parent)
        {
            var shouldUseGenericType = !(parent.IsGenericType && parent.GetGenericTypeDefinition().GetTypeInfo() != parent);

            if (parent.IsGenericType && shouldUseGenericType)
                parent = parent.GetGenericTypeDefinition().GetTypeInfo();

            return parent;
        }
开发者ID:Banane9,项目名称:SilverConfig,代码行数:9,代码来源:Util.cs

示例2: TryGetMapping

 private static bool TryGetMapping(TypeInfo firstGenericType, Type sourceType, Type targetType,
  string targetName, string prefix, ref TargetMapping mapping)
 {
     if (firstGenericType.GetGenericTypeDefinition() == sourceType.GetTypeInfo())
      {
     // Not crazy about this, but a lot of mapping work happens in the constructors here
     mapping = ReflectionHelpers.CreateInstanceOfType<TargetMapping>(targetType.GetTypeInfo(),
              firstGenericType, targetName, prefix, firstGenericType);
     return true;
      }
      return false;
 }
开发者ID:KathleenDollard,项目名称:CodeFirstMetadata,代码行数:12,代码来源:TargetMapping.cs

示例3: IsAssignableFrom

        public static bool IsAssignableFrom(TypeInfo toTypeInfo, TypeInfo fromTypeInfo, FoundationTypes foundationTypes)
        {
            if (toTypeInfo == null)
                throw new NullReferenceException();
            if (fromTypeInfo == null)
                return false;   // It would be more appropriate to throw ArgumentNullException here, but returning "false" is the desktop-compat behavior.

            if (fromTypeInfo.Equals(toTypeInfo))
                return true;

            if (toTypeInfo.IsGenericTypeDefinition)
            {
                // Asking whether something can cast to a generic type definition is arguably meaningless. The desktop CLR Reflection layer converts all
                // generic type definitions to generic type instantiations closed over the formal generic type parameters. The .NET Native framework
                // keeps the two separate. Fortunately, under either interpretation, returning "false" unless the two types are identical is still a 
                // defensible behavior. To avoid having the rest of the code deal with the differing interpretations, we'll short-circuit this now.
                return false;
            }

            if (fromTypeInfo.IsGenericTypeDefinition)
            {
                // The desktop CLR Reflection layer converts all generic type definitions to generic type instantiations closed over the formal 
                // generic type parameters. The .NET Native framework keeps the two separate. For the purpose of IsAssignableFrom(), 
                // it makes sense to unify the two for the sake of backward compat. We'll just make the transform here so that the rest of code
                // doesn't need to know about this quirk.
                fromTypeInfo = fromTypeInfo.GetGenericTypeDefinition().MakeGenericType(fromTypeInfo.GenericTypeParameters).GetTypeInfo();
            }

            if (fromTypeInfo.CanCastTo(toTypeInfo, foundationTypes))
                return true;

            Type toType = toTypeInfo.AsType();
            Type fromType = fromTypeInfo.AsType();

            // Desktop compat: IsAssignableFrom() considers T as assignable to Nullable<T> (but does not check if T is a generic parameter.)
            if (!fromType.IsGenericParameter)
            {
                Type nullableUnderlyingType = Nullable.GetUnderlyingType(toType);
                if (nullableUnderlyingType != null && nullableUnderlyingType.Equals(fromType))
                    return true;
            }
            return false;
        }
开发者ID:noahfalk,项目名称:corert,代码行数:43,代码来源:Assignability.cs

示例4: CanCastArrayToInterface

        //
        // T[] casts to IList<T>. This could be handled by the normal ancestor-walking code
        // but for one complication: T[] also casts to IList<U> if T[] casts to U[].
        //
        private static bool CanCastArrayToInterface(this TypeInfo fromTypeInfo, TypeInfo toTypeInfo, FoundationTypes foundationTypes)
        {
            Debug.Assert(fromTypeInfo.IsArray);
            Debug.Assert(toTypeInfo.IsInterface);

            Type toType = toTypeInfo.AsType();

            if (toType.IsConstructedGenericType)
            {
                Type[] toTypeGenericTypeArguments = toTypeInfo.GenericTypeArguments;
                if (toTypeGenericTypeArguments.Length != 1)
                    return false;
                TypeInfo toElementTypeInfo = toTypeGenericTypeArguments[0].GetTypeInfo();

                Type toTypeGenericTypeDefinition = toTypeInfo.GetGenericTypeDefinition();
                TypeInfo fromElementTypeInfo = fromTypeInfo.GetElementType().GetTypeInfo();
                foreach (Type ifc in fromTypeInfo.ImplementedInterfaces)
                {
                    if (ifc.IsConstructedGenericType)
                    {
                        Type ifcGenericTypeDefinition = ifc.GetGenericTypeDefinition();
                        if (ifcGenericTypeDefinition.Equals(toTypeGenericTypeDefinition))
                        {
                            if (fromElementTypeInfo.IsElementTypeCompatibleWith(toElementTypeInfo, foundationTypes))
                                return true;
                        }
                    }
                }
                return false;
            }
            else
            {
                foreach (Type ifc in fromTypeInfo.ImplementedInterfaces)
                {
                    if (ifc.Equals(toType))
                        return true;
                }
                return false;
            }
        }
开发者ID:tijoytom,项目名称:corert,代码行数:44,代码来源:Assignability.cs

示例5: Parse

        private static ResolvedPropertyTypeInfo Parse(TypeInfo propertyTypeInfo, Boolean throwException)
        {
            if (propertyTypeInfo == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(propertyTypeInfo));
            }

            if (!propertyTypeInfo.IsGenericType)
            {
                if (!throwException)
                {
                    return null;
                }

                throw Logger.Fatal.ArgumentFormat(
                    nameof(propertyTypeInfo),
                    SR.ResolveResultFactory_PropertyTypeNotGeneric,
                    propertyTypeInfo
                );
            }

            var interfaceType = propertyTypeInfo.GetGenericTypeDefinition();

            if (!KnownPropertyTypeDefinitions.Contains(interfaceType))
            {
                if (!throwException)
                {
                    return null;
                }

                throw InvalidInterfaceType(nameof(propertyTypeInfo), interfaceType);
            }

            return new ResolvedPropertyTypeInfo()
            {
                InterfaceType = interfaceType,
                ResolvedType = propertyTypeInfo.GenericTypeArguments.First(),
            };
        }
开发者ID:NaseUkolyCZ,项目名称:HarshPoint,代码行数:39,代码来源:ResolvedPropertyTypeInfo.cs

示例6: IsSupportedImportManyType

 private bool IsSupportedImportManyType(TypeInfo typeInfo)
 {
     return typeInfo.IsArray ||
         (typeInfo.IsGenericTypeDefinition && s_supportedImportManyTypes.Contains(typeInfo.AsType())) ||
         (typeInfo.AsType().IsConstructedGenericType && s_supportedImportManyTypes.Contains(typeInfo.GetGenericTypeDefinition()));
 }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:6,代码来源:ImportConventionBuilder.cs

示例7: IsClosedGenericOf

 /// <summary>
 /// Determines whether the provided interface is a closed generic of the specified open generic contract.
 /// </summary>
 /// <param name="openGenericContract">The open generic contract.</param>
 /// <param name="exportInterface">The export interface.</param>
 /// <returns><c>true</c> if the provided interface is a closed generic of the specified open generic contract, otherwise <c>false</c>.</returns>
 private bool IsClosedGenericOf(TypeInfo openGenericContract, TypeInfo exportInterface)
 {
     return exportInterface.IsGenericType && exportInterface.GetGenericTypeDefinition() == openGenericContract.AsType();
 }
开发者ID:movileanubeniamin,项目名称:kephas,代码行数:10,代码来源:AppServiceConventionsRegistrar.cs

示例8: AreTypesEquivalentInternal

        // Method to compare two types pointers for type equality
        // We cannot just compare the pointers as there can be duplicate type instances
        // for cloned and constructed types.
        static bool AreTypesEquivalentInternal(TypeInfo pType1, TypeInfo pType2)
        {
            if (!pType1.IsInstantiatedTypeInfo() && !pType2.IsInstantiatedTypeInfo())
                return pType1.Equals(pType2);

            if (pType1.IsGenericType && pType2.IsGenericType)
            {
                if (!pType1.GetGenericTypeDefinition().Equals(pType2.GetGenericTypeDefinition()))
                    return false;

                Type[] args1 = pType1.GenericTypeArguments;
                Type[] args2 = pType2.GenericTypeArguments;
                Debug.Assert(args1.Length == args2.Length);

                for (int i = 0; i < args1.Length; i++)
                {
                    if (!AreTypesEquivalentInternal(args1[i].GetTypeInfo(), args2[i].GetTypeInfo()))
                        return false;
                }

                return true;
            }

            if (pType1.IsArray && pType2.IsArray)
            {
                if (pType1.GetArrayRank() != pType2.GetArrayRank())
                    return false;

                return AreTypesEquivalentInternal(pType1.GetElementType().GetTypeInfo(), pType2.GetElementType().GetTypeInfo());
            }

            if (pType1.IsPointer && pType2.IsPointer)
            {
                return AreTypesEquivalentInternal(pType1.GetElementType().GetTypeInfo(), pType2.GetElementType().GetTypeInfo());
            }

            return false;
        }
开发者ID:huamichaelchen,项目名称:corert,代码行数:41,代码来源:TypeCast.cs

示例9: ImplementsInterface

        static bool ImplementsInterface(TypeInfo pObjType, TypeInfo pTargetType)
        {
            Debug.Assert(!pTargetType.IsArray, "did not expect array type");
            Debug.Assert(pTargetType.IsInterface, "IsInstanceOfInterface called with non-interface EEType");

            foreach (var pInterfaceType in pObjType.ImplementedInterfaces)
            {
                if (AreTypesEquivalentInternal(pInterfaceType.GetTypeInfo(), pTargetType))
                {
                    return true;
                }
            }

            // We did not find the interface type in the list of supported interfaces. There's still one
            // chance left: if the target interface is generic and one or more of its type parameters is co or
            // contra variant then the object can still match if it implements a different instantiation of
            // the interface with type compatible generic arguments.
            //
            // An additional edge case occurs because of array covariance. This forces us to treat any generic
            // interfaces implemented by arrays as covariant over their one type parameter.
            // if (pTargetType.HasGenericVariance || (fArrayCovariance && pTargetType.IsGenericType))
            //
            if (pTargetType.IsGenericType)
            {
                bool fArrayCovariance = pObjType.IsArray;
                Type pTargetGenericType = pTargetType.GetGenericTypeDefinition();

                // Fetch the instantiations lazily only once we get a potential match
                Type[] pTargetInstantiation = null;
                Type[] pTargetGenericInstantiation = null;

                foreach (var pInterface in pObjType.ImplementedInterfaces)
                {
                    TypeInfo pInterfaceType = pInterface.GetTypeInfo();

                    // We can ignore interfaces which are not also marked as having generic variance
                    // unless we're dealing with array covariance. 
                    // if (pInterfaceType.HasGenericVariance || (fArrayCovariance && pInterfaceType.IsGenericType))

                    if (!pInterfaceType.IsGenericType)
                        continue;

                    // If the generic types aren't the same then the types aren't compatible.
                    if (!pInterfaceType.GetGenericTypeDefinition().Equals(pTargetGenericType))
                        continue;

                    Type[] pInterfaceInstantiation = pInterfaceType.GenericTypeArguments;

                    if (pTargetInstantiation == null)
                    {
                        pTargetInstantiation = pTargetType.GenericTypeArguments;

                        if (!fArrayCovariance)
                            pTargetGenericInstantiation = pTargetGenericType.GetTypeInfo().GenericTypeParameters;
                    }

                    // Compare the instantiations to see if they're compatible taking variance into account.
                    if (TypeParametersAreCompatible(pInterfaceInstantiation,
                                                    pTargetInstantiation,
                                                    pTargetGenericInstantiation,
                                                    fArrayCovariance))
                        return true;

                    if (fArrayCovariance)
                    {
                        Debug.Assert(pInterfaceInstantiation.Length == 1, "arity mismatch for array generic interface");
                        Debug.Assert(pTargetInstantiation.Length == 1, "arity mismatch for array generic interface");

                        // Special case for generic interfaces on arrays. Arrays of integral types (including enums)
                        // can be cast to generic interfaces over the integral types of the same size. For example
                        // int[] . IList<uint>.
                        if (ArePrimitveTypesEquivalentSize(pInterfaceInstantiation[0].GetTypeInfo(),
                                                           pTargetInstantiation[0].GetTypeInfo()))
                        {
                            // We have checked that the interface type definition matches above. The checks are ordered differently
                            // here compared with rtm\system\runtime\typecast.cs version because of TypeInfo does not let us do
                            // the HasGenericVariance optimization.
                            return true;
                        }
                    }
                }
            }

            return false;
        }
开发者ID:huamichaelchen,项目名称:corert,代码行数:85,代码来源:TypeCast.cs

示例10: TypesAreCompatibleViaGenericVariance

        // Compare two types to see if they are compatible via generic variance.
        static bool TypesAreCompatibleViaGenericVariance(TypeInfo pSourceType, TypeInfo pTargetType)
        {
            Type pTargetGenericType = pTargetType.GetGenericTypeDefinition();
            Type pSourceGenericType = pSourceType.GetGenericTypeDefinition();

            // If the generic types aren't the same then the types aren't compatible.
            if (pTargetGenericType.Equals(pSourceGenericType))
            {
                // Compare the instantiations to see if they're compatible taking variance into account.
                if (TypeParametersAreCompatible(pSourceType.GenericTypeArguments,
                                                pTargetType.GenericTypeArguments,
                                                pTargetGenericType.GetTypeInfo().GenericTypeParameters,
                                                false))
                {
                    return true;
                }
            }

            return false;
        }
开发者ID:huamichaelchen,项目名称:corert,代码行数:21,代码来源:TypeCast.cs

示例11: IsGenericIEnumerable

 private static bool IsGenericIEnumerable (TypeInfo enumerableType)
 {
   return IsIEnumerable (enumerableType)
          && enumerableType.IsGenericType
          && enumerableType.GetGenericTypeDefinition() == typeof (IEnumerable<>);
 }
开发者ID:natemcmaster,项目名称:Relinq,代码行数:6,代码来源:ItemTypeReflectionUtility.cs

示例12: GetGenericTypeDefinition

        /// <summary>
        /// Gets the generic type definition.
        /// </summary>
        /// <param name="typeInfo">The <see cref="TypeInfo"/>.</param>
        /// <returns>
        /// The generic type definition.
        /// </returns>
        private ITypeInfo GetGenericTypeDefinition(TypeInfo typeInfo)
        {
            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                return GetRuntimeType(typeInfo.GetGenericTypeDefinition());
            }

            return null;
        }
开发者ID:raimu,项目名称:kephas,代码行数:16,代码来源:RuntimeTypeInfo.cs

示例13: MatchesWithVariance

        //
        // Check a base type or implemented interface type for equivalence (taking into account variance for generic instantiations.)
        // Does not check ancestors recursively.
        //
        private static bool MatchesWithVariance(this TypeInfo fromTypeInfo, TypeInfo toTypeInfo, FoundationTypes foundationTypes)
        {
            Debug.Assert(!(fromTypeInfo.IsArray || fromTypeInfo.IsByRef || fromTypeInfo.IsPointer || fromTypeInfo.IsGenericParameter));
            Debug.Assert(!(toTypeInfo.IsArray || toTypeInfo.IsByRef || toTypeInfo.IsPointer || toTypeInfo.IsGenericParameter));

            if (fromTypeInfo.Equals(toTypeInfo))
                return true;

            if (!(fromTypeInfo.AsType().IsConstructedGenericType && toTypeInfo.AsType().IsConstructedGenericType))
                return false;

            TypeInfo genericTypeDefinition = fromTypeInfo.GetGenericTypeDefinition().GetTypeInfo();
            if (!genericTypeDefinition.AsType().Equals(toTypeInfo.GetGenericTypeDefinition()))
                return false;

            Type[] fromTypeArguments = fromTypeInfo.GenericTypeArguments;
            Type[] toTypeArguments = toTypeInfo.GenericTypeArguments;
            Type[] genericTypeParameters = genericTypeDefinition.GenericTypeParameters;
            for (int i = 0; i < genericTypeParameters.Length; i++)
            {
                TypeInfo fromTypeArgumentInfo = fromTypeArguments[i].GetTypeInfo();
                TypeInfo toTypeArgumentInfo = toTypeArguments[i].GetTypeInfo();

                GenericParameterAttributes attributes = genericTypeParameters[i].GetTypeInfo().GenericParameterAttributes;
                switch (attributes & GenericParameterAttributes.VarianceMask)
                {
                    case GenericParameterAttributes.Covariant:
                        if (!(fromTypeArgumentInfo.IsGcReferenceTypeAndCastableTo(toTypeArgumentInfo, foundationTypes)))
                            return false;
                        break;

                    case GenericParameterAttributes.Contravariant:
                        if (!(toTypeArgumentInfo.IsGcReferenceTypeAndCastableTo(fromTypeArgumentInfo, foundationTypes)))
                            return false;
                        break;

                    case GenericParameterAttributes.None:
                        if (!(fromTypeArgumentInfo.Equals(toTypeArgumentInfo)))
                            return false;
                        break;

                    default:
                        throw new BadImageFormatException();  // Unexpected variance value in metadata.
                }
            }
            return true;
        }
开发者ID:noahfalk,项目名称:corert,代码行数:51,代码来源:Assignability.cs

示例14: UnwrapNullable

 private TypeInfo UnwrapNullable(TypeInfo info)
 {
     if (info.IsGenericType && info.GetGenericTypeDefinition() == typeof(Nullable<>))
     {
         return info.GenericTypeArguments[0].GetTypeInfo();
     }
     return info;
 }
开发者ID:kodefuguru,项目名称:bungie,代码行数:8,代码来源:ModelMetadataProvider.cs


注:本文中的System.Reflection.TypeInfo.GetGenericTypeDefinition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。