Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从程序集xxx加载类型xxx_C#_.net_Reflection - Fatal编程技术网

C# 无法从程序集xxx加载类型xxx

C# 无法从程序集xxx加载类型xxx,c#,.net,reflection,C#,.net,Reflection,我正在制作一个windows应用程序,其中用户从组合框中选择一种类型。基于选择,我希望使用反射创建相应类型的实例并调用其中一个方法。 我要创建的类型也在同一个windows应用程序中定义为sperate类。但我得到了标题中提到的错误。这是我的密码 表格1代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); cbLogs.SelectedInd

我正在制作一个windows应用程序,其中用户从组合框中选择一种类型。基于选择,我希望使用反射创建相应类型的实例并调用其中一个方法。 我要创建的类型也在同一个windows应用程序中定义为sperate类。但我得到了标题中提到的错误。这是我的密码

表格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的SustainabilityXpress类:

public class SustainabilityXpress: ILogBase
{
    string LogName = "SUSTAINABILITYXPRESS";
    public string process()
    {
        return "Sustainabilityxpress";
    }
}

我刚刚注意到“可持续发展新闻”中有一个空格。尝试删除它,可能这就是问题所在。

我不确定这是否仅在帖子中,但
“SustabilityXPress”
中不能有空格。您还必须使用完整的类名(包括名称空间)


如果这没有帮助,可能类型根本不在程序集中
GetExecutionGassembly
获取当前代码所在的程序集…

请确保正确命名
SustabilityxPress
类--您是否忘记在其名称空间前面加上前缀?(例如,“Name.Space.sustabilityxpress”)

此外,检查以确保满足所有要求

而且,正如@Grzenio所指出的,名字
sustabilityxpress
中可能有输入错误

public class SustainabilityXpress: ILogBase
{
    string LogName = "SUSTAINABILITYXPRESS";
    public string process()
    {
        return "Sustainabilityxpress";
    }
}