我正在製作一個Windows應用程序,用戶從組合框中選擇一種類型。基於選擇,使用反射我想創建相應類型的實例並調用其方法之一。 我想創建的類型也被定義在與sperate類相同的Windows應用程序中。但是我收到了Title中提到的錯誤。繼承人我的代碼。無法從程序集xxx中加載類型xxx
Form 1代碼:
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接口:
interface ILogBase
{
string process();
}
SustainabilityXpress中類,它實現ILogBase:
public class SustainabilityXpress: ILogBase
{
string LogName = "SUSTAINABILITYXPRESS";
public string process()
{
return "Sustainabilityxpress";
}
}
thanx Humberto,添加命名空間解決了我的問題。 – sunitw 2010-07-07 11:30:08