本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例9: IsTypeOfInterest
private static bool IsTypeOfInterest(Type type)
{
return type.IsEnumerable() &&
(!type.IsGenericType || type.GetGenericTypeDefinition() != typeof (IEnumerable<>));
}
示例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);
}
示例11: HasPayload
public bool HasPayload(Type type)
{
if (type.IsEnumerable())
{
type = type.GetEnumerableType();
}
return IsPayload(type);
}
示例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));
}
示例13: IsEnumerableTest
public void IsEnumerableTest(Type type, bool expectedResult)
{
var result = type.IsEnumerable();
Assert.AreEqual(expectedResult, result, "Check for enumerable type has failed.");
}
示例14: IsEnumerableDoesNotDetectCertainTypesOfCollections
public void IsEnumerableDoesNotDetectCertainTypesOfCollections(Type type)
{
Assert.False(type.IsEnumerable());
}
示例15: should_indicate_if_type_is_enumerable
public void should_indicate_if_type_is_enumerable(Type type)
{
type.IsEnumerable().ShouldBeTrue();
}