Multiple tooltip paths in csproj file from text variable

Context: In our application, there are two separate DLLs that are repositories of variables configured for different types of equipment. These DLLs have the same name and refer to our application. Now every time we want to test different types of hardware, we have to copy the corresponding dlls to the place where the application is running. I am looking for a workaround.

Links: I saw the following topics,

1) .csproj several hints ways to build

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 this text file into a csproj file and assign an appropriate link to my application?

Any help ..

The following is a test application that replicates our project. SignalPool1, SignalPool2 have the same class name and are repositories for two different hardware configurations available in two different places in our project. If we wanted to test hardware1, we would delete the current link and add a signal pool link, which is the same as for hardware2. Now, to avoid this manual work, I wanted to automate this process to declare a variable in XML and access the variable in the csproj file to decide who to contact at compile / runtime.

Currently, to avoid this problem, I have a separate exe that reads xml and decides to copy them to one shared folder. This exe will be called during the prebuild event in our project.

// Signal pool file 1

 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 Pool 2

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

// Access to 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; } } } 
+2
c # msbuild csproj
Dec 03 '15 at 7:03
source share
2 answers

"Can I declare a variable in the say type1 text file and import this text file into the csproj file and assign an appropriate link to my application?"

Isn't that what I am describing in a blog post that you link to ?

Look at the specific "SomeProject" variable in the "ProjectReferences.txt" configuration file, which is loaded from the .csproj file.

ProjectReferences.txt

 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="$(Configuration) == 'Debug With Project References'"> <SomeProject>..\SomeProject</SomeProject> </PropertyGroup> </Project> 

.csproj file

 <Import Project="..\ProjectReferences.txt" /> <Choose> <When Condition="Exists($(SomeProject))"> <ItemGroup> <ProjectReference Include="$(SomeProject)\SomeProject.csproj"> <Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project> <Name>SomeProject</Name> </ProjectReference> </ItemGroup> </When> <Otherwise> <ItemGroup> <Reference Include="SomeProject"> <HintPath>..\Libraries\SomeProject.dll</HintPath> </Reference> </ItemGroup> </Otherwise> </Choose> 
+1
Jan 07 '16 at 20:20
source share

You need to load the necessary DLL at the start of your application, before .Net loads it for you. NET will load the default DLL as soon as an instance of this class is created. Therefore, before accessing the properties, be sure to download the appropriate DLL. Make sure you are not accessing these properties in the initial class anywhere, since .Net will load it then.

Something like that:

 class Program { static void Main(string[] args) { //var configValue = XDocument.Parse("<config><hardware>type1</hardware></config>").Document.Descendants("hardware").First().Value; var configValue = XDocument.Load("MyXmlConfig.xml").Document.Descendants("hardware").First().Value; if (configValue == "type1") { System.Reflection.Assembly.LoadFile(@"C:\TEMP\MyAssembly_Type1.dll"); } else if (configValue == "type2") { System.Reflection.Assembly.LoadFile(@"C:\TEMP\MyAssembly_Type2.dll"); } MyRepositoryClass.Initialize(); } } static class MyRepositoryClass { public static void Initialize() { var variable1 = MyAssembly.MyRepository.Variable1; var variable2 = MyAssembly.MyRepository.Variable2; var variable3 = MyAssembly.MyRepository.Variable3; } } 

Alternative on-demand DLL loading (when .Net asks for this):

 class Program { static void Main(string[] args) { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; MyRepositoryClass.Initialize(); } private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.Contains("MyAssembly")) //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("MyXmlConfig.xml").Document.Descendants("hardware").First().Value; if (configValue == "type1") { return System.Reflection.Assembly.LoadFile(@"C:\TEMP\MyAssembly_Type1.dll"); } else if (configValue == "type2") { return System.Reflection.Assembly.LoadFile(@"C:\TEMP\MyAssembly_Type2.dll"); } } return null; } } 
0
Dec 21 '15 at 13:21
source share



All Articles