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.

115 lines
3.9 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. using System;
  26. using Newtonsoft.Json.Serialization;
  27. using System.Reflection;
  28. #if NET20
  29. using Newtonsoft.Json.Utilities.LinqBridge;
  30. #endif
  31. namespace Newtonsoft.Json.Utilities
  32. {
  33. internal class LateBoundReflectionDelegateFactory : ReflectionDelegateFactory
  34. {
  35. private static readonly LateBoundReflectionDelegateFactory _instance = new LateBoundReflectionDelegateFactory();
  36. internal static ReflectionDelegateFactory Instance => _instance;
  37. public override ObjectConstructor<object> CreateParameterizedConstructor(MethodBase method)
  38. {
  39. ValidationUtils.ArgumentNotNull(method, nameof(method));
  40. ConstructorInfo c = method as ConstructorInfo;
  41. if (c != null)
  42. {
  43. // don't convert to method group to avoid medium trust issues
  44. // https://github.com/JamesNK/Newtonsoft.Json/issues/476
  45. return a =>
  46. {
  47. object[] args = a;
  48. return c.Invoke(args);
  49. };
  50. }
  51. return a => method.Invoke(null, a);
  52. }
  53. public override MethodCall<T, object> CreateMethodCall<T>(MethodBase method)
  54. {
  55. ValidationUtils.ArgumentNotNull(method, nameof(method));
  56. ConstructorInfo c = method as ConstructorInfo;
  57. if (c != null)
  58. {
  59. return (o, a) => c.Invoke(a);
  60. }
  61. return (o, a) => method.Invoke(o, a);
  62. }
  63. public override Func<T> CreateDefaultConstructor<T>(Type type)
  64. {
  65. ValidationUtils.ArgumentNotNull(type, nameof(type));
  66. if (type.IsValueType())
  67. {
  68. return () => (T)Activator.CreateInstance(type);
  69. }
  70. ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true);
  71. return () => (T)constructorInfo.Invoke(null);
  72. }
  73. public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)
  74. {
  75. ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
  76. return o => propertyInfo.GetValue(o, null);
  77. }
  78. public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
  79. {
  80. ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo));
  81. return o => fieldInfo.GetValue(o);
  82. }
  83. public override Action<T, object> CreateSet<T>(FieldInfo fieldInfo)
  84. {
  85. ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo));
  86. return (o, v) => fieldInfo.SetValue(o, v);
  87. }
  88. public override Action<T, object> CreateSet<T>(PropertyInfo propertyInfo)
  89. {
  90. ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
  91. return (o, v) => propertyInfo.SetValue(o, v, null);
  92. }
  93. }
  94. }