Динамическая загрузка dll - C#
Формулировка задачи:
Есть dll с кодом
Command соответственно реализован вот так
Необходимо реализовать динамическую загрузку классов Cos и Sin в List<Command>.
Я не нашел, как сделать это для вложенного класса.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDll
{
static class Math
{
public class Cos : Command
{
public new int agr = 1;
public new string name = "cos";
public Cos()
{
base.name = name;
}
public override string Execution(string command)
{
string result = "";
double res = 0;
try
{
res = double.Parse(command.Split(' ')[2]);
}
catch { result = "Недопустимое значение"; return result; }
try
{
if (command.Split(' ')[3] == "d") res = (res *(System.Math.PI)/ (double)180);
}
catch { }
res = System.Math.Cos(res);
return System.Math.Round(res, 3).ToString();
}
public override string Permission()
{
return "Math.Cos";
}
}
public class Sin : Command
{
public new int agr = 1;
public new string name = "sin";
public Sin()
{
base.name = name;
}
public override string Execution(string command)
{
string result = "";
double res = 0;
try
{
res = double.Parse(command.Split(' ')[2]);
}
catch { result = "Недопустимое значение"; return result; }
try
{
if (command.Split(' ')[3] == "d") res = (res * (System.Math.PI) / (double)180);
}
catch { }
res = System.Math.Sin(res);
return System.Math.Round(res, 3).ToString();
}
public override string Permission()
{
return "Math.Sin";
}
}
}
} public abstract class Command
{
public string name;
public int agr;
public abstract string Permission();
public abstract string Execution(string command);
}Решение задачи: «Динамическая загрузка dll»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApplication173
{
class Program
{
static void Main(string[] args)
{
var list = new List<Command>();
var ass = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
foreach (var t in ass.GetExportedTypes())
if (t.IsSubclassOf(typeof(Command)))
list.Add((Command)Activator.CreateInstance(t));
}
}
public abstract class Command
{
public string name;
public int agr;
public abstract string Permission();
public abstract string Execution(string command);
}
}