Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.

Commit 18efe9e

Browse files
add-in path
1 parent 2eb5732 commit 18efe9e

File tree

1 file changed

+24
-12
lines changed
  • ReflectionAndDynamic/DynamicSamples/ClientApp

1 file changed

+24
-12
lines changed

ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs

+24-12
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,33 @@ namespace ClientApp
1212
{
1313
class Program
1414
{
15-
private const string CalculatorLibPath = @"c:\AddIns\CalculatorLib.dll";
1615
private const string CalculatorLibName = "CalculatorLib";
1716
private const string CalculatorTypeName = "CalculatorLib.Calculator";
1817

19-
static void Main()
18+
static void Main(string[] args)
2019
{
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");
2335
}
2436

25-
private static void ReflectionNew()
37+
private static void ReflectionNew(string addinPath)
2638
{
2739
double x = 3;
2840
double y = 4;
29-
dynamic calc = GetCalculator();
41+
dynamic calc = GetCalculator(addinPath);
3042
double result = calc.Add(x, y);
3143
WriteLine($"the result of {x} and {y} is {result}");
3244

@@ -40,11 +52,11 @@ private static void ReflectionNew()
4052
}
4153
}
4254

43-
private static void ReflectionOld()
55+
private static void ReflectionOld(string addinPath)
4456
{
4557
double x = 3;
4658
double y = 4;
47-
object calc = GetCalculator();
59+
object calc = GetCalculator(addinPath);
4860

4961
// previous to using the NuGet package System.Reflection.TypeExtensions
5062
// object result = calc.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, calc, new object[] { x, y });
@@ -53,17 +65,17 @@ private static void ReflectionOld()
5365
}
5466

5567
#if NET461
56-
private static object GetCalculator()
68+
private static object GetCalculator(string addinPath)
5769
{
58-
Assembly assembly = Assembly.LoadFile(CalculatorLibPath);
70+
Assembly assembly = Assembly.LoadFile(addinPath);
5971
return assembly.CreateInstance(CalculatorTypeName);
6072
}
6173
#endif
6274

6375
#if DOTNETCORE
64-
private static object GetCalculator()
76+
private static object GetCalculator(string addinPath)
6577
{
66-
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(CalculatorLibPath);
78+
Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(addinPath);
6779
Type type = assembly.GetType(CalculatorTypeName);
6880
return Activator.CreateInstance(type);
6981
}

0 commit comments

Comments
 (0)