Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShadowObject.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.Runtime.CompilerServices;
5 
6 namespace SiliconStudio.Core.Reflection
7 {
8  /// <summary>
9  /// Allows to attach dynamic properties to an object at runtime.
10  /// </summary>
11  public static class ShadowObject
12  {
13  // Use a conditional weak table in order to attach properties and to
14  private static readonly ConditionalWeakTable<object, ShadowContainer> Shadows = new ConditionalWeakTable<object, ShadowContainer>();
15 
16  /// <summary>
17  /// Tries to get the value of a dynamic property.
18  /// </summary>
19  /// <typeparam name="T">Type of the value</typeparam>
20  /// <param name="instance">The instance object.</param>
21  /// <param name="memberKey">The member key.</param>
22  /// <param name="attributeKey">The attribute key.</param>
23  /// <param name="value">The value attached.</param>
24  /// <returns><c>true</c> if there is a value attached, <c>false</c> otherwise.</returns>
25  /// <exception cref="System.ArgumentNullException">
26  /// instance
27  /// or
28  /// memberKey
29  /// or
30  /// attributeKey
31  /// </exception>
32  public static bool TryGetDynamicProperty<T>(this object instance, object memberKey, PropertyKey<T> attributeKey, out T value)
33  {
34  if (instance == null) throw new ArgumentNullException("instance");
35  if (memberKey == null) throw new ArgumentNullException("memberKey");
36  if (attributeKey == null) throw new ArgumentNullException("attributeKey");
37 
38  ShadowContainer shadow;
39  ShadowAttributes attributes;
40  value = default(T);
41  return (Shadows.TryGetValue(instance, out shadow) && shadow.TryGetAttributes(memberKey, out attributes) && attributes.TryGetAttribute(attributeKey, out value));
42  }
43 
44  /// <summary>
45  /// Sets a dynamic property.
46  /// </summary>
47  /// <typeparam name="T">Type of the value</typeparam>
48  /// <param name="instance">The instance object.</param>
49  /// <param name="memberKey">The member key.</param>
50  /// <param name="attributeKey">The attribute key.</param>
51  /// <param name="value">The value.</param>
52  /// <exception cref="System.ArgumentNullException">
53  /// instance
54  /// or
55  /// memberKey
56  /// or
57  /// attributeKey
58  /// </exception>
59  public static void SetDynamicProperty<T>(this object instance, object memberKey, PropertyKey<T> attributeKey, T value)
60  {
61  if (instance == null) throw new ArgumentNullException("instance");
62  if (memberKey == null) throw new ArgumentNullException("memberKey");
63  if (attributeKey == null) throw new ArgumentNullException("attributeKey");
64  Shadows.GetOrCreateValue(instance)[memberKey].SetAttribute(attributeKey, value);
65  }
66  }
67 }
Allows to attach dynamic properties to an object at runtime.
Definition: ShadowObject.cs:11
A class that represents a typed tag propety.
Definition: PropertyKey.cs:138