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


C# Type.IsGenericType方法代码示例

本文整理汇总了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;
        }
开发者ID:gitter-badger,项目名称:RethinkDb.Driver,代码行数:7,代码来源:ReqlGroupingConverter.cs

示例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;
 }
开发者ID:j2jensen,项目名称:ravendb,代码行数:29,代码来源:TypeSystem.cs

示例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));
        }
开发者ID:sh-sabooni,项目名称:AutoMapper,代码行数:28,代码来源:TypeHelper.cs

示例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;
		}
开发者ID:samueldjack,项目名称:ravendb,代码行数:40,代码来源:ReflectionUtil.cs

示例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;
        }
开发者ID:wholroyd,项目名称:ShareFile-NET,代码行数:31,代码来源:TypeExtensions.cs

示例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;
 }
开发者ID:mbrit,项目名称:MoqRTPOC,代码行数:7,代码来源:InvocationWithGenericDelegateContributor.cs

示例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);
      }
    }
开发者ID:pvasek,项目名称:Newtonsoft.Json,代码行数:31,代码来源:JsonArrayContract.cs

示例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;
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:28,代码来源:SimpleTypesExcludeFilter.cs

示例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);
        }
开发者ID:abatishchev,项目名称:SimpleInjector,代码行数:30,代码来源:TypeGeneralizer.cs

示例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);
    }
开发者ID:925coder,项目名称:ravendb,代码行数:35,代码来源:JsonArrayContract.cs

示例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;
        }
开发者ID:dmitriylyner,项目名称:ServiceControl,代码行数:41,代码来源:StringListBinder.cs

示例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<>);
        }
开发者ID:smasonuk,项目名称:odata-sparql,代码行数:11,代码来源:TypeUtils.cs

示例13: GetClosedContainerControlledItemsFor

        private ContainerControlledItem[] GetClosedContainerControlledItemsFor(Type serviceType)
        {
            var items = this.GetItemsFor(serviceType);

            return serviceType.IsGenericType()
                ? Helpers.GetClosedGenericImplementationsFor(serviceType, items)
                : items.ToArray();
        }
开发者ID:abatishchev,项目名称:SimpleInjector,代码行数:8,代码来源:ContainerControlledCollectionResolver.cs

示例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;
        }
开发者ID:mbrit,项目名称:MoqRTPOC,代码行数:9,代码来源:InvocationWithDelegateContributor.cs

示例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()));
        }
开发者ID:nsubstitute,项目名称:NSubstitute,代码行数:9,代码来源:ArgumentFormatter.cs


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