2

我的代码是这样的

public static object getClassInstance(string key, params  object[] constructorArgs)
        {
            string assemblyPath = null;
            string customClassName = null;
            DataSet objDataset = getAssemblyInfo(key);
            if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0)
            {
                assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString();
                customClassName = objDataset.Tables[0].Rows[0]["ACA_CLASS_NAME"].ToString();
            }

            Assembly assembly;
            Type type;

            if (assemblyPath != null && assemblyPath != string.Empty)
            {
                assembly = Assembly.LoadFile(assemblyPath);
                type = assembly.GetType(customClassName);
            }
            else // if no customisation
            {
                type = Type.GetType(key);
            }

            object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs);
            if (classInstance == null) throw new Exception("broke");
            return classInstance;

        }

在我上面的代码中,我将密钥作为 namespace-qualified 传递。// 如果代码的自定义部分没有在几天内完美运行。然后它突然开始表现得很诡异。它在某些情况下从字符串返回我的类名,在某些情况下它不会(返回 null)。从密钥生成的所有类名都可以从此方法访问。不存在这样的可访问性问题。

我看到了一个文档http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx。在备注部分它告诉

GetType 仅适用于从磁盘加载的程序集。如果调用 GetType 来查找在使用 System.Reflection.Emit 服务定义的动态程序集中定义的类型,则可能会出现不一致的行为。行为取决于动态程序集是否是持久的,即使用 System.Reflection.Emit.AssemblyBuilderAccess 枚举的 RunAndSave 或 Save 访问模式创建。如果动态程序集是持久的并且在调用 GetType 之前已写入磁盘,则加载程序会在磁盘上找到保存的程序集,加载该程序集,并从该程序集中检索类型。如果调用 GetType 时程序集尚未保存到磁盘,则该方法返回 Nothing。GetType 不理解瞬态动态程序集;因此,调用 GetType 来检索瞬态动态程序集中的类型会返回 Nothing。

那是什么导致我的问题吗?. 谁能帮忙。

4

0 回答 0