Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ArrayDescriptor.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.Generic;
5 using System.Reflection;
6 
7 namespace SiliconStudio.Core.Reflection
8 {
9  /// <summary>
10  /// A descriptor for an array.
11  /// </summary>
13  {
14  private readonly Type elementType;
15  private readonly Type listType;
16  private readonly MethodInfo toArrayMethod;
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
20  /// </summary>
21  /// <param name="factory">The factory.</param>
22  /// <param name="type">The type.</param>
23  /// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
24  public ArrayDescriptor(ITypeDescriptorFactory factory, Type type)
25  : base(factory, type)
26  {
27  if (!type.IsArray) throw new ArgumentException("Expecting array type", "type");
28 
29  if (type.GetArrayRank() != 1)
30  {
31  throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName));
32  }
33 
34  Category = DescriptorCategory.Array;
35  elementType = type.GetElementType();
36  listType = typeof(List<>).MakeGenericType(ElementType);
37  toArrayMethod = listType.GetMethod("ToArray");
38  }
39 
40  /// <summary>
41  /// Gets the type of the array element.
42  /// </summary>
43  /// <value>The type of the element.</value>
44  public Type ElementType
45  {
46  get
47  {
48  return elementType;
49  }
50  }
51 
52  /// <summary>
53  /// Creates the equivalent of list type for this array.
54  /// </summary>
55  /// <returns>A list type with same element type than this array.</returns>
56  public Array CreateArray(int dimension)
57  {
58  return Array.CreateInstance(ElementType, dimension);
59  }
60  }
61 }
Default implementation of a ITypeDescriptor.
ArrayDescriptor(ITypeDescriptorFactory factory, Type type)
Initializes a new instance of the ObjectDescriptor class.
A factory to create an instance of a ITypeDescriptor
Array CreateArray(int dimension)
Creates the equivalent of list type for this array.