为什么Assembly.GetType()返回null?

xhwwy 2009-10-10 09:10:32
我有一个程序片断
string className = profilePath + ".PetShopProfileProvider";
Assembly tmp = Assembly.Load(profilePath);
System.Type type = tmp.GetType(className);
跟踪调试tmp不为空,说明load程序集成功,可是后面的tmp.GetType(className)却总是返回null,明明className类是存在的啊,哪位高人指点一下啊?
...全文
1131 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
diagochen 2010-04-08
  • 打赏
  • 举报
回复
太感谢了,我也是碰到这个问题,
wartim 2009-10-10
  • 打赏
  • 举报
回复
System.Type type = tmp.GetType("DMProfileDAL.PetShopProfileProvider");
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
PetShopProfileProvider的定义如下:

using System;
using System.Data;
using Dm;
using PetShop.Model;
using PetShop.IProfileDAL;
using System.Collections.Generic;
using PetShop.DBUtility;
using System.Text;

namespace DMProfileDAL
{
class PetShopProfileProvider : IPetShopProfileProvider
{
wartim 2009-10-10
  • 打赏
  • 举报
回复
System.Type type = tmp.GetType("PetShop.DMProfileDAL.PetShopProfileProvider");

你确信
PetShopProfileProvider的命名空间是PetShop.DMProfileDAL吗
wartim 2009-10-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xhwwy 的回复:]
我的profilePath,是不带.dll后缀的,可以load成功啊,我刚刚试着改成
    Assembly tmp = Assembly.Load(profilePath+".dll");
怎么反而不成功了呢?
[/Quote]

看错了,你用的是load,要用文件名调用外部assembly用loadfrom

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 a
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

Assembly A = Assembly.Load("WindowsApplication83");
Type T = A.GetType("a.Z");
Object Obj = Activator.CreateInstance(T);
T.GetMethod("X").Invoke(Obj, null);
}
}

public class Z
{
public void X()
{
MessageBox.Show("!");
}
}
}
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
我现在改成了
Assembly tmp = Assembly.LoadFile(@"E:\work\provider应用\.NET Pet Shop 4.0\Web\Bin\PetShop.DMProfileDAL.dll");
System.Type type = tmp.GetType("PetShop.DMProfileDAL.PetShopProfileProvider");
仍旧是tmp成功取到了,但type还是为null
chen_ya_ping 2009-10-10
  • 打赏
  • 举报
回复
Type t=Assembly.LoadFile(@"E:\aspnet35\TestWeb35\TestWeb35\bin\TestWeb35.dll").GetType("TestWeb35.AllClass.Class1");
Console.Write(t.Name);
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
这段程序是MS的PetShop4.0里面的,原本是
string className = profilePath + ".PetShopProfileProvider";
return (PetShop.IProfileDAL.IPetShopProfileProvider)Assembly.Load(profilePath).CreateInstance(className);

由于总是返回null,所以我增加了两句想看一下中间结果
Assembly tmp = Assembly.Load(profilePath);
System.Type type = tmp.GetType(className);
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
晕,怎么贴本地图片啊?
chen_ya_ping 2009-10-10
  • 打赏
  • 举报
回复
Type t=Assembly.Load("MyReflection").GetType("MyReflection.Class1");
Console.Write(t.Name);
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
实在是找不出来了,跟踪时tmp的watch窗口如下

chen_ya_ping 2009-10-10
  • 打赏
  • 举报
回复
Class1 c=(Class1)(Assembly.Load("MyReflection").CreateInstance("MyReflection.Class1"));
Console.Write(c.GetType().Name);
trentliu 2009-10-10
  • 打赏
  • 举报
回复
你仔细查代码吧,再仔细一点
xhwwy 2009-10-10
  • 打赏
  • 举报
回复
我的profilePath,是不带.dll后缀的,可以load成功啊,我刚刚试着改成
Assembly tmp = Assembly.Load(profilePath+".dll");
怎么反而不成功了呢?
wartim 2009-10-10
  • 打赏
  • 举报
回复
string className = profilePath + ".PetShopProfileProvider";
Assembly tmp = Assembly.Load(profilePath);
System.Type type = tmp.GetType(className);

看你的load里写profilePath,应该是文件名,那怎么能用文件名去反射类呢
Assembly tmp = Assembly.Load("a.dll");
className = "a.dll" + ".PetShopProfileProvider";
肯定是不对的么
应该是
className = "NameSpace名" + ".PetShopProfileProvider";


xhwwy 2009-10-10
  • 打赏
  • 举报
回复
我用.NET Reflector看了,有这个class,是可访问的,大小写和拼写也没有错
lijie__520 2009-10-10
  • 打赏
  • 举报
回复
string className = profilePath + ".PetShopProfileProvider";
Assembly tmp = Assembly.Load(profilePath);
System.Type type = tmp.GetType(className);
profilePath等于className???


程序集名字错了 吧。。。
trentliu 2009-10-10
  • 打赏
  • 举报
回复
用 .NET Reflector 看看类名大小写怎么拼的
CsToD 2009-10-10
  • 打赏
  • 举报
回复
跟能不能访问没关系,internal类也能找得到
肯定是你名字写错了,看下是不是没写命名空间,或者大小写拼错
trentliu 2009-10-10
  • 打赏
  • 举报
回复
.NET Reflector

你用.NET Reflector 看下这个Assembly里到底有没有这个class, 是不是可以访问的
加载更多回复(4)
动态调用WebService的方法类,可以不用在VS本地引用,直接动态调用即可。 样例如下: /// /// 实例化WebServices /// /// WebServices地址 /// 调用的方法 /// 把webservices里需要的参数按顺序放到这个object[]里 public static object InvokeWebService(string url, string methodname, object[] args) { //这里的namespace是需引用的webservices的命名空间,我没有改过,也可以使用。也可以加一个参数从外面传进来。 //string @namespace = "Nq.Application.WebService"; string @namespace = "client"; try { //获取WSDL WebClient wc = new WebClient(); //Stream stream = wc.OpenRead(url); Stream stream = wc.OpenRead(url + "?WSDL"); ServiceDescription sd = ServiceDescription.Read(stream); string classname = sd.Services[0].Name; ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); CSharpCodeProvider csc = new CSharpCodeProvider(); //ICodeCompiler icc = csc.CreateCompiler(); //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false;//动态编译后的程序集不生成可执行文件 cplist.GenerateInMemory = true;//动态编译后的程序集只存在于内存中,不在硬盘的文件上 cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); //编译代理类 CompilerResults cr =

110,538

社区成员

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

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

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