SingleMessageAccessor.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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<TMessage, TBuilder> : SinglePrimitiveAccessor<TMessage, TBuilder>
  23. where TMessage : IMessage<TMessage, TBuilder>
  24. where TBuilder : IBuilder<TMessage, TBuilder> {
  25. /// <summary>
  26. /// The static method to create a builder for the property type. For example,
  27. /// in a message type "Foo", a field called "bar" might be of type "Baz". This
  28. /// method is Baz.CreateBuilder.
  29. /// </summary>
  30. private readonly MethodInfo createBuilderMethod;
  31. internal SingleMessageAccessor(string name) : base(name) {
  32. createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);//BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
  33. if (createBuilderMethod == null) {
  34. throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name);
  35. }
  36. }
  37. /// <summary>
  38. /// Creates a message of the appropriate CLR type from the given value,
  39. /// which may already be of the right type or may be a dynamic message.
  40. /// </summary>
  41. private object CoerceType(object value) {
  42. // If it's already of the right type, we're done
  43. if (ClrType.IsInstanceOfType(value)) {
  44. return value;
  45. }
  46. // No... so let's create a builder of the right type, and merge the value in.
  47. IMessage message = (IMessage) value;
  48. return CreateBuilder().WeakMergeFrom(message).WeakBuild();
  49. }
  50. public override void SetValue(TBuilder builder, object value) {
  51. base.SetValue(builder, CoerceType(value));
  52. }
  53. public override IBuilder CreateBuilder() {
  54. return (IBuilder) createBuilderMethod.Invoke(null, null);
  55. }
  56. }
  57. }