From 18efe9ea91d878a540cbc0002ce3ad819934b5b2 Mon Sep 17 00:00:00 2001 From: Christian Nagel Date: Sat, 29 Oct 2016 10:20:40 +0200 Subject: [PATCH] add-in path --- .../DynamicSamples/ClientApp/Program.cs | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs b/ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs index 3e93bf52..0c1c6b88 100644 --- a/ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs +++ b/ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs @@ -12,21 +12,33 @@ namespace ClientApp { class Program { - private const string CalculatorLibPath = @"c:\AddIns\CalculatorLib.dll"; private const string CalculatorLibName = "CalculatorLib"; private const string CalculatorTypeName = "CalculatorLib.Calculator"; - static void Main() + static void Main(string[] args) { - ReflectionOld(); - ReflectionNew(); + if (args.Length != 1) + { + ShowUsage(); + return; + } + ReflectionOld(args[0]); + ReflectionNew(args[0]); + } + + private static void ShowUsage() + { + WriteLine($"Usage: {nameof(ClientApp)} path"); + WriteLine(); + WriteLine("Copy CalculatorLib.dll to an addin directory"); + WriteLine("and pass the absolute path of this directory when starting the application to load the library"); } - private static void ReflectionNew() + private static void ReflectionNew(string addinPath) { double x = 3; double y = 4; - dynamic calc = GetCalculator(); + dynamic calc = GetCalculator(addinPath); double result = calc.Add(x, y); WriteLine($"the result of {x} and {y} is {result}"); @@ -40,11 +52,11 @@ private static void ReflectionNew() } } - private static void ReflectionOld() + private static void ReflectionOld(string addinPath) { double x = 3; double y = 4; - object calc = GetCalculator(); + object calc = GetCalculator(addinPath); // previous to using the NuGet package System.Reflection.TypeExtensions // object result = calc.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, calc, new object[] { x, y }); @@ -53,17 +65,17 @@ private static void ReflectionOld() } #if NET461 - private static object GetCalculator() + private static object GetCalculator(string addinPath) { - Assembly assembly = Assembly.LoadFile(CalculatorLibPath); + Assembly assembly = Assembly.LoadFile(addinPath); return assembly.CreateInstance(CalculatorTypeName); } #endif #if DOTNETCORE - private static object GetCalculator() + private static object GetCalculator(string addinPath) { - Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(CalculatorLibPath); + Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(addinPath); Type type = assembly.GetType(CalculatorTypeName); return Activator.CreateInstance(type); }