Skip to content

ParameterInfo.HasDefaultValue throws System.FormatException on optional DateTime parameter #18844

@epsitec

Description

@epsitec

When accessing ParameterInfo.HasDefaultValue for a parameter of type System.DateTime, I
get an unexpected System.FormatException.

ParameterInfo.Attributes is correctly set to Optional | HasDefault.

If the parameter is of any other value type (e.g. decimal or some custom struct) the
property works as expected. There is something specific to DateTime in the implementation
of RuntimeParameterInfo.GetDefaultValueInternal() which is broken.

Code snippet to test:

public void Usage()
{
    var type = typeof (Foo);
    var ctor = type.GetConstructors ()[0];
    var para = ctor.GetParameters ()[0];
    // This throws an exception...
    Assert.True (para.HasDefaultValue);
}

static class Foo
{
    public Foo(System.DateTime value = default (System.DateTime)) { }
}

Exception:

default-value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions