Skip to content

TypeMap API: Support duplicate TypeMapAttributes where trimTarget differs #120160

@jtschuster

Description

@jtschuster

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.

[assembly: TypeMap<UsedTypeMap>("DuplicateString", typeof(SameTargetType), typeof(DifferentTrimTarget1))]
[assembly: TypeMap<UsedTypeMap>("DuplicateString", typeof(SameTargetType), typeof(DifferentTrimTarget2))]

public class Program
{
    public static void Main()
    {
        var mappedTypes = System.Runtime.InteropServices.TypeMapping.GetOrCreateExternalTypeMapping<UsedTypeMap>()["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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions