1
#region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc. All rights reserved.
4 // https://developers.google.com/protocol-buffers/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
33 using System
.Collections
.Generic
;
34 using System
.Collections
.ObjectModel
;
35 using Google
.Protobuf
.Compatibility
;
37 namespace Google
.Protobuf
.Reflection
40 /// Describes a "oneof" field collection in a message type: a set of
41 /// fields of which at most one can be set in any particular message.
43 public sealed class OneofDescriptor
: DescriptorBase
45 private readonly OneofDescriptorProto proto
;
46 private MessageDescriptor containingType
;
47 private IList
<FieldDescriptor
> fields
;
48 private readonly OneofAccessor accessor
;
50 internal OneofDescriptor(OneofDescriptorProto proto
, FileDescriptor file
, MessageDescriptor parent
, int index
, string clrName
)
51 : base(file
, file
.ComputeFullName(parent
, proto
.Name
), index
)
54 containingType
= parent
;
56 file
.DescriptorPool
.AddSymbol(this);
57 accessor
= CreateAccessor(clrName
);
61 /// The brief name of the descriptor's target.
63 public override string Name { get { return proto.Name; }
}
66 /// Gets the message type containing this oneof.
69 /// The message type containing this oneof.
71 public MessageDescriptor ContainingType
73 get { return containingType; }
77 /// Gets the fields within this oneof, in declaration order.
80 /// The fields within this oneof, in declaration order.
82 public IList
<FieldDescriptor
> Fields { get { return fields; }
}
85 /// Gets an accessor for reflective access to the values associated with the oneof
86 /// in a particular message.
89 /// The accessor used for reflective access, or <c>null</c> if reflection is not
90 /// supported by this descriptor.
92 public OneofAccessor Accessor { get { return accessor; }
}
94 internal void CrossLink()
96 List
<FieldDescriptor
> fieldCollection
= new List
<FieldDescriptor
>();
97 foreach (var field
in ContainingType
.Fields
.InDeclarationOrder())
99 if (field
.ContainingOneof
== this)
101 fieldCollection
.Add(field
);
104 fields
= new ReadOnlyCollection
<FieldDescriptor
>(fieldCollection
);
107 private OneofAccessor
CreateAccessor(string clrName
)
109 if (containingType
.GeneratedType
== null || clrName
== null)
113 var caseProperty
= containingType
.GeneratedType
.GetProperty(clrName
+ "Case");
114 if (caseProperty
== null)
116 throw new DescriptorValidationException(this, "Property " + clrName
+ "Case not found in " + containingType
.GeneratedType
);
118 var clearMethod
= containingType
.GeneratedType
.GetMethod("Clear" + clrName
);
119 if (clearMethod
== null)
121 throw new DescriptorValidationException(this, "Method Clear" + clrName
+ " not found in " + containingType
.GeneratedType
);
124 return new OneofAccessor(caseProperty
, clearMethod
, this);