0

I'm working a VSIX project that is a command to build a DLL with reflection and my target DLL does reflection too.

I load the target DLL with

        asmBase = System.IO.Path.GetDirectoryName(assemblyName);

        var settings = System.Configuration.ConfigurationManager.ConnectionStrings[0];

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

And here is my AssemblyResolve event

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

My issue is when my target DLL executes this line of code:

        string cachePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
        cachePath = Path.GetDirectoryName(cachePath);

The GetExecutingAssembly method is throwing a "path is not legal form" exception for me.

What is the expected path result of the GetExecutingAssembly method?

I tried to use

        System.Diagnostics.Debug.WriteLine(cachePath);

But it didn't show the message.

  • I think that this [question](http://stackoverflow.com/questions/6198392/check-whether-a-path-is-valid) will help you. – vappolinario Oct 13 '15 at 13:24
  • unfortunately that question didn't help me. I updated the question with more information. – Renato Ramos Nascimento Oct 13 '15 at 13:42
  • In what context does this code run? How is it loaded? A plugin system of some sort? Or is it generated at runtime? Or do you load the assembly from a byte array? – CodeCaster Oct 13 '15 at 13:42
  • Instead of `System.Diagnostics.Debug.WriteLine(cachePath)`, could you write the path out to a simple file? I'm guessing that `stdout` (er.. guess `debug`) is null or not available for the installer during its process. Either that or the debugger is having some problems capturing the installer's debug output. – code4life Oct 13 '15 at 13:49
  • Reading at [msdn](https://msdn.microsoft.com/en-us/library/system.reflection.assembly.location(v=vs.110).aspx#Anchor_2) the `Location` property is affected by shadow-copying and maybe it's the case with a VSIX, can you try using the suggested method on the link ? In this [other question](http://stackoverflow.com/questions/4804950/receiving-the-path-of-a-vs2010-extension) there is a code snippet explaining how to use `CodeBase` – vappolinario Oct 13 '15 at 14:00
  • @vappolinario the result for the vsix calling the snippet is "C:\USERS\My User\APPDATA\LOCAL\MICROSOFT\VISUALSTUDIO\14.0EXP\EXTENSIONS\My User\MyCommand\1.0". I guess the VS doesn't have permission to write or read from this folder, is that correct? – Renato Ramos Nascimento Oct 13 '15 at 14:16
  • Theoretically the VS is running with your user and have access to your user folder. Besides, has the error changed ? Indicating that is a permission problem ? Have you verified if the path exists ? – vappolinario Oct 13 '15 at 14:39
  • @vappolinario the answer is no for all your questions, i can't change the target dll code. – Renato Ramos Nascimento Oct 13 '15 at 16:10

0 Answers0