2
0

ReflectionDescriptor.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Reflection;
  2. using Jint.Native;
  3. using Jint.Runtime.Interop.Reflection;
  4. namespace Jint.Runtime.Descriptors.Specialized
  5. {
  6. internal sealed class ReflectionDescriptor : PropertyDescriptor
  7. {
  8. private readonly Engine _engine;
  9. private readonly ReflectionAccessor _reflectionAccessor;
  10. private readonly object _target;
  11. public ReflectionDescriptor(
  12. Engine engine,
  13. ReflectionAccessor reflectionAccessor,
  14. object target)
  15. : base(PropertyFlag.Enumerable | PropertyFlag.CustomJsValue)
  16. {
  17. _engine = engine;
  18. _reflectionAccessor = reflectionAccessor;
  19. _target = target;
  20. Writable = reflectionAccessor.Writable && engine.Options.Interop.AllowWrite;
  21. }
  22. protected internal override JsValue CustomValue
  23. {
  24. get
  25. {
  26. var value = _reflectionAccessor.GetValue(_engine, _target);
  27. return JsValue.FromObject(_engine, value);
  28. }
  29. set
  30. {
  31. try
  32. {
  33. _reflectionAccessor.SetValue(_engine, _target, value);
  34. }
  35. catch (TargetInvocationException exception)
  36. {
  37. ExceptionHelper.ThrowMeaningfulException(_engine, exception);
  38. }
  39. }
  40. }
  41. }
  42. }