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

Commit

Permalink
add-in path
Browse files Browse the repository at this point in the history
  • Loading branch information
christiannagel committed Oct 29, 2016
1 parent 2eb5732 commit 18efe9e
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions ReflectionAndDynamic/DynamicSamples/ClientApp/Program.cs
Expand Up @@ -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}");

Expand All @@ -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 });
Expand All @@ -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);
}
Expand Down

0 comments on commit 18efe9e

Please sign in to comment.