我正在制作一个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