runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Missing test coverage for CustomConstantAttribute

Open MichalStrehovsky opened this issue 5 years ago • 6 comments

This is used in e.g. reflection invoke and Delegate.DynamicInvoke. The below program will print "Hello". We should ideally turn this into a test and add it somewhere around https://github.com/dotnet/runtime/blob/79ae74f5ca5c8a6fe3a48935e85bd7374959c570/src/libraries/System.Runtime/tests/System/DelegateTests.cs#L106.

using System;
using System.Runtime.CompilerServices;


Mine m = MyAttribute.Do;
m.DynamicInvoke(Type.Missing);

delegate void Mine([My] object o);

class MyAttribute : CustomConstantAttribute
{
    public static void Do(object o)
    {
        Console.WriteLine(o);
    }

    public override object Value => "Hello";
}

MichalStrehovsky avatar Mar 17 '21 11:03 MichalStrehovsky

Hey, I'd love to work with the issue. If you don't mind I will take it.

SkiFoD avatar Mar 17 '21 13:03 SkiFoD

It's yours! Thank you!

MichalStrehovsky avatar Mar 17 '21 13:03 MichalStrehovsky

@MichalStrehovsky @SkiFoD seems it is fixed with https://github.com/dotnet/runtime/pull/49767? Could we close it?

buyaa-n avatar Mar 22 '21 19:03 buyaa-n

Yup, closing.

MichalStrehovsky avatar Mar 22 '21 20:03 MichalStrehovsky

Hello @MichalStrehovsky

I know this is an closed issue but is related to what I am trying to fix for Mono via https://github.com/dotnet/runtime/pull/75612 . After looking into code in coreCLR, I am trying to figure out what is the defined behaviour in case when there are multipleCustomConstant (or derived) attributes attached to a parameter.

Based on this comment: https://github.com/dotnet/runtime/blob/79ae74f5ca5c8a6fe3a48935e85bd7374959c570/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs#L311-#L317 and code that follows, it seems coreCLR would handle default value resolution differently when there are more than one CustomConstant attribute attached (depending on their type: DateTime vs decimal).

Do you have any insight on what would be the desired behaviour?

PS I am also planning to improve test coverage for such cases.

ivanpovazan avatar Sep 15 '22 22:09 ivanpovazan

Do you have any insight on what would be the desired behaviour?

The defined behavior is basically how CoreCLR implemented it because it has been like that for 20 years.

It also looks like there's a difference in behavior for DefaultValue and RawDefaultValue (one seems to pick the first CustomConstant, the other will pick the last one). Fun. Thanks for looking into it!

MichalStrehovsky avatar Sep 15 '22 22:09 MichalStrehovsky