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 }}
Repository navigation
TypeMap API: Support duplicate TypeMapAttributes where trimTarget differs #120160
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.
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]")]sealedclassHandler_Aliases{}[AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=false)]sealedclassJavaInteropAliasesAttribute(paramsstring[]aliasKeys):Attribute{publicstring[]AliasKeys{get;}=aliasKeys;}IEnumerable<Type>GetMappedTypes(stringkey){if(!_externalTypeMap.TryGetValue(key,outType?aliasedType)){yieldbreak;}varaliasesAttr=type.GetCustomAttribute<JavaInteropAliasesAttribute>();if(aliasesAttrisnull){yieldreturntype;yieldbreak;}foreach(varaliasKeyinaliasesAttr.AliasKeys){if(_externalTypeMap.TryGetValue(aliasKey,outType?aliasedType)){yieldreturnaliasedType;}}}
Activity
dotnet-policy-service commentedon Sep 26, 2025
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
AaronRobinsonMSFT commentedon Sep 26, 2025
/cc @simonrozsival @vitek-karas @jkoritzinsky @agocke
simonrozsival commentedon Feb 3, 2026
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):
I suggest closing this issue.
AaronRobinsonMSFT commentedon Feb 4, 2026
I think this is a fine suggestion. I will move it to future though since Android doesn't need it.
dotnet-policy-service commentedon Apr 13, 2026
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.