/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs

https://gitlab.com/github-cloud-corporation/protobuf · C# · 107 lines · 45 code · 6 blank · 56 comment · 0 complexity · bb2c23d1deb0e5edb1ec905174eea926 MD5 · raw file

  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Linq.Expressions;
  35. using System.Reflection;
  36. namespace Google.Protobuf.Reflection
  37. {
  38. /// <summary>
  39. /// The methods in this class are somewhat evil, and should not be tampered with lightly.
  40. /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos
  41. /// which are more strongly typed. They do this by creating an appropriate strongly typed
  42. /// delegate from the MethodInfo, and then calling that within an anonymous method.
  43. /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are
  44. /// very fast compared with calling Invoke later on.
  45. /// </summary>
  46. internal static class ReflectionUtil
  47. {
  48. /// <summary>
  49. /// Empty Type[] used when calling GetProperty to force property instead of indexer fetching.
  50. /// </summary>
  51. internal static readonly Type[] EmptyTypes = new Type[0];
  52. /// <summary>
  53. /// Creates a delegate which will cast the argument to the appropriate method target type,
  54. /// call the method on it, then convert the result to object.
  55. /// </summary>
  56. internal static Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method)
  57. {
  58. ParameterExpression parameter = Expression.Parameter(typeof(IMessage), "p");
  59. Expression downcast = Expression.Convert(parameter, method.DeclaringType);
  60. Expression call = Expression.Call(downcast, method);
  61. Expression upcast = Expression.Convert(call, typeof(object));
  62. return Expression.Lambda<Func<IMessage, object>>(upcast, parameter).Compile();
  63. }
  64. /// <summary>
  65. /// Creates a delegate which will cast the argument to the appropriate method target type,
  66. /// call the method on it, then convert the result to the specified type.
  67. /// </summary>
  68. internal static Func<IMessage, T> CreateFuncIMessageT<T>(MethodInfo method)
  69. {
  70. ParameterExpression parameter = Expression.Parameter(typeof(IMessage), "p");
  71. Expression downcast = Expression.Convert(parameter, method.DeclaringType);
  72. Expression call = Expression.Call(downcast, method);
  73. Expression upcast = Expression.Convert(call, typeof(T));
  74. return Expression.Lambda<Func<IMessage, T>>(upcast, parameter).Compile();
  75. }
  76. /// <summary>
  77. /// Creates a delegate which will execute the given method after casting the first argument to
  78. /// the target type of the method, and the second argument to the first parameter type of the method.
  79. /// </summary>
  80. internal static Action<IMessage, object> CreateActionIMessageObject(MethodInfo method)
  81. {
  82. ParameterExpression targetParameter = Expression.Parameter(typeof(IMessage), "target");
  83. ParameterExpression argParameter = Expression.Parameter(typeof(object), "arg");
  84. Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType);
  85. Expression castArgument = Expression.Convert(argParameter, method.GetParameters()[0].ParameterType);
  86. Expression call = Expression.Call(castTarget, method, castArgument);
  87. return Expression.Lambda<Action<IMessage, object>>(call, targetParameter, argParameter).Compile();
  88. }
  89. /// <summary>
  90. /// Creates a delegate which will execute the given method after casting the first argument to
  91. /// the target type of the method.
  92. /// </summary>
  93. internal static Action<IMessage> CreateActionIMessage(MethodInfo method)
  94. {
  95. ParameterExpression targetParameter = Expression.Parameter(typeof(IMessage), "target");
  96. Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType);
  97. Expression call = Expression.Call(castTarget, method);
  98. return Expression.Lambda<Action<IMessage>>(call, targetParameter).Compile();
  99. }
  100. }
  101. }