You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.5 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. #if !(NET20 || NET35)
  26. using System;
  27. using System.Collections.Generic;
  28. #if NET20
  29. using Newtonsoft.Json.Utilities.LinqBridge;
  30. #endif
  31. using System.Text;
  32. using System.Reflection;
  33. using Newtonsoft.Json.Utilities;
  34. using System.Globalization;
  35. namespace Newtonsoft.Json.Serialization
  36. {
  37. /// <summary>
  38. /// Get and set values for a <see cref="MemberInfo"/> using dynamic methods.
  39. /// </summary>
  40. internal class ExpressionValueProvider : IValueProvider
  41. {
  42. private readonly MemberInfo _memberInfo;
  43. private Func<object, object> _getter;
  44. private Action<object, object> _setter;
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="ExpressionValueProvider"/> class.
  47. /// </summary>
  48. /// <param name="memberInfo">The member info.</param>
  49. public ExpressionValueProvider(MemberInfo memberInfo)
  50. {
  51. ValidationUtils.ArgumentNotNull(memberInfo, nameof(memberInfo));
  52. _memberInfo = memberInfo;
  53. }
  54. /// <summary>
  55. /// Sets the value.
  56. /// </summary>
  57. /// <param name="target">The target to set the value on.</param>
  58. /// <param name="value">The value to set on the target.</param>
  59. public void SetValue(object target, object value)
  60. {
  61. try
  62. {
  63. if (_setter == null)
  64. {
  65. _setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(_memberInfo);
  66. }
  67. #if DEBUG
  68. // dynamic method doesn't check whether the type is 'legal' to set
  69. // add this check for unit tests
  70. if (value == null)
  71. {
  72. if (!ReflectionUtils.IsNullable(ReflectionUtils.GetMemberUnderlyingType(_memberInfo)))
  73. {
  74. throw new JsonSerializationException("Incompatible value. Cannot set {0} to null.".FormatWith(CultureInfo.InvariantCulture, _memberInfo));
  75. }
  76. }
  77. else if (!ReflectionUtils.GetMemberUnderlyingType(_memberInfo).IsAssignableFrom(value.GetType()))
  78. {
  79. throw new JsonSerializationException("Incompatible value. Cannot set {0} to type {1}.".FormatWith(CultureInfo.InvariantCulture, _memberInfo, value.GetType()));
  80. }
  81. #endif
  82. _setter(target, value);
  83. }
  84. catch (Exception ex)
  85. {
  86. throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the value.
  91. /// </summary>
  92. /// <param name="target">The target to get the value from.</param>
  93. /// <returns>The value.</returns>
  94. public object GetValue(object target)
  95. {
  96. try
  97. {
  98. if (_getter == null)
  99. {
  100. _getter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);
  101. }
  102. return _getter(target);
  103. }
  104. catch (Exception ex)
  105. {
  106. throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex);
  107. }
  108. }
  109. }
  110. }
  111. #endif