Patrząc na to: http://4programmers.net/Forum/viewtopic.php?id=144168
Udało mi się zrobić test, ale nie do końca to wyszło:
Program główny:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace MainPrg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string PlugFile = "E:\\Csharp\\DLLtest\\DLLtest\\DLLtest\\bin\\Debug\\DLLtest.dll";
Assembly As;
Object Ob;
MethodInfo Mstart;
MethodInfo Mrec;
MethodInfo Msnd;
// Uruchom
private void button1_Click(object sender, EventArgs e)
{
As = Assembly.LoadFile(PlugFile);
Type[] TT = As.GetTypes();
foreach (Type Tw in TT)
{
// Kanal - nazwa interfejsu
// TestDll - nazwa klasy implementujacej interfejs "Kanal"
if (Tw.ToString().IndexOf("Kanal") > (-1))
{
// Tu sie zatrzymuje i blad: "Cannot create an instance of an interface."
Ob = Activator.CreateInstance(Tw);
Mstart = Tw.GetMethod("Wystartuj");
Mrec = Tw.GetMethod("Pobierz");
Msnd = Tw.GetMethod("Wyswietl");
}
}
Mstart.Invoke(Ob, null);
}
// Wyslij
private void button2_Click(object sender, EventArgs e)
{
Object[] Params = new Object[1];
Params[0] = textBox1.Text;
Msnd.Invoke(Ob, Params);
}
// Pobierz
private void button3_Click(object sender, EventArgs e)
{
label1.Text = (string)(Mrec.Invoke(Ob, null));
}
}
}
Wtyczka:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DLLtest
{
public interface Kanal
{
void Wystartuj();
void Wyswietl(string S);
string Pobierz();
}
public class TestDll : Kanal
{
Form1 Frm;
public void Wyswietl(string S)
{
Frm.label1.Text = S;
}
public string Pobierz()
{
return Frm.textBox1.Text;
}
public void Wystartuj()
{
Frm = new Form1();
Frm.Visible = true;
}
}
}
Jak w programie głównym w miejscu "if (Tb.ToString().IndexOf("Kanal") > (-1))" wpisze nazwę klasy, to wszystko działa prawidłowo, a jak wpiszę w tym miejscu nazwe interfejsu, to pokazuje się ten bład w miejscu wskazanym w kodzie.
W takim razie, jak wykorzystać interfejs?