dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

ParameterInfo.DefaultValue may return Type.Missing

Open OwnageIsMagic opened this issue 2 years ago • 2 comments

ParameterInfo.DefaultValue may return Type.Missing if parameter is optional, but no default value in metadata (probably COM interop case). Same for ParameterInfo.RawDefaultValue

See implementation

https://github.com/dotnet/runtime/blob/v7.0.13/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs#L287-L298

OwnageIsMagic avatar Nov 08 '23 22:11 OwnageIsMagic

Basically, having a default value sets the Optional attribute for the parameter. Though one can set it using [Optional] attribute even without default value. Also, it can be set with reflection emit without a default value and result Type.Missing

public void Method([Optional]string parameter) { }

buyaa-n avatar Nov 09 '23 22:11 buyaa-n

See also https://github.com/dotnet/runtime/issues/100322

steveharter avatar Mar 27 '24 20:03 steveharter