33using System ;
44using System . Collections . Generic ;
55using System . Diagnostics ;
6- using System . Runtime . CompilerServices ;
76using System . Reflection ;
87using System . Reflection . Emit ;
8+ using System . Runtime . CompilerServices ;
99using System . Runtime . InteropServices ;
1010using System . Threading ;
1111using TestLibrary ;
@@ -35,6 +35,20 @@ public static class UnmanagedCallersOnlyDll
3535 public static extern int PInvokeMarkedWithUnmanagedCallersOnly ( int n ) ;
3636 }
3737
38+ private const string InvalidCSharpAssemblyName = "InvalidCSharp" ;
39+
40+ public static Type GetCallbacksType ( )
41+ {
42+ var asm = Assembly . Load ( InvalidCSharpAssemblyName ) ;
43+ return asm . GetType ( "InvalidCSharp.Callbacks" ) ;
44+ }
45+
46+ public static Type GetGenericClassOfIntType ( )
47+ {
48+ var asm = Assembly . Load ( InvalidCSharpAssemblyName ) ;
49+ return asm . GetType ( "InvalidCSharp.GenericClass`1" ) . MakeGenericType ( typeof ( int ) ) ;
50+ }
51+
3852 private delegate int IntNativeMethodInvoker ( ) ;
3953 private delegate void NativeMethodInvoker ( ) ;
4054
@@ -338,12 +352,6 @@ void CallAsDelegate()
338352 }
339353 }
340354
341- [ UnmanagedCallersOnly ]
342- public int CallbackNonStatic ( int val )
343- {
344- Assert . Fail ( $ "Instance functions with attribute { nameof ( UnmanagedCallersOnlyAttribute ) } are invalid") ;
345- return - 1 ;
346- }
347355
348356 public static void NegativeTest_NonStaticMethod ( )
349357 {
@@ -354,7 +362,7 @@ void TestUnmanagedCallersOnlyNonStatic()
354362 {
355363 .locals init ([0] native int ptr)
356364 nop
357- ldftn int CallbackNonStatic(int)
365+ ldftn int GetCallbacksType(). CallbackNonStatic(int)
358366 stloc.0
359367
360368 ldloc.0
@@ -371,7 +379,7 @@ ldftn int CallbackNonStatic(int)
371379 il . Emit ( OpCodes . Nop ) ;
372380
373381 // Get native function pointer of the callback
374- il . Emit ( OpCodes . Ldftn , typeof ( Program ) . GetMethod ( nameof ( CallbackNonStatic ) ) ) ;
382+ il . Emit ( OpCodes . Ldftn , GetCallbacksType ( ) . GetMethod ( " CallbackNonStatic" ) ) ;
375383 il . Emit ( OpCodes . Stloc_0 ) ;
376384 il . Emit ( OpCodes . Ldloc_0 ) ;
377385
@@ -436,13 +444,6 @@ ldftn int CallbackMethodNonBlittable(bool)
436444 Assert . Throws < InvalidProgramException > ( ( ) => { testNativeMethod ( ) ; } ) ;
437445 }
438446
439- [ UnmanagedCallersOnly ]
440- public static int CallbackMethodGeneric < T > ( T arg )
441- {
442- Assert . Fail ( $ "Functions with attribute { nameof ( UnmanagedCallersOnlyAttribute ) } cannot have generic arguments") ;
443- return - 1 ;
444- }
445-
446447 public static void NegativeTest_NonInstantiatedGenericArguments ( )
447448 {
448449 Console . WriteLine ( $ "Running { nameof ( NegativeTest_NonInstantiatedGenericArguments ) } ...") ;
@@ -452,7 +453,7 @@ void TestUnmanagedCallersOnlyNonInstGenericArguments()
452453 {
453454 .locals init ([0] native int ptr)
454455 IL_0000: nop
455- IL_0001: ldftn void CallbackMethodGeneric(T)
456+ IL_0001: ldftn void InvalidCSharp.Callbacks. CallbackMethodGeneric(T)
456457 IL_0007: stloc.0
457458 IL_0008: ret
458459 }
@@ -463,7 +464,7 @@ .locals init ([0] native int ptr)
463464 il . Emit ( OpCodes . Nop ) ;
464465
465466 // Get native function pointer of the callback
466- il . Emit ( OpCodes . Ldftn , typeof ( Program ) . GetMethod ( nameof ( CallbackMethodGeneric ) ) ) ;
467+ il . Emit ( OpCodes . Ldftn , GetCallbacksType ( ) . GetMethod ( " CallbackMethodGeneric" ) ) ;
467468 il . Emit ( OpCodes . Stloc_0 ) ;
468469
469470 il . Emit ( OpCodes . Ret ) ;
@@ -482,7 +483,7 @@ void TestUnmanagedCallersOnlyInstGenericArguments()
482483 {
483484 .locals init ([0] native int ptr)
484485 nop
485- ldftn void CallbackMethodGeneric(int)
486+ ldftn void InvalidCSharp.Callbacks. CallbackMethodGeneric(int)
486487 stloc.0
487488
488489 ldloc.0
@@ -499,7 +500,7 @@ ldftn void CallbackMethodGeneric(int)
499500 il . Emit ( OpCodes . Nop ) ;
500501
501502 // Get native function pointer of the instantiated generic callback
502- il . Emit ( OpCodes . Ldftn , typeof ( Program ) . GetMethod ( nameof ( CallbackMethodGeneric ) ) . MakeGenericMethod ( new [ ] { typeof ( int ) } ) ) ;
503+ il . Emit ( OpCodes . Ldftn , GetCallbacksType ( ) . GetMethod ( " CallbackMethodGeneric" ) . MakeGenericMethod ( new [ ] { typeof ( int ) } ) ) ;
503504 il . Emit ( OpCodes . Stloc_0 ) ;
504505 il . Emit ( OpCodes . Ldloc_0 ) ;
505506
@@ -515,15 +516,6 @@ ldftn void CallbackMethodGeneric(int)
515516 Assert . Throws < InvalidProgramException > ( ( ) => { testNativeMethod ( ) ; } ) ;
516517 }
517518
518- public class GenericClass < T >
519- {
520- [ UnmanagedCallersOnly ]
521- public static void CallbackMethod ( int n )
522- {
523- Assert . Fail ( $ "Functions with attribute { nameof ( UnmanagedCallersOnlyAttribute ) } within a generic type are invalid") ;
524- }
525- }
526-
527519 public static void NegativeTest_FromInstantiatedGenericClass ( )
528520 {
529521 Console . WriteLine ( $ "Running { nameof ( NegativeTest_FromInstantiatedGenericClass ) } ...") ;
@@ -533,7 +525,7 @@ void TestUnmanagedCallersOnlyInstGenericType()
533525 {
534526 .locals init ([0] native int ptr)
535527 nop
536- ldftn int GenericClass<int>::CallbackMethod(int)
528+ ldftn int InvalidCSharp. GenericClass<int>::CallbackMethod(int)
537529 stloc.0
538530
539531 ldloc.0
@@ -550,7 +542,7 @@ .locals init ([0] native int ptr)
550542 il . Emit ( OpCodes . Nop ) ;
551543
552544 // Get native function pointer of the callback from the instantiated generic class.
553- il . Emit ( OpCodes . Ldftn , typeof ( GenericClass < int > ) . GetMethod ( nameof ( GenericClass < int > . CallbackMethod ) ) ) ;
545+ il . Emit ( OpCodes . Ldftn , GetGenericClassOfIntType ( ) . GetMethod ( " CallbackMethod" ) ) ;
554546 il . Emit ( OpCodes . Stloc_0 ) ;
555547 il . Emit ( OpCodes . Ldloc_0 ) ;
556548
0 commit comments