Class MessageDescriptor (3.27.1)

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
Type Description
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
Type Description
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
Type Description
CustomOptions

EnumTypes

public IList<EnumDescriptor> EnumTypes { get; }
Property Value
Type Description
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
Type Description
ExtensionCollection

Fields

public MessageDescriptor.FieldCollection Fields { get; }
Property Value
Type Description
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
Type Description
string
Overrides

NestedTypes

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

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

Oneofs

public IList<OneofDescriptor> Oneofs { get; }
Property Value
Type Description
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
Type Description
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
Type Description
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
Name Description
name string

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

Returns
Type Description
T

The descriptor, or null if not found.

Type Parameter
Name Description
T

FindFieldByName(string)

public FieldDescriptor FindFieldByName(string name)

Finds a field by field name.

Parameter
Name Description
name string

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

Returns
Type Description
FieldDescriptor

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

FindFieldByNumber(int)

public FieldDescriptor FindFieldByNumber(int number)

Finds a field by field number.

Parameter
Name Description
number int

The field number within this message type.

Returns
Type Description
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
Name Description
extension ExtensionMessageOptions
Returns
Type Description
T
Type Parameter
Name Description
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
Name Description
extension RepeatedExtensionMessageOptions
Returns
Type Description
RepeatedField
Type Parameter
Name Description
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
Type Description
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
Type Description
DescriptorProto

A protobuf representation of this message descriptor.