Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
1.4 KiB

using System;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.FieldAccess {
/// <summary>
/// Accessor for fields representing a non-repeated enum value.
/// </summary>
internal sealed class SingleEnumAccessor : SinglePrimitiveAccessor {
private readonly EnumDescriptor enumDescriptor;
internal SingleEnumAccessor(FieldDescriptor field, string name, Type messageType, Type builderType)
: base(name, messageType, builderType) {
enumDescriptor = field.EnumType;
}
/// <summary>
/// Returns an EnumValueDescriptor representing the value in the builder.
/// Note that if an enum has multiple values for the same number, the descriptor
/// for the first value with that number will be returned.
/// </summary>
public override object GetValue(IMessage message) {
// Note: This relies on the fact that the CLR allows unboxing from an enum to
// its underlying value
int rawValue = (int) base.GetValue(message);
return enumDescriptor.FindValueByNumber(rawValue);
}
/// <summary>
/// Sets the value as an enum (via an int) in the builder,
/// from an EnumValueDescriptor parameter.
/// </summary>
public override void SetValue(IBuilder builder, object value) {
EnumValueDescriptor valueDescriptor = (EnumValueDescriptor) value;
base.SetValue(builder, valueDescriptor.Number);
}
}
}