@@ -12,21 +12,33 @@ namespace ClientApp
12
12
{
13
13
class Program
14
14
{
15
- private const string CalculatorLibPath = @"c:\AddIns\CalculatorLib.dll" ;
16
15
private const string CalculatorLibName = "CalculatorLib" ;
17
16
private const string CalculatorTypeName = "CalculatorLib.Calculator" ;
18
17
19
- static void Main ( )
18
+ static void Main ( string [ ] args )
20
19
{
21
- ReflectionOld ( ) ;
22
- ReflectionNew ( ) ;
20
+ if ( args . Length != 1 )
21
+ {
22
+ ShowUsage ( ) ;
23
+ return ;
24
+ }
25
+ ReflectionOld ( args [ 0 ] ) ;
26
+ ReflectionNew ( args [ 0 ] ) ;
27
+ }
28
+
29
+ private static void ShowUsage ( )
30
+ {
31
+ WriteLine ( $ "Usage: { nameof ( ClientApp ) } path") ;
32
+ WriteLine ( ) ;
33
+ WriteLine ( "Copy CalculatorLib.dll to an addin directory" ) ;
34
+ WriteLine ( "and pass the absolute path of this directory when starting the application to load the library" ) ;
23
35
}
24
36
25
- private static void ReflectionNew ( )
37
+ private static void ReflectionNew ( string addinPath )
26
38
{
27
39
double x = 3 ;
28
40
double y = 4 ;
29
- dynamic calc = GetCalculator ( ) ;
41
+ dynamic calc = GetCalculator ( addinPath ) ;
30
42
double result = calc . Add ( x , y ) ;
31
43
WriteLine ( $ "the result of { x } and { y } is { result } ") ;
32
44
@@ -40,11 +52,11 @@ private static void ReflectionNew()
40
52
}
41
53
}
42
54
43
- private static void ReflectionOld ( )
55
+ private static void ReflectionOld ( string addinPath )
44
56
{
45
57
double x = 3 ;
46
58
double y = 4 ;
47
- object calc = GetCalculator ( ) ;
59
+ object calc = GetCalculator ( addinPath ) ;
48
60
49
61
// previous to using the NuGet package System.Reflection.TypeExtensions
50
62
// object result = calc.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, calc, new object[] { x, y });
@@ -53,17 +65,17 @@ private static void ReflectionOld()
53
65
}
54
66
55
67
#if NET461
56
- private static object GetCalculator ( )
68
+ private static object GetCalculator ( string addinPath )
57
69
{
58
- Assembly assembly = Assembly . LoadFile ( CalculatorLibPath ) ;
70
+ Assembly assembly = Assembly . LoadFile ( addinPath ) ;
59
71
return assembly . CreateInstance ( CalculatorTypeName ) ;
60
72
}
61
73
#endif
62
74
63
75
#if DOTNETCORE
64
- private static object GetCalculator ( )
76
+ private static object GetCalculator ( string addinPath )
65
77
{
66
- Assembly assembly = AssemblyLoadContext . Default . LoadFromAssemblyPath ( CalculatorLibPath ) ;
78
+ Assembly assembly = AssemblyLoadContext . Default . LoadFromAssemblyPath ( addinPath ) ;
67
79
Type type = assembly . GetType ( CalculatorTypeName ) ;
68
80
return Activator . CreateInstance ( type ) ;
69
81
}
0 commit comments