反射的问题

huangboxi 2009-04-01 04:39:32
有一类库class1,命名空间class1,里面有几个FORM: Form1,Form2....编译出来的为class.dll,我想通过反射调用class1下的Form1的show(),代码如下:

Assembly assembly = Assembly.LoadFrom("class1.dll");
Type type = assembly.GetType("class1.Form1");
Control instance = (Control)assembly.CreateInstance(type.ToString());
MethodInfo method;

method = type.GetMethod("show", BindingFlags.Public |
BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] ,
null
);
method.Invoke((object)instance);

method = null;

运行提示:未将对象引用设置到对象的实例。
经跟踪method为null
请问是什么问题?如何解决?


...全文
134 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
moonshineidolon 2009-04-01
  • 打赏
  • 举报
回复
有才
我姓区不姓区 2009-04-01
  • 打赏
  • 举报
回复
注意大小写
huangboxi 2009-04-01
  • 打赏
  • 举报
回复
万分感谢
wartim 2009-04-01
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Assembly assembly = Assembly.LoadFrom(Application.ExecutablePath);
Type type = assembly.GetType("WindowsApplication5.Form1");
Form F = (Form)Activator.CreateInstance(type);
F.Show();
}
}
}
gomoku 2009-04-01
  • 打赏
  • 举报
回复
默认是大小写相关:

method = typeof(Form).GetMethod("Show", BindingFlags.Public |
BindingFlags.Instance,
null,
CallingConventions.Any,
Type.EmptyTypes,
null
);

111,126

社区成员

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

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

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