Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Tracked by #93172 ...
epsitec opened this issue Oct 4, 2016 · 11 comments
Labels
area-System.Reflection bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@epsitec
Copy link
Contributor

epsitec commented Oct 4, 2016

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

@epsitec
Copy link
Contributor Author

epsitec commented Oct 4, 2016

@sepidehms do you think you can have a look at this issue?

I feel that these tests should be updated to include also the System.DateTime scenario.

@epsitec
Copy link
Contributor Author

epsitec commented Oct 4, 2016

@justinvp would you like to update the ParameterInfoTests in corefx to cover this issue?

@karelz
Copy link
Member

karelz commented Oct 4, 2016

Is the described behavior from desktop .NET Framework? Or just general desire?

@carusology
Copy link

carusology commented Oct 6, 2016

I have also run into this on .NET Core.

The DefaultValue and RawDefaultValue properties also throw the same FormatException when the ParameterInfo.ParameterType property is of type System.DateTime.

The only way I see to avoid it is having my reflection code step around DateTime parameters.

private static object GetDefaultValue(ParameterInfo parameter) {
    return parameter.ParameterType == typeof(DateTime) ? default(DateTime) : parameter.DefaultValue
}

var defaultValues =
     typeof(Foo)
        .GetConstructors()[0]
        .GetParameters()
        .Select(GetDefaultValue)
        .ToList();

@carusology
Copy link

Guid also has issues. The DefaultValue property on ParameterInfo is null when the default value is default(Guid). It doesn't throw when you access it, but it should be equivalent to Guid.Empty.

@epsitec
Copy link
Contributor Author

epsitec commented Oct 7, 2016

@karelz in my case, I want this to work both on the .NET Framework and on .NET Core.

@karelz
Copy link
Member

karelz commented Mar 1, 2017

Looks related to #18599

Next step: Root-cause (under debugger) and propose a fix.

@pakrym
Copy link
Contributor

pakrym commented Jun 26, 2017

tillig referenced this issue in autofac/Autofac Oct 16, 2017
dotnet/corefx#17943
dotnet/corefx#12338
dotnet/corefx#11797
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@steveharter steveharter modified the milestones: Future, 5.0 Feb 11, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@steveharter steveharter removed the untriaged New issue has not been triaged by the area owner label Mar 10, 2020
@SingleAccretion
Copy link
Contributor

SingleAccretion commented Jul 16, 2020

@steveharter Given dotnet/coreclr#17877 has been merged for quite some time now and the titular bug does not repro on .NET Core 3.1, maybe it is time to close this issue and remove it from the reflection roadmap for 5.0?

@danmoseley
Copy link
Member

I'm going to resolve based on comment above.

@danmoseley
Copy link
Member

I'm going to resolve based on comment above.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

9 participants