dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.71k forks source link

DefaultValueAttribute return null value for Guid type. #18599

Open Jobria opened 8 years ago

Jobria commented 8 years ago

I added the DefaultValueAttribute into the a Guid property and get the attribute in constructor of the object but the value in the attribute is null. I have also tried another type of attribute like "int", "long", "flost" or etc. It works great. Anyone have idea what's wrong of the Guid type?

The following is the coding for reference.

        [DefaultValue(typeof(Guid), "8DAEDD83-289E-417C-AAAC-D48D77E0D84C")]
        public Guid DefaultGuidField { get; set; }

        [DefaultValue(typeof(int), "9")]
        public int DefaultIntField { get; set; }

        [DefaultValue(typeof(long), "4611686018427387903")]
        public long DefaultLongField { get; set; }

The retrieve value in constructor like this

        var defaultValue = propertyInfo.GetCustomAttribute<DefaultValueAttribute>(true);

        if (defaultValue != null)
        {
            Type defaultValueType = defaultValue.Value.GetType();
karelz commented 7 years ago

Next step: Root cause with the help of debugger and fix it.

carusology commented 7 years ago

@karelz any progress here?

karelz commented 7 years ago

@carusology there is currently no action planned from our side (milestone is Future, it is not assigned to anyone and it is marked as up-for-grabs). Any particular reason why you're looking for progress? The impact seems to be small. I don't see any specific reason why this bug should be higher priority than the rest of the ~2,000 bugs we have in the repo ...

carusology commented 7 years ago

Thanks for the update, @karelz.

We have a hack in the code with a comment that points to this issue as the root cause. From my perspective it is not critical, just a "nice to have."

JonHanna commented 6 years ago

To judge from comparing reference source and corefx:

Original NetFX implementation used TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value) which would work fine with typeof(Guid) for typeand ""8DAEDD83-289E-417C-AAAC-D48D77E0D84C" for value.

Silverlight implementation would see if it could use Enum.Parse, then see if it could use TimeSpan.Parse and finally if the type belonged to the same module as string (which is true for Guid) attempt Convert.ChangeType() (which won't work for Guid).

CoreFX implementation is the Silverlight implementation, but with the same-module-as-string restriction. But since that still won't work, that is moot.

Changing to copy the NetFX approach would seem the most obvious step, but alas TypeDescriptor lives in corefx while DefaultValueAttribute lives in the shared coreclr/corert code

ghost commented 6 years ago

I don't see a Reflection issue here - the issue is the implementation of DefaultValueAttribute

iSazonov commented 4 years ago

I create simple application and can not reproduce the issue. Perhaps the issue came from how type converters was packaged to assemblies. I think the issue can be closed. Although maybe there are more tests needed, but that's another story. /cc @ericstj