简体   繁体   中英

Could not load file or assembly or one of its dependencies. The system cannot find the file specified

I have a code like this

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

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' a nd 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

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

UPDATE

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); how to overcome this situation

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

I had the same issue, if you are working on SharePoint, sometime the references might not get bundled in WSP so, while deploying, these referenced dlls will not be deployed. Resolution is to manually these from the GAC or force these into WSPs or copy them to the local bin. The last one solved for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Could not load file or assembly 'Extensions' or one of its dependencies. The system cannot find the file specified Could not load file or assembly 'Ajax' or one of its dependencies. The system cannot find the file specified A specific: Could not load file or assembly 'xxxx' or one of its dependencies. The system cannot find the file specified Could not load file or assembly 'XXX' or one of its dependencies. The system cannot find the file specified Could not load file or assembly or one of its dependencies. The system cannot find the file specified. (No GAC allowed) Could not load file or assembly 'ServiceStack' or one of its dependencies. The system cannot find the file specified Could not load file or assembly 'Logging.SNLLoggingStandard3' or one of its dependencies. The system cannot find the file specified Could not load file or assembly 'Elmah' or one of its dependencies. The system cannot find the file specified Could not load file or assembly "path" or one of its dependencies. The system cannot find the file specified XamlParseException: Could not load file or assembly 'ResourceLibrary, …' or one of its dependencies. The system cannot find the file specified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM