/src/ProtocolBuffers/MessageUtil.cs

https://code.google.com/p/protobuf-csharp-port/ · C# · 109 lines · 51 code · 5 blank · 53 comment · 13 complexity · cdecb1b7b2d5b38492f3617e66338995 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. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Reflection;
  36. namespace Google.ProtocolBuffers
  37. {
  38. /// <summary>
  39. /// Utilities for arbitrary messages of an unknown type. This class does not use
  40. /// generics precisely because it is designed for dynamically discovered types.
  41. /// </summary>
  42. public static class MessageUtil
  43. {
  44. /// <summary>
  45. /// Returns the default message for the given type. If an exception is thrown
  46. /// (directly from this code), the message will be suitable to be displayed to a user.
  47. /// </summary>
  48. /// <param name="type"></param>
  49. /// <exception cref="ArgumentNullException">The type parameter is null.</exception>
  50. /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
  51. /// have a static DefaultMessage property of the same type, or is generic or abstract.</exception>
  52. /// <returns></returns>
  53. public static IMessage GetDefaultMessage(Type type)
  54. {
  55. if (type == null)
  56. {
  57. throw new ArgumentNullException("type", "No type specified.");
  58. }
  59. if (type.IsAbstract || type.IsGenericTypeDefinition)
  60. {
  61. throw new ArgumentException("Unable to get a default message for an abstract or generic type (" +
  62. type.FullName + ")");
  63. }
  64. if (!typeof (IMessage).IsAssignableFrom(type))
  65. {
  66. throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName +
  67. ")");
  68. }
  69. PropertyInfo property = type.GetProperty("DefaultInstance",
  70. BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  71. if (property == null)
  72. {
  73. throw new ArgumentException(type.FullName + " doesn't have a static DefaultInstance property");
  74. }
  75. if (property.PropertyType != type)
  76. {
  77. throw new ArgumentException(type.FullName + ".DefaultInstance property is of the wrong type");
  78. }
  79. return (IMessage) property.GetValue(null, null);
  80. }
  81. /// <summary>
  82. /// Returns the default message for the type with the given name. This is just
  83. /// a convenience wrapper around calling Type.GetType and then GetDefaultMessage.
  84. /// If an exception is thrown, the message will be suitable to be displayed to a user.
  85. /// </summary>
  86. /// <param name="typeName"></param>
  87. /// <exception cref="ArgumentNullException">The typeName parameter is null.</exception>
  88. /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
  89. /// have a static DefaultMessage property of the same type, or can't be found.</exception>
  90. public static IMessage GetDefaultMessage(string typeName)
  91. {
  92. if (typeName == null)
  93. {
  94. throw new ArgumentNullException("typeName", "No type name specified.");
  95. }
  96. Type type = Type.GetType(typeName);
  97. if (type == null)
  98. {
  99. throw new ArgumentException("Unable to load type " + typeName);
  100. }
  101. return GetDefaultMessage(type);
  102. }
  103. }
  104. }