无法加载文件或程序集或其依赖项之一.该系统找不到指定的文件 [英] Could not load file or assembly or one of its dependencies. The system cannot find the file specified

查看:29
本文介绍了无法加载文件或程序集或其依赖项之一.该系统找不到指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的代码

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"的价值nd so for 'type as {Name = "_rounded_tree" FullName = "_rounded_tree"}'.但是保存后,如果我重新启动我的应用程序,我无法加载程序集"的值,获取异常无法加载文件或程序集"_rounded_treeGOLD,版本 = 0.0.0.0,文化 = 中性,PublicKeyToken = null' 或其依赖项之一.系统找不到指定的文件.":"_rounded_treeGOLD, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 并且返回类型变为 null 这不应该发生

I have a base class named "leaf" and a userdefinedclass named "roundedtree" when 'xmlSerializableType.Name' becomes userdefined class '_rounded_tree', first time i am getting value for 'assembly as _rounded_treeGOLD, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and so for 'type as {Name = "_rounded_tree" FullName = "_rounded_tree"}'. But after saving if i restart my application i cannot load value for 'assembly' getting exception 'Could not load file or assembly '_rounded_treeGOLD, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"_rounded_treeGOLD, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' and return type becomes null this should not happen

对于基类leaf"没有问题,我将获得 xmlSerializableType.Name 作为Root.Systemmodel.leaf"并且type"变为 {Name = "leaf" FullName = "Root.Systemmodel.leaf"} 程序集将是 Root.系统模型,版本=8.0.7.0,文化=中性,PublicKeyToken=83bd062a94e26d58在这种情况下我该怎么办这是一段代码,将为用户定义的类生成程序集

For baseclass "leaf" no issuses i will get xmlSerializableType.Name as " Root.Systemmodel.leaf" and 'type' becomes {Name = "leaf" FullName = "Root.Systemmodel.leaf"} assembly will be Root.Systemmodel, Version=8.0.7.0, Culture=neutral, PublicKeyToken=83bd062a94e26d58 What should i do in these circumstances This is a bit of code which will generate assembly for userdefined class

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);

更新

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

probably my assembly is creating for userdefined class but not saving that may be the reason i am not facing any issue first time, once i close the application i will lose that one see my code

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

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

更新这里我的数据库是 xml 文件.当我检查基类叶子时,我可以看到条目是 Root.Systemmodel.WindowsSystemRoot.Systemmodel, Version=8.0.7.0, Culture=neutral, PublicKeyToken=83bd062a94e26d58<;/AssemblyName> 在这种情况下,如果重新启动我的应用程序没有问题,但对于用户定义的类roundedtree"xml 条目是 _rounded_tree<AssemblyName>_rounded_treeGOLD,版本=0.0.0.0,Culture=neutral,PublicKeyToken=null</AssemblyName>这是第一次没有问题,但如果我重新启动我的应用程序,我会遇到异常

UPDATE Here my database is xml files. When i checked for base class leaf i can see the entry is <Name>Root.Systemmodel.WindowsSystem</Name><AssemblyName>Root.Systemmodel, Version=8.0.7.0, Culture=neutral, PublicKeyToken=83bd062a94e26d58</AssemblyName> in this case if restart my application no issues, but for user defined class "roundedtree" xml entry is <Name>_rounded_tree</Name> <AssemblyName>_rounded_treeGOLD, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</AssemblyName> Here first time no issues, but if i restart my application i am getting exception

推荐答案

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

it happens because maybe the assembly you're going to load references to the another assembly that not exist in the same directory or system directory put all assembly in same folder I,ve just copy paste my code but its clear

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, args.Name.IndexOf(",")) + ".dll";
            break;
        }

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

    //Return the loaded assembly.
    return MyAssembly;
}

这篇关于无法加载文件或程序集或其依赖项之一.该系统找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆