You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
DynamicallyAccessedMembers with a string TypeName of a generic type doesn't appear to work #1469
Then Publish with PublishTrimmed=true and TrimMode=link, and adding the application assembly as a TrimMode=link assembly. Run the published app and you will see:
ConsoleApp1.TypeDescriptionProvider
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at ConsoleApp1.Program.GetDescriptionProvider(Object o) in F:\DotNetTest\TrimmingTest\Program.cs:line 20
at ConsoleApp1.Program.Main() in F:\DotNetTest\TrimmingTest\Program.cs:line 14
(Note my app was named TrimmingTest. If you have a different name, you will need to use a different assembly name in the strings.)
The .NET Native treeshaker has a Type.GetType parser that has been factored out of the .NET Native runtime reflection stack that we could use to parse these properly.
This looks to me like quite a serious limitation in the newly added code in 5.0. Why is there no error or warning if we cannot parse the string format? I think this is worth addressing for 5.0.
This looks to me like quite a serious limitation in the newly added code in 5.0
AFAIK this is calling the same codepaths to parse the string that the Type.GetType pattern matcher that pre-dates dataflow analysis was calling. Was that also new in 5.0?
I won't have time to work on it, but I can point whoever works on this to the factored out type name parser we have in .NET Native. The part that needs to be converted from CCI to Cecil is pretty small.
Why is there no error or warning if we cannot parse the string format
Because it could just be a type name that doesn't exist. Reflection is used for lightups and the things the code is looking for often don't exist. We tried to be helpful like this in .NET Native ("hey the p/invoke you're doing doesn't call into any known Win32 APIs that are allowed in the Windows Store and will throw at runtime", or "hey, the IL in this method is invalid and will throw at runtime"). This pretty much always found unreachable code (the former in crossplatform libraries that had Unix p/invokes under condition checks, the latter pretty much always found unreachable code generated by Unity tooling). It was a support cost because people asked questions about this, or got distracted by this when they hit some unrelated issue.
No warning: This is in sync with the rest of the data flow behavior - linker will not warn/fail on missing types/members even when specifically referenced in code via reflection. So for example:
Will not cause a warning - the reasoning is that it's not "wrong" code in any sense. If the method doesn't exist the API will return null - and this will not change after linker. So linker doesn't change the code behavior in any way here.
It might not be what the author wanted, but linker is not in the business of recognizing the intent of the developer.
Data flow in this case is basically just an extension of the above rule, where the value does not come from a literal, but comes from annotated variable.
Activity
MichalStrehovsky commentedon Sep 2, 2020
Yep, this is #1387.
MichalStrehovsky commentedon Sep 2, 2020
The .NET Native treeshaker has a Type.GetType parser that has been factored out of the .NET Native runtime reflection stack that we could use to parse these properly.
marek-safar commentedon Sep 3, 2020
This looks to me like quite a serious limitation in the newly added code in 5.0. Why is there no error or warning if we cannot parse the string format? I think this is worth addressing for 5.0.
@MichalStrehovsky could you work on that?
/cc @vitek-karas
MichalStrehovsky commentedon Sep 3, 2020
AFAIK this is calling the same codepaths to parse the string that the Type.GetType pattern matcher that pre-dates dataflow analysis was calling. Was that also new in 5.0?
I won't have time to work on it, but I can point whoever works on this to the factored out type name parser we have in .NET Native. The part that needs to be converted from CCI to Cecil is pretty small.
Because it could just be a type name that doesn't exist. Reflection is used for lightups and the things the code is looking for often don't exist. We tried to be helpful like this in .NET Native ("hey the p/invoke you're doing doesn't call into any known Win32 APIs that are allowed in the Windows Store and will throw at runtime", or "hey, the IL in this method is invalid and will throw at runtime"). This pretty much always found unreachable code (the former in crossplatform libraries that had Unix p/invokes under condition checks, the latter pretty much always found unreachable code generated by Unity tooling). It was a support cost because people asked questions about this, or got distracted by this when they hit some unrelated issue.
vitek-karas commentedon Sep 7, 2020
No warning: This is in sync with the rest of the data flow behavior - linker will not warn/fail on missing types/members even when specifically referenced in code via reflection. So for example:
Will not cause a warning - the reasoning is that it's not "wrong" code in any sense. If the method doesn't exist the API will return
null- and this will not change after linker. So linker doesn't change the code behavior in any way here.It might not be what the author wanted, but linker is not in the business of recognizing the intent of the developer.
Data flow in this case is basically just an extension of the above rule, where the value does not come from a literal, but comes from annotated variable.
marek-safar commentedon Sep 16, 2020
Closing as fixed