1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System; 34 using System.Collections.Generic; 35 36 namespace Google.Protobuf.Reflection 37 { 38 /// <summary> 39 /// Descriptor for an enum type in a .proto file. 40 /// </summary> 41 public sealed class EnumDescriptor : DescriptorBase 42 { 43 private readonly EnumDescriptorProto proto; 44 private readonly MessageDescriptor containingType; 45 private readonly IList<EnumValueDescriptor> values; 46 private readonly Type clrType; 47 EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType)48 internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, Type clrType) 49 : base(file, file.ComputeFullName(parent, proto.Name), index) 50 { 51 this.proto = proto; 52 this.clrType = clrType; 53 containingType = parent; 54 55 if (proto.Value.Count == 0) 56 { 57 // We cannot allow enums with no values because this would mean there 58 // would be no valid default value for fields of this type. 59 throw new DescriptorValidationException(this, "Enums must contain at least one value."); 60 } 61 62 values = DescriptorUtil.ConvertAndMakeReadOnly(proto.Value, 63 (value, i) => new EnumValueDescriptor(value, file, this, i)); 64 65 File.DescriptorPool.AddSymbol(this); 66 } 67 68 internal EnumDescriptorProto Proto { get { return proto; } } 69 70 /// <summary> 71 /// The brief name of the descriptor's target. 72 /// </summary> 73 public override string Name { get { return proto.Name; } } 74 75 /// <summary> 76 /// The CLR type for this enum. For generated code, this will be a CLR enum type. 77 /// </summary> 78 public Type ClrType { get { return clrType; } } 79 80 /// <value> 81 /// If this is a nested type, get the outer descriptor, otherwise null. 82 /// </value> 83 public MessageDescriptor ContainingType 84 { 85 get { return containingType; } 86 } 87 88 /// <value> 89 /// An unmodifiable list of defined value descriptors for this enum. 90 /// </value> 91 public IList<EnumValueDescriptor> Values 92 { 93 get { return values; } 94 } 95 96 /// <summary> 97 /// Finds an enum value by number. If multiple enum values have the 98 /// same number, this returns the first defined value with that number. 99 /// If there is no value for the given number, this returns <c>null</c>. 100 /// </summary> FindValueByNumber(int number)101 public EnumValueDescriptor FindValueByNumber(int number) 102 { 103 return File.DescriptorPool.FindEnumValueByNumber(this, number); 104 } 105 106 /// <summary> 107 /// Finds an enum value by name. 108 /// </summary> 109 /// <param name="name">The unqualified name of the value (e.g. "FOO").</param> 110 /// <returns>The value's descriptor, or null if not found.</returns> FindValueByName(string name)111 public EnumValueDescriptor FindValueByName(string name) 112 { 113 return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name); 114 } 115 } 116 }