MessageUtil.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. namespace Google.ProtocolBuffers {
  6. /// <summary>
  7. /// Utilities for arbitrary messages of an unknown type. This class does not use
  8. /// generics precisely because it is designed for dynamically discovered types.
  9. /// </summary>
  10. public static class MessageUtil {
  11. /// <summary>
  12. /// Returns the default message for the given type. If an exception is thrown
  13. /// (directly from this code), the message will be suitable to be displayed to a user.
  14. /// </summary>
  15. /// <param name="type"></param>
  16. /// <exception cref="ArgumentNullException">The type parameter is null.</exception>
  17. /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
  18. /// have a static DefaultMessage property of the same type, or is generic or abstract.</exception>
  19. /// <returns></returns>
  20. public static IMessage GetDefaultMessage(Type type) {
  21. if (type == null) {
  22. throw new ArgumentNullException("type", "No type specified.");
  23. }
  24. if (type.IsAbstract || type.IsGenericTypeDefinition) {
  25. throw new ArgumentException("Unable to get a default message for an abstract or generic type (" + type.FullName + ")");
  26. }
  27. if (!typeof(IMessage).IsAssignableFrom(type)) {
  28. throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName + ")");
  29. }
  30. PropertyInfo property = type.GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
  31. if (property == null) {
  32. throw new ArgumentException(type.FullName + " doesn't have a static DefaultInstance property");
  33. }
  34. if (property.PropertyType != type) {
  35. throw new ArgumentException(type.FullName + ".DefaultInstance property is of the wrong type");
  36. }
  37. return (IMessage) property.GetValue(null, null);
  38. }
  39. /// <summary>
  40. /// Returns the default message for the type with the given name. This is just
  41. /// a convenience wrapper around calling Type.GetType and then GetDefaultMessage.
  42. /// If an exception is thrown, the message will be suitable to be displayed to a user.
  43. /// </summary>
  44. /// <param name="typeName"></param>
  45. /// <exception cref="ArgumentNullException">The typeName parameter is null.</exception>
  46. /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
  47. /// have a static DefaultMessage property of the same type, or can't be found.</exception>
  48. public static IMessage GetDefaultMessage(string typeName) {
  49. if (typeName == null) {
  50. throw new ArgumentNullException("typeName", "No type name specified.");
  51. }
  52. Type type = Type.GetType(typeName);
  53. if (type == null) {
  54. throw new ArgumentException("Unable to load type " + typeName);
  55. }
  56. return GetDefaultMessage(type);
  57. }
  58. }
  59. }