@@ -10,11 +10,11 @@ namespace Xamarin.Android.Tasks
1010{
1111 class MarshalMethodsAssemblyRewriter
1212 {
13- IDictionary < string , MarshalMethodEntry > methods ;
13+ IDictionary < string , IList < MarshalMethodEntry > > methods ;
1414 ICollection < AssemblyDefinition > uniqueAssemblies ;
1515 IDictionary < string , HashSet < string > > assemblyPaths ;
1616
17- public MarshalMethodsAssemblyRewriter ( IDictionary < string , MarshalMethodEntry > methods , ICollection < AssemblyDefinition > uniqueAssemblies , IDictionary < string , HashSet < string > > assemblyPaths )
17+ public MarshalMethodsAssemblyRewriter ( IDictionary < string , IList < MarshalMethodEntry > > methods , ICollection < AssemblyDefinition > uniqueAssemblies , IDictionary < string , HashSet < string > > assemblyPaths )
1818 {
1919 this . methods = methods ?? throw new ArgumentNullException ( nameof ( methods ) ) ;
2020 this . uniqueAssemblies = uniqueAssemblies ?? throw new ArgumentNullException ( nameof ( uniqueAssemblies ) ) ;
@@ -23,17 +23,20 @@ public MarshalMethodsAssemblyRewriter (IDictionary<string, MarshalMethodEntry> m
2323
2424 public void Rewrite ( DirectoryAssemblyResolver resolver )
2525 {
26+ MethodDefinition unmanagedCallersOnlyAttributeCtor = GetUnmanagedCallersOnlyAttributeConstructor ( resolver ) ;
2627 var unmanagedCallersOnlyAttributes = new Dictionary < AssemblyDefinition , CustomAttribute > ( ) ;
2728 foreach ( AssemblyDefinition asm in uniqueAssemblies ) {
28- unmanagedCallersOnlyAttributes . Add ( asm , GetUnmanagedCallersOnlyAttribute ( asm , resolver ) ) ;
29+ unmanagedCallersOnlyAttributes . Add ( asm , CreateImportedUnmanagedCallersOnlyAttribute ( asm , unmanagedCallersOnlyAttributeCtor ) ) ;
2930 }
3031
3132 Console . WriteLine ( "Adding the [UnmanagedCallersOnly] attribute to native callback methods and removing unneeded fields+methods" ) ;
32- foreach ( MarshalMethodEntry method in methods . Values ) {
33- Console . WriteLine ( $ "\t { method . NativeCallback . FullName } (token: 0x{ method . NativeCallback . MetadataToken . RID : x} )") ;
34- method . NativeCallback . CustomAttributes . Add ( unmanagedCallersOnlyAttributes [ method . NativeCallback . Module . Assembly ] ) ;
35- method . Connector . DeclaringType . Methods . Remove ( method . Connector ) ;
36- method . CallbackField ? . DeclaringType . Fields . Remove ( method . CallbackField ) ;
33+ foreach ( IList < MarshalMethodEntry > methodList in methods . Values ) {
34+ foreach ( MarshalMethodEntry method in methodList ) {
35+ Console . WriteLine ( $ "\t { method . NativeCallback . FullName } (token: 0x{ method . NativeCallback . MetadataToken . RID : x} )") ;
36+ method . NativeCallback . CustomAttributes . Add ( unmanagedCallersOnlyAttributes [ method . NativeCallback . Module . Assembly ] ) ;
37+ method . Connector . DeclaringType . Methods . Remove ( method . Connector ) ;
38+ method . CallbackField ? . DeclaringType . Fields . Remove ( method . CallbackField ) ;
39+ }
3740 }
3841
3942 Console . WriteLine ( ) ;
@@ -75,8 +78,10 @@ public void Rewrite (DirectoryAssemblyResolver resolver)
7578
7679 Console . WriteLine ( ) ;
7780 Console . WriteLine ( "Method tokens:" ) ;
78- foreach ( MarshalMethodEntry method in methods . Values ) {
79- Console . WriteLine ( $ "\t { method . NativeCallback . FullName } (token: 0x{ method . NativeCallback . MetadataToken . RID : x} )") ;
81+ foreach ( IList < MarshalMethodEntry > methodList in methods . Values ) {
82+ foreach ( MarshalMethodEntry method in methodList ) {
83+ Console . WriteLine ( $ "\t { method . NativeCallback . FullName } (token: 0x{ method . NativeCallback . MetadataToken . RID : x} )") ;
84+ }
8085 }
8186
8287 void MoveFile ( string source , string target )
@@ -99,7 +104,7 @@ ICollection<string> GetAssemblyPaths (AssemblyDefinition asm)
99104 return paths ;
100105 }
101106
102- CustomAttribute GetUnmanagedCallersOnlyAttribute ( AssemblyDefinition targetAssembly , DirectoryAssemblyResolver resolver )
107+ MethodDefinition GetUnmanagedCallersOnlyAttributeConstructor ( DirectoryAssemblyResolver resolver )
103108 {
104109 AssemblyDefinition asm = resolver . Resolve ( "System.Runtime.InteropServices" ) ;
105110 TypeDefinition unmanagedCallersOnlyAttribute = null ;
@@ -118,16 +123,24 @@ CustomAttribute GetUnmanagedCallersOnlyAttribute (AssemblyDefinition targetAssem
118123 }
119124 }
120125
121- MethodDefinition attrConstructor = null ;
126+ if ( unmanagedCallersOnlyAttribute == null ) {
127+ throw new InvalidOperationException ( "Unable to find the System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute type" ) ;
128+ }
129+
122130 foreach ( MethodDefinition md in unmanagedCallersOnlyAttribute . Methods ) {
123131 if ( ! md . IsConstructor ) {
124132 continue ;
125133 }
126134
127- attrConstructor = md ;
135+ return md ;
128136 }
129137
130- return new CustomAttribute ( targetAssembly . MainModule . ImportReference ( attrConstructor ) ) ;
138+ throw new InvalidOperationException ( "Unable to find the System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute type constructor" ) ;
139+ }
140+
141+ CustomAttribute CreateImportedUnmanagedCallersOnlyAttribute ( AssemblyDefinition targetAssembly , MethodDefinition unmanagedCallersOnlyAtributeCtor )
142+ {
143+ return new CustomAttribute ( targetAssembly . MainModule . ImportReference ( unmanagedCallersOnlyAtributeCtor ) ) ;
131144 }
132145 }
133146}
0 commit comments