29
29
#if NET462
30
30
using System . ComponentModel ;
31
31
#endif
32
+ #if NET5_0_OR_GREATER
33
+ using System . Diagnostics . CodeAnalysis ;
34
+ #endif
32
35
33
36
34
37
namespace Confluent . Kafka . Impl
@@ -171,7 +174,11 @@ public static string LastError
171
174
}
172
175
}
173
176
174
- static bool SetDelegates ( Type nativeMethodsClass )
177
+ static bool SetDelegates (
178
+ #if NET5_0_OR_GREATER
179
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
180
+ #endif
181
+ Type nativeMethodsClass )
175
182
{
176
183
var methods = nativeMethodsClass . GetRuntimeMethods ( ) . ToArray ( ) ;
177
184
@@ -662,14 +669,45 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
662
669
663
670
#endif
664
671
665
- private static bool TrySetDelegates ( List < Type > nativeMethodCandidateTypes )
672
+ private static bool TrySetDelegates (
673
+ #if NET5_0_OR_GREATER
674
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
675
+ #endif
676
+ Type nativeMethodCandidateType )
666
677
{
667
- foreach ( var t in nativeMethodCandidateTypes )
678
+ if ( SetDelegates ( nativeMethodCandidateType ) )
668
679
{
669
- if ( SetDelegates ( t ) )
670
- {
671
- return true ;
672
- }
680
+ return true ;
681
+ }
682
+
683
+ throw new DllNotFoundException ( "Failed to load the librdkafka native library." ) ;
684
+ }
685
+
686
+ private static bool TrySetDelegates (
687
+ #if NET5_0_OR_GREATER
688
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
689
+ #endif
690
+ Type nativeMethodCandidateType1 ,
691
+ #if NET5_0_OR_GREATER
692
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
693
+ #endif
694
+ Type nativeMethodCandidateType2 ,
695
+ #if NET5_0_OR_GREATER
696
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
697
+ #endif
698
+ Type nativeMethodCandidateType3 )
699
+ {
700
+ if ( SetDelegates ( nativeMethodCandidateType1 ) )
701
+ {
702
+ return true ;
703
+ }
704
+ if ( SetDelegates ( nativeMethodCandidateType2 ) )
705
+ {
706
+ return true ;
707
+ }
708
+ if ( SetDelegates ( nativeMethodCandidateType3 ) )
709
+ {
710
+ return true ;
673
711
}
674
712
675
713
throw new DllNotFoundException ( "Failed to load the librdkafka native library." ) ;
@@ -687,7 +725,7 @@ private static void LoadNetStandardDelegates(string userSpecifiedPath)
687
725
}
688
726
}
689
727
690
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
728
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
691
729
}
692
730
693
731
private static void LoadOSXDelegates ( string userSpecifiedPath )
@@ -700,7 +738,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
700
738
}
701
739
}
702
740
703
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
741
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
704
742
}
705
743
706
744
private static void LoadLinuxDelegates ( string userSpecifiedPath )
@@ -712,7 +750,7 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
712
750
throw new InvalidOperationException ( $ "Failed to load librdkafka at location '{ userSpecifiedPath } '. dlerror: '{ PosixNative . LastError } '.") ;
713
751
}
714
752
715
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
753
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
716
754
}
717
755
else
718
756
{
@@ -721,16 +759,15 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
721
759
var osName = PlatformApis . GetOSName ( ) ;
722
760
if ( osName . Equals ( "alpine" , StringComparison . OrdinalIgnoreCase ) )
723
761
{
724
- delegates . Add ( typeof ( NativeMethods . NativeMethods_Alpine ) ) ;
762
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods_Alpine ) ) ;
725
763
}
726
764
else
727
765
{
728
- delegates . Add ( typeof ( NativeMethods . NativeMethods_Centos7 ) ) ;
729
- delegates . Add ( typeof ( NativeMethods . NativeMethods ) ) ;
730
- delegates . Add ( typeof ( NativeMethods . NativeMethods_Centos6 ) ) ;
766
+ TrySetDelegates (
767
+ typeof ( NativeMethods . NativeMethods_Centos7 ) ,
768
+ typeof ( NativeMethods . NativeMethods ) ,
769
+ typeof ( NativeMethods . NativeMethods_Centos6 ) ) ;
731
770
}
732
-
733
- TrySetDelegates ( delegates ) ;
734
771
}
735
772
}
736
773
0 commit comments