SingleMessageAccessor.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Reflection;
  18. namespace Google.ProtocolBuffers.FieldAccess {
  19. /// <summary>
  20. /// Accessor for fields representing a non-repeated message value.
  21. /// </summary>
  22. internal sealed class SingleMessageAccessor : SinglePrimitiveAccessor {
  23. /// <summary>
  24. /// The static method to create a builder for the property type. For example,
  25. /// in a message type "Foo", a field called "bar" might be of type "Baz". This
  26. /// method is Baz.CreateBuilder.
  27. /// </summary>
  28. private readonly MethodInfo createBuilderMethod;
  29. internal SingleMessageAccessor(string name, Type messageType, Type builderType)
  30. : base(name, messageType, builderType) {
  31. createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);//BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
  32. if (createBuilderMethod == null) {
  33. throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name);
  34. }
  35. }
  36. /// <summary>
  37. /// Creates a message of the appropriate CLR type from the given value,
  38. /// which may already be of the right type or may be a dynamic message.
  39. /// </summary>
  40. private object CoerceType(object value) {
  41. // If it's already of the right type, we're done
  42. if (ClrType.IsInstanceOfType(value)) {
  43. return value;
  44. }
  45. // No... so let's create a builder of the right type, and merge the value in.
  46. IMessage message = (IMessage) value;
  47. return CreateBuilder().WeakMergeFrom(message).WeakBuild();
  48. }
  49. public override void SetValue(IBuilder builder, object value) {
  50. base.SetValue(builder, CoerceType(value));
  51. }
  52. public override IBuilder CreateBuilder() {
  53. return (IBuilder) createBuilderMethod.Invoke(null, null);
  54. }
  55. }
  56. }