本文整理汇总了C#中Type.IsEnumerable方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsEnumerable方法的具体用法?C# Type.IsEnumerable怎么用?C# Type.IsEnumerable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsEnumerable方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: CanConvertTo
/// <summary>
/// Whether the converter can convert to the destination type
/// </summary>
/// <param name="destinationType">Destination type</param>
/// <param name="context">The current binding context</param>
/// <returns>True if conversion supported, false otherwise</returns>
public bool CanConvertTo(Type destinationType, BindingContext context)
{
return destinationType.IsCollection() || destinationType.IsEnumerable() || destinationType.IsArray();
}
示例3: MakeInstance
/// <summary>Creates an instance of the <paramref name="targetType" /> from an enumeration of objects.</summary>
/// <param name="values">Objects to feed the new instance.</param>
/// <param name="targetType">Target type of the instance.</param>
/// <param name="itemType">Optional collection item type.</param>
/// <returns>Instance of the <paramref name="targetType" />.</returns>
public static object MakeInstance(this IEnumerable<object> values, Type targetType, Type itemType = null)
{
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
if (values == null)
{
return null;
}
if (!targetType.IsEnumerable())
{
return ((targetType.IsGenericType) && (targetType.GetGenericTypeDefinition() == typeof(Nullable<>)) ?
typeof(Nullable<>).MakeGenericType(itemType).GetConstructor(new[] { itemType }).Invoke(new[] { Convert.ChangeType(values.First(), itemType) }) :
Convert.ChangeType(values.First(), targetType));
}
return CreateEnumerable(values, targetType, itemType ?? typeof(object));
}