Class MessageDescriptor (3.23.0)

public sealed class MessageDescriptor : DescriptorBase, IDescriptor

Describes a message type.

Inheritance

object > DescriptorBase > MessageDescriptor

Implements

IDescriptor

Namespace

Google.Protobuf.Reflection

Assembly

Google.Protobuf.dll

Properties

ClrType

public Type ClrType { get; }

The CLR type used to represent message instances from this descriptor.

Property Value
TypeDescription
Type
Remarks

The value returned by this property will be non-null for all regular fields. However, if a message containing a map field is introspected, the list of nested messages will include an auto-generated nested key/value pair message for the field. This is not represented in any generated type, so this property will return null in such cases.

For wrapper types (StringValue and the like), the type returned here will be the generated message type, not the native type used by reflection for fields of those types. Code using reflection should call IsWrapperType to determine whether a message descriptor represents a wrapper type, and handle the result appropriately.

ContainingType

public MessageDescriptor ContainingType { get; }
Property Value
TypeDescription
MessageDescriptor

If this is a nested type, get the outer descriptor, otherwise null.

CustomOptions

[Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")]
public CustomOptions CustomOptions { get; }

The (possibly empty) set of custom options for this message.

Property Value
TypeDescription
CustomOptions

EnumTypes

public IList<EnumDescriptor> EnumTypes { get; }
Property Value
TypeDescription
IListEnumDescriptor

An unmodifiable list of this message type's enum types.

Extensions

public ExtensionCollection Extensions { get; }

An unmodifiable list of extensions defined in this message's scope. Note that some extensions may be incomplete (FieldDescriptor.Extension may be null) if they are declared in a file generated using a version of protoc that did not fully support extensions in C#.

Property Value
TypeDescription
ExtensionCollection

Fields

public MessageDescriptor.FieldCollection Fields { get; }
Property Value
TypeDescription
MessageDescriptorFieldCollection

A collection of fields, which can be retrieved by name or field number.

Name

public override string Name { get; }

The brief name of the descriptor's target.

Property Value
TypeDescription
string
Overrides

NestedTypes

public IList<MessageDescriptor> NestedTypes { get; }
Property Value
TypeDescription
IListMessageDescriptor

An unmodifiable list of this message type's nested types.

Oneofs

public IList<OneofDescriptor> Oneofs { get; }
Property Value
TypeDescription
IListOneofDescriptor

An unmodifiable list of the "oneof" field collections in this message type. All "real" oneofs (where IsSynthetic returns false) come before synthetic ones.

Parser

public MessageParser Parser { get; }

A parser for this message type.

Property Value
TypeDescription
MessageParser
Remarks

As MessageDescriptor is not generic, this cannot be statically typed to the relevant type, but it should produce objects of a type compatible with ClrType.

The value returned by this property will be non-null for all regular fields. However, if a message containing a map field is introspected, the list of nested messages will include an auto-generated nested key/value pair message for the field. No message parser object is created for such messages, so this property will return null in such cases.

For wrapper types (StringValue and the like), the parser returned here will be the generated message type, not the native type used by reflection for fields of those types. Code using reflection should call IsWrapperType to determine whether a message descriptor represents a wrapper type, and handle the result appropriately.

RealOneofCount

public int RealOneofCount { get; }

The number of real "oneof" descriptors in this message type. Every element in Oneofs with an index less than this will have a IsSynthetic property value of false; every element with an index greater than or equal to this will have a IsSynthetic property value of true.

Property Value
TypeDescription
int

Methods

FindDescriptor<T>(string)

public T FindDescriptor<T>(string name) where T : class, IDescriptor

Finds a nested descriptor by name. The is valid for fields, nested message types, oneofs and enums.

Parameter
NameDescription
namestring

The unqualified name of the descriptor, e.g. "Foo"

Returns
TypeDescription
T

The descriptor, or null if not found.

Type Parameter
NameDescription
T

FindFieldByName(string)

public FieldDescriptor FindFieldByName(string name)

Finds a field by field name.

Parameter
NameDescription
namestring

The unqualified name of the field (e.g. "foo").

Returns
TypeDescription
FieldDescriptor

The field's descriptor, or null if not found.

FindFieldByNumber(int)

public FieldDescriptor FindFieldByNumber(int number)

Finds a field by field number.

Parameter
NameDescription
numberint

The field number within this message type.

Returns
TypeDescription
FieldDescriptor

The field's descriptor, or null if not found.

GetOption<T>(Extension<MessageOptions, T>)

[Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
public T GetOption<T>(Extension<MessageOptions, T> extension)

Gets a single value message option for this descriptor

Parameter
NameDescription
extensionExtensionMessageOptions
Returns
TypeDescription
T
Type Parameter
NameDescription
T

GetOption<T>(RepeatedExtension<MessageOptions, T>)

[Obsolete("GetOption is obsolete. Use the GetOptions() method.")]
public RepeatedField<T> GetOption<T>(RepeatedExtension<MessageOptions, T> extension)

Gets a repeated value message option for this descriptor

Parameter
NameDescription
extensionRepeatedExtensionMessageOptions
Returns
TypeDescription
RepeatedField
Type Parameter
NameDescription
T

GetOptions()

public MessageOptions GetOptions()

The MessageOptions, defined in descriptor.proto. If the options message is not present (i.e. there are no options), null is returned. Custom options can be retrieved as extensions of the returned message. NOTE: A defensive copy is created each time this property is retrieved.

Returns
TypeDescription
MessageOptions

ToProto()

public DescriptorProto ToProto()

Returns a clone of the underlying DescriptorProto describing this message. Note that a copy is taken every time this method is called, so clients using it frequently (and not modifying it) may want to cache the returned value.

Returns
TypeDescription
DescriptorProto

A protobuf representation of this message descriptor.