当前位置: 代码迷 >> ASP.NET >> 答,就有分。该如何解决
  详细解决方案

答,就有分。该如何解决

热度:3747   发布时间:2013-02-25 00:00:00.0
答,就有分。
using     Zl;    
 
 
UserInfo     Zl=new     UserInfo();    
Zl.Name= "zhanglei ";    
Response.Write( " <br> "+Zl.Name);    
 
Zl是命名空间。    
UserInfo是类    
这个没有问题。    
 
 
 
Type     myType1     =     Type.GetType( "Zl.UserInfo ");    
if(myType1==null)    
Response.Write( "Error ");    
else    
Response.Write( "Ok ");    
这个就不对了。老是说出错了。老是空的。

------解决方案--------------------------------------------------------
Zl.UserInfo 这个是什么???
直接写UserInfo
------解决方案--------------------------------------------------------
类UserInfo是命名空间Z1下的吗?
------解决方案--------------------------------------------------------
UserInfo Zl=new UserInfo();
这一句有问题,对象名Zl和命名空间Zl重名了。

UserInfo Z2=new UserInfo();

Type myType1 = Type.GetType( "Z2.UserInfo ");
------解决方案--------------------------------------------------------
Type myType1 = Type.GetType( "Zl.UserInfo ");
——————————————————————
类 UserInfo 在命名空间 Zl 中吗?
------解决方案--------------------------------------------------------
Zl.UserInfo
不存在这个东西
------解决方案--------------------------------------------------------
干嘛 弄个和namespace重名的变量啊。。。。
------解决方案--------------------------------------------------------
编程序不要自己给自己找麻烦
------解决方案--------------------------------------------------------
zl不要重複了
------解决方案--------------------------------------------------------

------解决方案--------------------------------------------------------
up
------解决方案--------------------------------------------------------
本来程序都很麻烦了,自己再找麻烦,楼主这人,肯定爱折腾

------解决方案--------------------------------------------------------
Type myType1 = Type.GetType( "Zl.UserInfo ");


what?
------解决方案--------------------------------------------------------
Type myType1 = Type.GetType( "Zl.UserInfo ");
这句代码所在的Assembly是不是和Zl.UserInfo不在同一个Assembly.一般这样Type.GetType为空,虽然项目里引用了它。(java的反射就没有这样的问题 T_T)

需要找到Zl.UserInfo所在的框架集。
两种方法:
1 循环当前AppDomain中所有的Assembly来加载此Type

public static Type createType(string typeName)
{
Type type = Type.GetType(typeName);
if (type == null)
{
Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in ass)
{
type = assembly.GetType(typeName);
if (type != null)
return type;
}

return null;
}
else
{
return type;
}
}


2 根据dll文件名加载Assembly,然后从此Assembly取Type.

Assembly ass = Assembly.LoadFile( "Zl.dll ");
Type type = ass.GetType( "Zl.UserInfo ");
------解决方案--------------------------------------------------------
直接写UserInfo
------解决方案--------------------------------------------------------
  相关解决方案