當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。