dotnet / dotnet/runtime

TypeMap API: Support duplicate TypeMapAttributes where trimTarget differs

Open
#120,160 5 comments 1 reaction 0 assignees View on GitHub
area-Interop-coreclr
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

Android requires a one-to-many mapping from a Java class (string in TypeMapAttribute) to .NET types. A workaround is possible to make this mapping via an intermediate type with attributes pointing to the mapped types. However, to make the types trimmable, the type map needs to support having duplicate TypeMapAttribute's with identical first two parameters (java class string and .NET target type) but with different trimTarget types.

```C#
[assembly: TypeMap("DuplicateString", typeof(SameTargetType), typeof(DifferentTrimTarget1))]
[assembly: TypeMap("DuplicateString", typeof(SameTargetType), typeof(DifferentTrimTarget2))]

public class Program
{
public static void Main()
{
var mappedTypes = System.Runtime.InteropServices.TypeMapping.GetOrCreateExternalTypeMapping()["DuplicateString"].GetCustomAttributes(typeof(MappingAttribute));
}
}

class DifferentTrimTarget1
{
}

class DifferentTrimTarget2
{
}

[Mapping(typeof(DifferentTrimTarget1))]
[Mapping(typeof(DifferentTrimTarget2))]
class SameTargetType
{
}

class UsedTypeMap { }
```

This can be worked around in the meantime by omitting the trim target and rooting the target type always.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.