DynamicObjectInstance.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. namespace ShwasherSys.ReflectionMagic
  6. {
  7. public class DynamicObjectInstance : DynamicObjectBase
  8. {
  9. private static readonly ConcurrentDictionary<Type, IDictionary<string, IProperty>> _propertiesOnType = new ConcurrentDictionary<Type, IDictionary<string, IProperty>>();
  10. private readonly object _instance;
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="DynamicObjectInstance"/> class, wrapping the specified object.
  13. /// </summary>
  14. /// <param name="instance">The object to wrap.</param>
  15. /// <exception cref="ArgumentNullException">Thrown when <paramref name="instance"/> is <c>null</c>.</exception>
  16. public DynamicObjectInstance(object instance)
  17. {
  18. _instance = instance ?? throw new ArgumentNullException(nameof(instance));
  19. }
  20. protected override IDictionary<Type, IDictionary<string, IProperty>> PropertiesOnType => _propertiesOnType;
  21. // For instance calls, we get the type from the instance
  22. protected override Type TargetType => _instance.GetType();
  23. protected override object Instance => _instance;
  24. public override object RealObject => Instance;
  25. protected override BindingFlags BindingFlags => BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  26. }
  27. }