# Class MessageDescriptor (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.Reflection.MessageDescriptor)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.Reflection.MessageDescriptor)

    public sealed class MessageDescriptor : DescriptorBase, IDescriptor

Describes a message type.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> [DescriptorBase](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase) \> MessageDescriptor

## Implements

[IDescriptor](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.IDescriptor)

## Inherited Members

[DescriptorBase.Index](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase#Google_Protobuf_Reflection_DescriptorBase_Index) [DescriptorBase.FullName](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase#Google_Protobuf_Reflection_DescriptorBase_FullName) [DescriptorBase.File](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase#Google_Protobuf_Reflection_DescriptorBase_File) [DescriptorBase.Declaration](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase#Google_Protobuf_Reflection_DescriptorBase_Declaration) [object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)) [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals) [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Namespace

[Google.Protobuf.Reflection](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/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** |
| `https://learn.microsoft.com/dotnet/api/system.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](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.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](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor#Google_Protobuf_Reflection_MessageDescriptor_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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.CustomOptions` |   |

### EnumTypes

    public IList<EnumDescriptor> EnumTypes { get; }

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.ilist-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.EnumDescriptor` | 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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.ExtensionCollection` |   |

### Fields

    public MessageDescriptor.FieldCollection Fields { get; }

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptorhttps://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor.FieldCollection` | 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** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

**Overrides** [DescriptorBase.Name](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorBase#Google_Protobuf_Reflection_DescriptorBase_Name)

### NestedTypes

    public IList<MessageDescriptor> NestedTypes { get; }

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.ilist-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor` | An unmodifiable list of this message type's nested types. |

### Oneofs

    public IList<OneofDescriptor> Oneofs { get; }

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.ilist-1https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.OneofDescriptor` | An unmodifiable list of the "oneof" field collections in this message type. All "real" oneofs (where [IsSynthetic](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.OneofDescriptor#Google_Protobuf_Reflection_OneofDescriptor_IsSynthetic) returns false) come before synthetic ones. |

### Parser

    public MessageParser Parser { get; }

A parser for this message type.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.MessageParser` |   |

**Remarks**
As [MessageDescriptor](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor) is not generic, this cannot be statically
typed to the relevant type, but it should produce objects of a type compatible with [ClrType](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor#Google_Protobuf_Reflection_MessageDescriptor_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](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WellKnownTypes.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](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor#Google_Protobuf_Reflection_MessageDescriptor_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](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageDescriptor#Google_Protobuf_Reflection_MessageDescriptor_Oneofs)
with an index less than this will have a [IsSynthetic](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.OneofDescriptor#Google_Protobuf_Reflection_OneofDescriptor_IsSynthetic) property value
of `false`; every element with an index greater than or equal to this will have a
[IsSynthetic](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.OneofDescriptor#Google_Protobuf_Reflection_OneofDescriptor_IsSynthetic) property value of `true`.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

## 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` | `https://learn.microsoft.com/dotnet/api/system.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` | `https://learn.microsoft.com/dotnet/api/system.string` The unqualified name of the field (e.g. "foo"). |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.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` | `https://learn.microsoft.com/dotnet/api/system.int32` The field number within this message type. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.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` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Extension-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageOptions` |

| **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` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.RepeatedExtension-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageOptions` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.RepeatedField-1` |   |

| **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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.MessageOptions` |   |

### ToProto()

    public DescriptorProto ToProto()

Returns a clone of the underlying [DescriptorProto](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.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** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Reflection.DescriptorProto` | A protobuf representation of this message descriptor. |