1
1
namespace NativeCode . Mobile . AppCompat . Renderers . Extensions
2
2
{
3
3
using System ;
4
- using System . Linq ;
4
+ using System . Reflection ;
5
+
6
+ using NativeCode . Mobile . AppCompat . Renderers . Helpers ;
5
7
6
8
using Xamarin . Forms ;
7
9
@@ -10,10 +12,21 @@ namespace NativeCode.Mobile.AppCompat.Renderers.Extensions
10
12
/// </summary>
11
13
public static class ElementExtensions
12
14
{
13
- private const string ButtonController = "IButtonController" ;
15
+ private const string ButtonControllerType = "Xamarin.Forms. IButtonController, Xamarin.Forms.Core " ;
14
16
15
17
private const string ButtonControllerSendClicked = "SendClicked" ;
16
18
19
+ private static readonly MethodInfo MethodSendClicked ;
20
+
21
+ /// <summary>
22
+ /// Initializes static members of the <see cref="ElementExtensions"/> class.
23
+ /// </summary>
24
+ static ElementExtensions ( )
25
+ {
26
+ var type = Type . GetType ( ButtonControllerType , true ) ;
27
+ MethodSendClicked = type . GetMethod ( ButtonControllerSendClicked ) ;
28
+ }
29
+
17
30
/// <summary>
18
31
/// Tries to cast the element to a button controller and invoke the SendClicked method.
19
32
/// </summary>
@@ -22,27 +35,7 @@ public static class ElementExtensions
22
35
/// <remarks>The IButtonController is an internal interface, so we have to use reflection.</remarks>
23
36
public static void InvokeSendClicked ( this Element element )
24
37
{
25
- var type = GetImplementedInterface ( element , ButtonController ) ;
26
- var method = type . GetMethod ( ButtonControllerSendClicked ) ;
27
-
28
- if ( method == null )
29
- {
30
- throw new MissingMethodException ( type . Name , ButtonControllerSendClicked ) ;
31
- }
32
-
33
- method . Invoke ( element , new object [ 0 ] ) ;
34
- }
35
-
36
- private static Type GetImplementedInterface ( object instance , string name )
37
- {
38
- var type = instance . GetType ( ) . GetInterfaces ( ) . Single ( x => x . Name == ButtonController ) ;
39
-
40
- if ( type == null )
41
- {
42
- throw new InvalidCastException ( "Type does not implement interface " + name + "." ) ;
43
- }
44
-
45
- return type ;
38
+ MethodSendClicked . Invoke ( element , ReflectionHelper . EmptyParameters ) ;
46
39
}
47
40
}
48
41
}
0 commit comments