Skip to content

TypeMap API: Support duplicate TypeMapAttributes where trimTarget differs #120160

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.

Activity

  1. added
    untriaged
    on Sep 26, 2025
  2. added and removed
    untriaged
    on Sep 26, 2025
  3. added this to the 11.0.0 milestone on Sep 26, 2025
  4. dotnet-policy-service commented on Sep 26, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  5. AaronRobinsonMSFT commented on Sep 26, 2025

    @AaronRobinsonMSFT
    Member
  6. simonrozsival commented on Feb 3, 2026

    @simonrozsival
    Member

    While it would be nice to have a built-in way of handling duplicates, there is a workaround we can use to still make the typemap fully trimmable (see dotnet/android#10757 for more details):

    // Base name → alias holder
    [assembly: TypeMap<Java.Lang.Object>("com/example/Handler", typeof(Handler_Aliases), typeof(Handler_Aliases))]
    
    // Indexed names → proxies
    [assembly: TypeMap<Java.Lang.Object>("com/example/Handler[0]", typeof(HandlerA_Proxy), typeof(HandlerA))]
    [assembly: TypeMap<Java.Lang.Object>("com/example/Handler[1]", typeof(HandlerB_Proxy), typeof(HandlerB))]
    
    // Associations for trimmer (preserves alias holder when target is used)
    [assembly: TypeMapAssociation<AliasesUniverse>(typeof(HandlerA), typeof(Handler_Aliases))]
    [assembly: TypeMapAssociation<AliasesUniverse>(typeof(HandlerB), typeof(Handler_Aliases))]
    
    [JavaInteropAliases("com/example/Handler[0]", "com/example/Handler[1]")]
    sealed class Handler_Aliases { }
    
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    sealed class JavaInteropAliasesAttribute(params string[] aliasKeys) : Attribute
    {
    	public string[] AliasKeys { get; } = aliasKeys;
    }
    
    IEnumerable<Type> GetMappedTypes (string key)
    {
    	if (!_externalTypeMap.TryGetValue(key, out Type? aliasedType)) {
    		yield break;
    	}
    
    	var aliasesAttr = type.GetCustomAttribute<JavaInteropAliasesAttribute>();
    	if (aliasesAttr is null) {
    		yield return type;
    		yield break;
    	}
    
    	foreach (var aliasKey in aliasesAttr.AliasKeys) {
    		if (_externalTypeMap.TryGetValue(aliasKey, out Type? aliasedType)) {
    			yield return aliasedType;
    		}
    	}
    }

    I suggest closing this issue.

  7. AaronRobinsonMSFT commented on Feb 4, 2026

    @AaronRobinsonMSFT
    Member

    I suggest closing this issue.

    I think this is a fine suggestion. I will move it to future though since Android doesn't need it.

  8. dotnet-policy-service commented on Apr 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions