사용자가 콤보 상자에서 유형을 선택하는 Windows 응용 프로그램을 만들고 있습니다. 선택을 기반으로, 리플렉션을 사용하여 해당 유형의 인스턴스를 만들고 해당 메소드 중 하나를 호출하려고합니다. 작성하려는 유형은 같은 Windows 응용 프로그램에서 sperate 클래스로 정의됩니다. 하지만 제목에서 언급 한 오류가 나타납니다. 내 코드를 heres.xxx를 어셈블리 xxx에서로드 할 수 없습니다.
Form1 코드 :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cbLogs.SelectedIndex = 0;
}
private void btnProcess_Click(object sender, EventArgs e)
{
lblMessage.Text = "";
lblResult.Text = "";
if (cbLogs.SelectedIndex <= 0)
{
lblMessage.Text = "Please select Log to be processed";
cbLogs.Focus();
return;
}
Assembly currAss = System.Reflection.Assembly.GetExecutingAssembly();
//I get above error on below line.
object obj = Activator.CreateInstance(currAss.FullName,"SustainabilityXpress ");
Type type = obj.GetType();
object result = type.InvokeMember("process",
BindingFlags.Default | BindingFlags.InvokeMethod,
null, obj, null);
lblResult.Text = result.ToString();
}
}
ILogBase 인터페이스 : ILogBase를 구현
interface ILogBase
{
string process();
}
SustainabilityXpress는 클래스 :
public class SustainabilityXpress: ILogBase
{
string LogName = "SUSTAINABILITYXPRESS";
public string process()
{
return "Sustainabilityxpress";
}
}
Humberto 님, 내 문제를 해결 한 네임 스페이스를 추가하십시오. – sunitw