Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CountEnumerable.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Globalization;
6 using System.Linq;
7 
8 namespace SiliconStudio.Presentation.ValueConverters
9 {
10  /// <summary>
11  /// This converter will take an enumerable as input and return the number of items it contains.
12  /// </summary>
13  public class CountEnumerable : OneWayValueConverter<CountEnumerable>
14  {
15  /// <inheritdoc/>
16  public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
17  {
18  if (value == null)
19  return 0;
20 
21  if (!(value is IEnumerable))
22  throw new ArgumentException(@"The given value must implement IEnumerable", "value");
23 
24  var enumerable = (IEnumerable)value;
25  return enumerable.Cast<object>().Count();
26  }
27  }
28 }
override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
This converter will take an enumerable as input and return the number of items it contains...