Context : In our application there two separate dlls which are repositories of variables configured for different types of hardware .These dlls has same name and is referenced from our application, Now every time if we wanted to test the different type of hardware we have to copy the relevant dll to the location where application is running. I am looking for a workaround.
References : I have seen the following threads ,
1) .csproj multiple hint paths for an assembly
2) https://whathecode.wordpress.com/2012/10/07/conditional-project-or-library-reference-in-visual-studio/
Question:
Can i declare a variable in a text file say <hardware> type1 </hardware>
and import that text file in my csproj file and assign the appropriate reference to my application ?
Any help ..
Following is the test application which replicates our project. SignalPool1, SignalPool2 have the same class name and are repositories for two different hardware configurations available in two different locations of our project. If we wanted to test the hardware1 we would remove the current reference and add the reference of signal pool which is relevant there same applies for hardware2. Now to avoid this manual work i wanted to automate this process either to declare a variable on XML and access the variable in csproj file to decide which to access at the time of Compilation/Running.
Currently to avoid this problem i have a separate exe which reads the xml and decides to copy them into one common folder.This exe will be called at the time of prebuild event in our project.
// Signal Pool 1 File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SignalPool
{
public abstract class SignalPool
{
public abstract string PreExec();
public abstract string PostExec();
public abstract string VnVExec();
public static string HWVar1 = "HWVar1";
public static string HWVar2 = "HWVar2";
public static string HWVar3 = "HWVar3";
public static string HWVar4 = "HWVar4";
}
}
// Signal pool2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SignalPool
{
public abstract class SignalPool
{
public abstract string PreExec();
public abstract string PostExec();
public abstract string VnVExec();
public static string HWVar1 = "HWVar5";
public static string HWVar2 = "HWVar6";
public static string HWVar3 = "HWVar7";
public static string HWVar4 = "HWVar8";
}
}
//Accessing pool Variables
Main File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace TestLibraries
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Testhardware th = new Testhardware();
th.functionToValues();
}
private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.Contains("SignalPool")) //Put in the name of your assembly
{
//var configValue = XDocument.Parse("<config><hardware>type1</hardware></config>").Document.Descendants("hardware").First().Value;
var configValue = XDocument.Load(@"C:\Users\ha23031\Documents\Visual Studio 2010\Projects\TestLibraries\TestLibraries\TestInfo.xml").Document.Descendants("IsRealHMI").First().Value;
if (configValue == "false")
{
return System.Reflection.Assembly.LoadFile(@"C:\Users\ha23031\Documents\Visual Studio 2010\Projects\TestLibraries\SignalPool\bin\Debug\SignalPool.dll");
}
else if (configValue == "true")
{
return System.Reflection.Assembly.LoadFile(@"C:\Users\ha23031\Documents\Visual Studio 2010\Projects\SignalPool\bin\Debug\SignalPool.dll");
}
}
return null;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLibraries
{
class Testhardware : SignalPool.SignalPool
{
public override string PostExec()
{
return string.Empty;
}
public override string PreExec()
{
return string.Empty;
}
public override string VnVExec()
{
return string.Empty;
}
public string functionToValues()
{
// This is how i access variables based on the loaded variables
string s = SignalPool.SignalPool.HWVar1;
return string.Empty;
}
}
}