I am working on a visual studio add-in that accepts SQL queries in your project, plays the query, and generates a C # wrapper class for the results. I want to make the simplest possible dependency injection, where projects using my add-in provide a class that can provide a db project connection string, among other things.
This interface is defined in my add-in ...
[Serializable] public interface IDesignTimeQueryProcessing { public string ConnectionString { get; } ... }
And the question is: how to define and instantiate a specific implementation, and then use it from the add-in?
Progress?
The interface above is defined in the add-in. I created a link in the target project on the add-in, wrote a specific implementation and put the name of this class in the target web.config project. Now I need to load the target project from the add-in in order to use my specific class.
If I use Assembly.Load () ...
var userAssembly = Assembly.LoadFrom(GetAssemblyPath(userProject)); IQueryFirst_TargetProject iqftp = (IQueryFirst_TargetProject)Activator.CreateInstance(userAssembly.GetType(typeName.Value));
I can successfully load my class, but I am blocking the target assembly and can no longer compile the target project.
If I create a temporary application domain ...
AppDomain ad = AppDomain.CreateDomain("tmpDomain", null, new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(targetAssembly) }); byte[] assemblyBytes = File.ReadAllBytes(targetAssembly); var userAssembly = ad.Load(assemblyBytes);
I get an exception not found in the ad.Load () declaration, even if the bytes of my dll are in memory.
If I use CreateInstanceFromAndUnwrap () ...
AppDomain ad = AppDomain.CreateDomain("tmpDomain", null, new AppDomainSetup { ApplicationBase = Path.GetDirectoryName(targetAssembly) }); IQueryFirst_TargetProject iqftp = (IQueryFirst_TargetProject)ad.CreateInstanceFromAndUnwrap(targetAssembly, typeName.Value);
I get
InvalidCastException. "Cannot pass transparent proxy for input QueryFirst.IQueryFirst_TargetProject"
Does it make me think I'm very close? Why does explicit listing work fine with Assembly.Load () but fail when the same assembly is loaded into a newly created AppDomain?