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
@@ -173,7 +176,11 @@ public static string LastError
173
176
}
174
177
}
175
178
176
- static bool SetDelegates ( Type nativeMethodsClass )
179
+ static bool SetDelegates (
180
+ #if NET5_0_OR_GREATER
181
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
182
+ #endif
183
+ Type nativeMethodsClass )
177
184
{
178
185
var methods = nativeMethodsClass . GetRuntimeMethods ( ) . ToArray ( ) ;
179
186
@@ -673,14 +680,37 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
673
680
674
681
#endif
675
682
676
- private static bool TrySetDelegates ( List < Type > nativeMethodCandidateTypes )
683
+ private static bool TrySetDelegates (
684
+ #if NET5_0_OR_GREATER
685
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
686
+ #endif
687
+ Type nativeMethodCandidateType )
677
688
{
678
- foreach ( var t in nativeMethodCandidateTypes )
689
+ if ( SetDelegates ( nativeMethodCandidateType ) )
679
690
{
680
- if ( SetDelegates ( t ) )
681
- {
682
- return true ;
683
- }
691
+ return true ;
692
+ }
693
+
694
+ throw new DllNotFoundException ( "Failed to load the librdkafka native library." ) ;
695
+ }
696
+
697
+ private static bool TrySetDelegates (
698
+ #if NET5_0_OR_GREATER
699
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
700
+ #endif
701
+ Type nativeMethodCandidateType1 ,
702
+ #if NET5_0_OR_GREATER
703
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods ) ]
704
+ #endif
705
+ Type nativeMethodCandidateType2 )
706
+ {
707
+ if ( SetDelegates ( nativeMethodCandidateType1 ) )
708
+ {
709
+ return true ;
710
+ }
711
+ if ( SetDelegates ( nativeMethodCandidateType2 ) )
712
+ {
713
+ return true ;
684
714
}
685
715
686
716
throw new DllNotFoundException ( "Failed to load the librdkafka native library." ) ;
@@ -698,7 +728,7 @@ private static void LoadNetStandardDelegates(string userSpecifiedPath)
698
728
}
699
729
}
700
730
701
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
731
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
702
732
}
703
733
704
734
private static void LoadOSXDelegates ( string userSpecifiedPath )
@@ -711,7 +741,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
711
741
}
712
742
}
713
743
714
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
744
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
715
745
}
716
746
717
747
private static void LoadLinuxDelegates ( string userSpecifiedPath )
@@ -723,7 +753,7 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
723
753
throw new InvalidOperationException ( $ "Failed to load librdkafka at location '{ userSpecifiedPath } '. dlerror: '{ PosixNative . LastError } '.") ;
724
754
}
725
755
726
- TrySetDelegates ( new List < Type > { typeof ( NativeMethods . NativeMethods ) } ) ;
756
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods ) ) ;
727
757
}
728
758
else
729
759
{
@@ -732,17 +762,16 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
732
762
var osName = PlatformApis . GetOSName ( ) ;
733
763
if ( osName . Equals ( "alpine" , StringComparison . OrdinalIgnoreCase ) )
734
764
{
735
- delegates . Add ( typeof ( NativeMethods . NativeMethods_Alpine ) ) ;
765
+ TrySetDelegates ( typeof ( NativeMethods . NativeMethods_Alpine ) ) ;
736
766
}
737
767
else
738
768
{
739
769
// Try to load first the shared library with GSSAPI linkage
740
770
// and then the one without.
741
- delegates . Add ( typeof ( NativeMethods . NativeMethods ) ) ;
742
- delegates . Add ( typeof ( NativeMethods . NativeMethods_Centos8 ) ) ;
771
+ TrySetDelegates (
772
+ typeof ( NativeMethods . NativeMethods ) ,
773
+ typeof ( NativeMethods . NativeMethods_Centos8 ) ) ;
743
774
}
744
-
745
- TrySetDelegates ( delegates ) ;
746
775
}
747
776
}
748
777
0 commit comments