Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BoxedContent.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.Collections.Generic;
6 
7 using SiliconStudio.Core.Reflection;
8 
9 namespace SiliconStudio.Quantum.Contents
10 {
11  public class BoxedContent : ObjectContent
12  {
13  public BoxedContent(object value, ITypeDescriptor descriptor, bool isPrimitive)
14  : base(value, descriptor, isPrimitive, null)
15  {
16  }
17 
18  public override object Value
19  {
20  get { return base.Value; }
21  set
22  {
23  base.Value = value;
24  if (BoxedStructureOwner != null)
25  {
26  if (BoxedStructureOwnerIndices != null)
27  {
28  var currentObj = BoxedStructureOwner.Value;
29  for (int i = 0; i < BoxedStructureOwnerIndices.Length - 1; ++i)
30  {
31  currentObj = FetchItem(currentObj, BoxedStructureOwnerIndices[i]);
32  }
33  SetItem(currentObj, BoxedStructureOwnerIndices[BoxedStructureOwnerIndices.Length - 1], value);
34  }
35  else
36  BoxedStructureOwner.Value = value;
37  }
38  }
39  }
40 
41  internal IContent BoxedStructureOwner { get; set; }
42 
43  internal object[] BoxedStructureOwnerIndices { get; set; }
44 
45  private static object FetchItem(object enumerable, object index)
46  {
47  var list = enumerable as IList;
48  if (list != null && index is int)
49  return list[(int)index];
50 
51  var dictionary = enumerable as IDictionary;
52  if (dictionary != null)
53  return dictionary[index];
54 
55  var type = enumerable.GetType();
56  if (type.HasInterface(typeof(IDictionary<,>)))
57  {
58  var keyType = type.GetInterface(typeof(IDictionary<,>)).GetGenericArguments()[0];
59  var valueType = type.GetInterface(typeof(IDictionary<,>)).GetGenericArguments()[0];
60  var indexerMethod = type.GetProperty("Item", valueType, new[] { keyType });
61  return indexerMethod.GetValue(enumerable, new [] { index });
62  }
63  throw new ArgumentException(@"Enumerable object has no indexing and is not supported.", "enumerable");
64  }
65 
66  private static void SetItem(object enumerable, object index, object value)
67  {
68  var list = enumerable as IList;
69  if (list != null && index is int)
70  {
71  list[(int)index] = value;
72  return;
73  }
74 
75  var dictionary = enumerable as IDictionary;
76  if (dictionary != null)
77  {
78  dictionary[index] = value;
79  return;
80  }
81 
82  var type = enumerable.GetType();
83  if (type.HasInterface(typeof(IDictionary<,>)))
84  {
85  var keyType = type.GetInterface(typeof(IDictionary<,>)).GetGenericArguments()[0];
86  var valueType = type.GetInterface(typeof(IDictionary<,>)).GetGenericArguments()[0];
87  var indexerMethod = type.GetProperty("Item", valueType, new[] { keyType });
88  indexerMethod.SetValue(enumerable, value, new[] { index });
89  return;
90  }
91  throw new ArgumentException(@"Enumerable object has no indexing and is not supported.", "enumerable");
92  }
93  }
94 }
Content of a IModelNode.
Definition: IContent.cs:13
An implementation of IContent that gives access to an object or a boxed struct.
Provides access members of a type.
BoxedContent(object value, ITypeDescriptor descriptor, bool isPrimitive)
Definition: BoxedContent.cs:13