加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法加载文件或程序集或其依赖项之一.该系统找不到指定的

发布时间:2020-12-16 01:49:02 所属栏目:百科 来源:网络整理
导读:我有这样的代码 public static Type ToType(XmlSerializableType xmlSerializableType){ string func = "XmlSerialzationType.ToType"; Type type = null; if (xmlSerializableType != null xmlSerializableType.Name != string.Empty) { type = Type.GetTyp
我有这样的代码

public static Type ToType(XmlSerializableType xmlSerializableType)
{
  string func = "XmlSerialzationType.ToType";
  Type type = null;
  if (xmlSerializableType != null && xmlSerializableType.Name != string.Empty)
  {
    type = Type.GetType(xmlSerializableType.Name);
    if (type == null)
    {
      // May be a user defined class
      try
      {
        Assembly assembly = Assembly.Load(xmlSerializableType.AssemblyName);
        type = assembly.GetType(xmlSerializableType.Name);
      }
      catch (Exception ex)
      {
        TestDebug.DebugTraceSevere(func,"Exception " + ex.ToString());
      }
    }
  }
  return type;
}

我有一个名为“leaf”的基类和一个名为“roundedtree”的用户定义类
当’xmlSerializableType.Name’成为用户定义的类’_rounded_tree’时,我第一次获得’assembly as _rounded_treeGOLD,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null’的值,因此’type as {Name =“_rounded_tree “FullName =”_ ground_tree“}’.但保存后,如果我重新启动我的应用程序,我无法加载“程序集”获取异常’无法加载文件或程序集’_rounded_treeGOLD,PublicKeyToken = null’或其依赖项之一的值.系统找不到指定的文件.“:”_ rounded_treeGOLD,PublicKeyToken = null’并且返回类型变为null这不应该发生

对于基类“leaf”没有发布,我将获得xmlSerializableType.Name作为“Root.Systemmodel.leaf”并且’type’变为{Name =“leaf”FullName =“Root.Systemmodel.leaf”}程序集将是Root.Systemmodel,Version = 8.0.7.0,PublicKeyToken = 83bd062a94e26d58
在这种情况下我该怎么办
这是一些代码,它将为用户定义的类生成程序集

public Type CreateType()
      {
         string func = "ManagedClass.CreateType";

         // Create instances of AssemblyBuilder and ModuleBuilder for the new Type
         AppDomain myDomain = Thread.GetDomain();
         AssemblyName myAsmName = new AssemblyName();

         // Create the assembly name by appending the machine name to the typename.
         myAsmName.Name = this.TypeName + Environment.MachineName;
         // Define assembly that can be executed but not saved
         this.UserClassAssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName,AssemblyBuilderAccess.Run);
         // Create dynamic module with symbol information
         this.UserClassModuleBuilder = this.UserClassAssemblyBuilder.DefineDynamicModule("userdefinedmodule",true);

UPDATE

可能我的程序集是为用户定义的类创建但不保存可能是我第一次没有遇到任何问题的原因,一旦我关闭应用程序我会失去那个看到我的代码

// Define assembly that can be executed but not saved
         this.UserClassAssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName,

AssemblyBuilderAccess.Run);
如何克服这种情况

UPDATE
这里我的数据库是xml文件.当我检查基类叶时,我可以看到条目是< Name> Root.Systemmodel.WindowsSystem< / Name>< AssemblyName> Root.Systemmodel,PublicKeyToken = 83bd062a94e26d58< / AssemblyName&gt ;在这种情况下,如果重启我的应用程序没有问题,但对于用户定义的类“roundedtree”xml条目是< Name> _rounded_tree< / Name> < AssemblyName> _rounded_treeGOLD,PublicKeyToken = null< / AssemblyName>
这里第一次没有问题,但如果我重新启动我的应用程序我会得到例外

解决方法

它发生的原因可能是您要加载对另一个程序集的引用,该程序集不存在于同一目录或系统目录中,而是将所有程序集放在同一文件夹中
我,我只是复制粘贴我的代码,但它清楚

private string asmBase;
public Type[] GetAllTypeinAssembly(string assemblyName)
{
    asmBase = System.IO.Path.GetDirectoryName(assemblyName);

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    System.Reflection.Assembly asm = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(assemblyName));//domain.Load(bt) ;// 

    return asm.GetTypes();
}


private Assembly CurrentDomain_AssemblyResolve(object sender,ResolveEventArgs args)
{
    //This handler is called only when the common language runtime tries to bind to the assembly and fails.

    //Retrieve the list of referenced assemblies in an array of AssemblyName.
    Assembly MyAssembly,objExecutingAssemblies;
    string strTempAssmbPath = "";
    objExecutingAssemblies = args.RequestingAssembly;
    AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();

    //Loop through the array of referenced assembly names.
    foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
    {
        //Check for the assembly names that have raised the "AssemblyResolve" event.
        if (strAssmbName.FullName.Substring(0,strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0,args.Name.IndexOf(",")))
        {
            //Build the path of the assembly from where it has to be loaded.                
            strTempAssmbPath = asmBase + "" + args.Name.Substring(0,")) + ".dll";
            break;
        }

    }
    //Load the assembly from the specified path.                    
    MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

    //Return the loaded assembly.
    return MyAssembly;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读