Sergio0694/PolySharp

Help: attempting to get access to DynamicallyAccessedMembers but not working

charlesroddie opened this issue · 4 comments

I'm trying to annotate https://github.com/peteroupc/CBOR .
I gather the way to do this is to use PolySharp to get access to DynamicallyAccessedMembers , and install this and set langversion to 11.0. But I did this and it doesn't work. Am I doing this wrong?

Attempt is in: https://github.com/charlesroddie/CBOR/pull/1/files
Build error:

CBOR\PeterO\Cbor\PropertyMap.cs(548,6,548,32): error CS0246: The type or namespace name 'DynamicallyAccessedMembersAttribute' could not be found (are you missing a using directive or an assembly reference?)
CBOR\PeterO\Cbor\PropertyMap.cs(548,6,548,32): error CS0246: The type or namespace name 'DynamicallyAccessedMembers' could not be found (are you missing a using directive or an assembly reference?)

Those polyfills are not included by default, you need to define the PolySharpIncludeRuntimeSupportedAttributes MSBuild property (in your .csproj or in some .props file or something). Note that while the attributes will be present, they will not actually work, and they're only meant to be used to support multi-targeting scenarios (so the target with modern .NET would use the real attributes, which work, and the older target would still let you have the same code to keep things nice and clean) 🙂

Thanks. That property did give the attributes, and yes they don't seem to work. That doesn't seem compatible with what I have read elsewhere.

The official "solution" to not having trimming attributes in netstandard is dotnet/runtime#36656 (comment)

dotnet/runtime#36656 (comment)
we've decided not to make these attributes available outside of net5.0. If a library needs to apply them in a down-level assembly, they can compile internal versions of these attributes into the library.

And the advice on how to do this is to use polysharp:

dotnet/runtime#56612 (comment)
https://github.com/Sergio0694/PolySharp has the trimming attributes (and a lot more).

So at least the most official dotnet advice is to use your library and that it will work without multi-targeting, and will allow building netstandard libraries that, when used in NativeAOT projects, will give more accurate trimming warnings.

I'm not entirely sure NativeAOT would recognize them, but sure, you can try. Just define this compile constant:

MULTI_TARGETING_SUPPORT_ATTRIBUTES

This will cause those attributes to be preserved when building the project as well 🙂

Thanks. This does work.