为什么CreateInstance会返回NULL?

onunix 2011-08-04 10:18:40
文件A:

using System;

using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


using System.Threading;

using System.Windows.Forms;



namespace BackService
{




public class Program
{


}



public class MathClass//要定义为public
{

//私有变量
public static int m_testi;

private int x;

private int y;

//无参数的构造函数

public MathClass()
{

this.x = 0;

this.y = 0;


m_testi = 0;



}


public int Add()
{



return m_testi;

}





}







}



文件B:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using abcTest.view;

using System.Reflection;
using BackService;
using System.Windows.Forms;

namespace abcTest
{
public partial class LoginView : Form
{
public LoginView()
{
InitializeComponent();
this.ControlBox = false;
}



private void button1_Click(object sender, EventArgs e)
{


Assembly asm = Assembly.Load("BackService");



object obj = asm.CreateInstance("BackService.MathClass");



if (null == obj)
{

MessageBox.Show("动态创建对象失败!");

}
else
{
MessageBox.Show("创建好了!");
}


Type t = typeof(MathClass);

MessageBox.Show("开始调用方法!");

int result = (int)t.InvokeMember("Add", BindingFlags.InvokeMethod, null, obj, null);

string aa;

//aa.ToString(result);
string strresult = result.ToString();

MessageBox.Show(strresult);




}

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}




//end class
}

}


这个为什么会出异常呢?
object obj = asm.CreateInstance("BackService.MathClass");

如果改成 object obj = asm.CreateInstance("MathClass");
则返回的obj为NULL。

应该如何修改呢?

...全文
346 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cio1160 2012-06-07
  • 打赏
  • 举报
回复
我也遇到楼主类似的问题,不知道怎么解决,楼主给我讲下,配置选项在哪里设置呢?
给个步骤好么?
onunix 2011-08-04
  • 打赏
  • 举报
回复
谢谢,已经解决了,asm.CreateInstance("BackService.MathClass");是正确的,是配置选项设得有问题,没有把dll文件配置到移动设备上所致。
卧_槽 2011-08-04
  • 打赏
  • 举报
回复
namespace BackService
{
去掉

就可以用asm.CreateInstance("MathClass");
否则只能用 asm.CreateInstance("BackService.MathClass");

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧