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


C# Type.IsEnumerable方法代码示例

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


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

示例1: CreateSpecFor

        private ModelSpec CreateSpecFor(Type type, bool deferIfComplex, Dictionary<Type, ModelSpec> deferredMappings)
        {
            if (_customMappings.ContainsKey(type))
                return _customMappings[type];

            if (PrimitiveMappings.ContainsKey(type))
                return PrimitiveMappings[type];

            if (type.IsEnum)
                return new ModelSpec {Type = "string", Enum = type.GetEnumNames()};

            Type innerType;
            if (type.IsNullable(out innerType))
                return CreateSpecFor(innerType, deferIfComplex, deferredMappings);

            Type itemType;
            if (type.IsEnumerable(out itemType))
                return new ModelSpec { Type = "array", Items = CreateSpecFor(itemType, true, deferredMappings) };

            // Anthing else is complex

            if (deferIfComplex)
            {
                if (!deferredMappings.ContainsKey(type))
                    deferredMappings.Add(type, null);
                
                // Just return a reference for now
                return new ModelSpec {Ref = UniqueIdFor(type)};
            }

            return CreateComplexSpecFor(type, deferredMappings);
        }
开发者ID:robinvanderknaap,项目名称:Swashbuckle.Lite,代码行数:32,代码来源:ModelSpecGenerator.cs

示例2: 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

示例3: Convert

        /// <summary>
        /// Convert the string representation to the destination type
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="destinationType">Destination type</param>
        /// <param name="context">Current context</param>
        /// <returns>Converted object of the destination type</returns>
        public object Convert(string input, Type destinationType, BindingContext context)
        {
            if (string.IsNullOrEmpty(input))
            {
                return null;
            }

            var items = input.Split(',');

            // Strategy, schmategy ;-)
            if (destinationType.IsCollection())
            {
                return this.ConvertCollection(items, destinationType, context);
            }

            if (destinationType.IsArray())
            {
                return this.ConvertArray(items, destinationType, context);
            }

            if (destinationType.IsEnumerable())
            {
                return this.ConvertEnumerable(items, destinationType, context);
            }

            return null;
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:34,代码来源:CollectionConverter.cs

示例4: IncludePathMetadataFragment

 /// <summary>
 /// Initializes a new instance of the <see cref="IncludePathMetadataFragment"/> class that has the given declaring type, property type, and property access expression.
 /// </summary>
 /// <param name="declaringType">The <see cref="System.Type"/> that contains the represented property as a member.</param>
 /// <param name="propertyType">The <see cref="System.Type"/> of the represented property.</param>
 /// <param name="propertyAccessExpression">The <see cref="System.Linq.Expressions.MemberExpression"/> used to access the represented property.</param>
 public IncludePathMetadataFragment(Type declaringType, Type propertyType, MemberExpression propertyAccessExpression)
 {
     DeclaringType = declaringType;
     IsEnumerable = propertyType.IsEnumerable();
     PropertyAccessExpression = propertyAccessExpression;
     PropertySingularType = propertyType.ToSingleType();
     PropertyType = propertyType;
 }
开发者ID:calebjenkins,项目名称:Highway.Data,代码行数:14,代码来源:IncludePathMetadataFragment.cs

示例5: IsDefactoComplexType

        public static bool IsDefactoComplexType(Type type)
        {
            if (type.IsString())
                return false;

            if (type.IsEnumerable())
                return true;

            return (type.IsClass || type.IsInterface || type.IsEnum);
        }
开发者ID:leovonwyss,项目名称:nplant,代码行数:10,代码来源:TypeMetaModel.cs

示例6: BuildSource

        public Expression BuildSource(Expression expression, Type destinationType, IMappingConfiguration mappingConfiguration)
        {
            if (!expression.Type.IsEnumerable() || !destinationType.IsEnumerable())
                return null;

            var isArray = destinationType.IsArray;
            var sourceElementType = TypeUtils.GetElementTypeOfEnumerable(expression.Type);
            var destinationElementType = TypeUtils.GetElementTypeOfEnumerable(destinationType);
            return Expression.Convert(CreateSelect(mappingConfiguration, sourceElementType, destinationElementType, expression, isArray ? "ToArray" : "ToList"), destinationType);
        }
开发者ID:erkanmaras,项目名称:OoMapper,代码行数:10,代码来源:EnumerableToEnumerableMapper.cs

示例7: SimplifyType

        private static Type SimplifyType(Type type)
        {
            if (type.IsEnumerable())
            {
                Type elementType = type.ExtractEnumerableElementType();

                if (elementType != null)
                {
                    return typeof (IEnumerable<>).MakeGenericType(elementType);
                }
            }

            return type;
        }
开发者ID:justin-lovell,项目名称:semantic-pipes,代码行数:14,代码来源:SimplifyEnumerableTypeResolving.cs

示例8: Decompress

        public object Decompress(object payload, Type expected)
        {
            if (payload != null)
            {
                PayloadDescriptor payloadDescriptor;
                var enumerable = false;

                if (expected.IsEnumerable())
                {
                    enumerable = true;
                    payloadDescriptor = _provider.GetPayload(expected.GetEnumerableType());
                }
                else
                {
                    payloadDescriptor = _provider.GetPayload(expected);
                }

                // Check if our payload object is actually a payload
                if (payloadDescriptor != null)
                {
                    if (enumerable)
                    {
                        IList list = null;

                        // Arrays have no default constructor so cannot be created by the Activator
                        if (expected.IsArray)
                        {
                            list = new List<object>();
                        }
                        else
                        {
                            list = Activator.CreateInstance(expected) as IList;
                        }

                        var compressedPayload = payload as object[];

                        foreach (object data in compressedPayload)
                        {
                            list.Add(Decompress(ConvertToObjectArray(data), payloadDescriptor));
                        }

                        if (expected.IsArray)
                        {
                            var arr = Array.CreateInstance(expected.GetEnumerableType(), list.Count);

                            list.CopyTo(arr, 0);

                            return arr;
                        }

                        return list;
                    }
                    else
                    {
                        return Decompress(payload, payloadDescriptor);
                    }
                }
            }

            return payload;
        }
开发者ID:NTaylorMullen,项目名称:SignalR.Compression,代码行数:61,代码来源:DefaultPayloadDecompressor.cs

示例9: IsTypeOfInterest

 private static bool IsTypeOfInterest(Type type)
 {
     return type.IsEnumerable() &&
            (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof (IEnumerable<>));
 }
开发者ID:justin-lovell,项目名称:semantic-pipes,代码行数:5,代码来源:SimplifyEnumerableOutputRegistryMediator.cs

示例10: CreateModelSpec

        private ModelSpec CreateModelSpec(Type type)
        {
            // Primitives, incl. enums
            if (_predefinedTypeMap.ContainsKey(type))
                return _predefinedTypeMap[type];

            if (type.IsEnum)
                return new ModelSpec
                    {
                        Type = "string",
                        Enum = type.GetEnumNames()
                    };

            Type enumerableTypeArgument;
            if (type.IsEnumerable(out enumerableTypeArgument))
                return CreateContainerSpec(enumerableTypeArgument);

            Type nullableTypeArgument;
            if (type.IsNullable(out nullableTypeArgument))
                return FindOrCreateFor(nullableTypeArgument);

            return CreateComplexSpec(type);
        }
开发者ID:nprovencher,项目名称:Swashbuckle,代码行数:23,代码来源:ModelSpecMap.cs

示例11: HasPayload

        public bool HasPayload(Type type)
        {
            if (type.IsEnumerable())
            {
                type = type.GetEnumerableType();
            }

            return IsPayload(type);
        }
开发者ID:jaysilk84,项目名称:SignalR.Compression,代码行数:9,代码来源:ReflectedPayloadDescriptorProvider.cs

示例12: GetOrRegister

        private DataType GetOrRegister(Type type, bool deferIfComplex, Queue<Type> deferredTypes)
        {
            if (_customMappings.ContainsKey(type))
                return _customMappings[type]();

            if (IndeterminateMappings.ContainsKey(type))
                return IndeterminateMappings[type]();

            if (PrimitiveMappings.ContainsKey(type))
                return PrimitiveMappings[type]();

            if (type.IsEnum)
                return new DataType { Type = "string", Enum = type.GetEnumNames() };

            Type innerType;
            if (type.IsNullable(out innerType))
                return GetOrRegister(innerType, deferIfComplex, deferredTypes);

            Type itemType;
            if (type.IsEnumerable(out itemType))
            {
                if (itemType.IsEnumerable() && !IsSupportedEnumerableItem(itemType))
                    throw new InvalidOperationException(
                        String.Format("Type {0} is not supported. Swagger does not support containers of containers", type));

                return new DataType { Type = "array", Items = GetOrRegister(itemType, true, deferredTypes) };
            }

            // Anthing else is complex
            if (deferIfComplex)
            {
                if (!_complexMappings.ContainsKey(type))
                    deferredTypes.Enqueue(type);

                // Just return a reference for now
                return new DataType { Ref = UniqueIdFor(type) };
            }

            return _complexMappings.GetOrAdd(type, () => CreateComplexDataType(type, deferredTypes));
        }
开发者ID:jasonmueller,项目名称:Swashbuckle,代码行数:40,代码来源:DataTypeRegistry.cs

示例13: IsEnumerableTest

        public void IsEnumerableTest(Type type, bool expectedResult)
        {
            var result = type.IsEnumerable();

            Assert.AreEqual(expectedResult, result, "Check for enumerable type has failed.");
        }
开发者ID:nbarnwell,项目名称:Regalo,代码行数:6,代码来源:TypeExtensionsTests.cs

示例14: IsEnumerableDoesNotDetectCertainTypesOfCollections

 public void IsEnumerableDoesNotDetectCertainTypesOfCollections(Type type)
 {
     Assert.False(type.IsEnumerable());
 }
开发者ID:NTaylorMullen,项目名称:SignalR.Compression,代码行数:4,代码来源:TypeExtensionFacts.cs

示例15: should_indicate_if_type_is_enumerable

 public void should_indicate_if_type_is_enumerable(Type type)
 {
     type.IsEnumerable().ShouldBeTrue();
 }
开发者ID:carl-berg,项目名称:Bender,代码行数:4,代码来源:CollectionExtensionTests.cs


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