首先,我写了一个函数public static Type GetClassType(string assembly, string @class)
{
    if (@class + "" == "")
        throw new Exception(
            "提供通用查询的类名不能为空,且必须是类的全名");
    Type t = null;
    if (assembly + "" == "")
    {
        t = Type.GetType(@class);
    }
    else
    {
        try
        {
            Assembly ass = Assembly.LoadFile(assembly);
            t = ass.GetType(@class);
        }
        catch (System.IO.FileNotFoundException ex)
        {
            throw new Exception("加载类型错误!"+assembly + " " + ex.Message);
        }
    }
    return t;
}
下面是调用该方法代码//默认Assembly
if (sAssembly + "" != "")
{
    sAssembly = ZHXX.Web.SiteState.MapPathApp("bin/" + sAssembly);
}
Type t = Utility.Classes.GetClassType(sAssembly, sClass);
object o = Utility.Classes.CreateIntance(t);
//if(  !(o is ISearch )){//用这个判断语句和下面判断语句是同一个问题
if(  !t.IsSubclassOf (typeof (ISearch))){
    throw new ZHXX.Utility.CustException (sClass + "类型不是 ISearch 类型,输入类型不正确");//在第二次调用的时候抛出异常
}
ISearch search=(ISearch )o;第一次调用
sAssembly ="";没有任何异常,成功返回
第二次调用
sAssembly ="TF2008Module.dll"
这个dll是已经加入到项目工程中的一个dll,抛出了异常
感到很奇怪
下面是异常时候的调试信息
t | {Name = "SearchDemo" FullName = "TF2008Module.Search.SearchDemo"} | System.Type {System.RuntimeType}
o | {TF2008Module.Search.SearchDemo} | object {TF2008Module.Search.SearchDemo}
o is ISearch | true | bool
t.IsSubclassOf(typeof(ISearch)) | false | bool
(SearchDemo)o | {TF2008Module.Search.SearchDemo} | TF2008Module.Search.SearchDemo

解决方案 »

  1.   

    问题奇怪之处在于,当我使用下面的判断语句的时候
    if(  !(o is ISearch )){
        throw new ZHXX.Utility.CustException (sClass + "类型不是 ISearch 类型,输入类型不正确");//在第二次调用的时候抛出异常
    }这句,我在观察窗口中明明看见 !(o is ISearch )=fase;(o is ISearch )=true;
    它却偏偏执行了 if块里面的内容
    又或
    在调试信息中出现了两个截然相反的判断
    o is ISearch | true | bool 
    t.IsSubclassOf(typeof(ISearch)) | false | bool 
    搞不明白,望高手来解决
      

  2.   

    is,跟IsSubClassOf应该都不会有问题,你在GetClassType这里抛出了异常,你在外面蒱获一下看看,
      

  3.   

    建议清理下debug 重新编译下程序!
      

  4.   

    帮你up一下,你可以把这个dll强名试试
      

  5.   

    我在外面写了一个DLL,跟这个工程无关的,拷贝到BIN目录下然后调用就没问题了public class Class1:ISearch
    {
    public ResultItemCollection SearchByKeywords(...)
    {
        .....
    }
    调用:
    assembly="tmp1.dll" @class="tmp1.Class1"如果assembly="TF2008Module.dll"则会出错,清除了缓存也不行
    我怀疑是本工程里面使用Assembly.LoadFile(本工程的dll)出错
    但为什么出错需要高手来点明