本文介绍了从文本文件变量的csproj文件中的多个路径的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:
在我们的应用有两个独立的dll它们的配置为不同类型的硬件。这些变量的dll库具有相同的名称,并从我们的应用程序,现在每次引用如果我们想测试不同类型的硬件,我们有相关的DLL复制到运行应用程序的位置。我要寻找一个解决办法。



参考文献:
我已经看到下面的线程,



1) href=\"http://stackoverflow.com/questions/15666618/csproj-multiple-hint-paths-for-an-assembly\"> .csproj的多路径提示



2)的



问:
我可以宣布在文本文件中的变量表示的 <五金GT; TYPE1< /硬件和GT; 并导入文本文件在我的csproj文件,然后指定相应的参考,以我的应用程序?



任何帮助..



以下是它复制我们的项目的测试应用程序。 SignalPool1,SignalPool2具有相同的类名,并在我们的项目中两个不同的位置提供了两种不同的硬件配置存储库。如果我们想测试hardware1我们将删除当前的参考和补充信号池的基准是相关的有同样适用于hardware2。现在,为了避免这种手工工作我想这个过程自动化要么宣布XML的变量,并访问变量的csproj文件来决定要在编译/运行时的访问。



目前,以避免这个问题,我有一个单独的EXE它读取XML,并决定将它们复制到一个共同的folder.This exe将在我们的项目预生成事件的时间被调用。



//信号池1文件

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间SignalPool
{
公共抽象类SignalPool
{
公共抽象的字符串PreExec();
公共抽象的字符串PostExec();
公共抽象的字符串VnVExec();

公共静态字符串HWVar1 =HWVar1;
公共静态字符串HWVar2 =HWVar2;
公共静态字符串HWVar3 =HWVar3;
公共静态字符串HWVar4 =HWVar4;
}



}



//信号POOL2

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间SignalPool
{
公共抽象类SignalPool
{
公共抽象的字符串PreExec();
公共抽象的字符串PostExec();
公共抽象的字符串VnVExec();

公共静态字符串HWVar1 =HWVar5;
公共静态字符串HWVar2 =HWVar6;
公共静态字符串HWVar3 =HWVar7;
公共静态字符串HWVar4 =HWVar8;
}
}



//访问池变量



主文件

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Xml.Linq的;

命名空间TestLibraries
{
类节目
{
静态无效的主要(字串[] args)
{

AppDomain.CurrentDomain.AssemblyResolve + = CurrentDomain_AssemblyResolve;

Testhardware日=新Testhardware();
th.functionToValues​​();



}

私有静态System.Reflection.Assembly CurrentDomain_AssemblyResolve(对象发件人,ResolveEventArgs参数)
{
如果(args.Name.Contains(SignalPool))//把你的装配
{
// VAR configValue = XDocument.Parse(<的名称;配置><硬件> TYPE1< /hardware></config>\").Document.Descendants(\"hardware\").First().Value;
VAR configValue = XDocument.Load(@C:\Users\ha23031\Documents\Visual工作室2010\Projects\TestLibraries\TestLibraries\TestInfo.xml)。Document.Descendants( IsRealHMI)第一()值。;
如果(configValue ==假)
{
返回System.Reflection.Assembly.LoadFile(@C:\Users\ha23031\Documents\Visual工作室2010\\ \\Projects\TestLibraries\SignalPool\bin\Debug\SignalPool.dll);
}
,否则如果(configValue ==真)
{
返回System.Reflection.Assembly.LoadFile(@C:\Users\ha23031\Documents \Visual工作室2010\Projects\SignalPool\bin\Debug\SignalPool.dll);
}
}
返回NULL;
}


}



}

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间TestLibraries
{
类Testhardware:SignalPool.SignalPool
{
公众覆盖字符串PostExec()
{
返回的String.Empty;
}

公众覆盖字符串PreExec()
{
返回的String.Empty;
}

公众覆盖字符串VnVExec()
{
返回的String.Empty;
}

公共字符串functionToValues​​()
{
//这是我如何访问基于被装入变量
字符串s = SignalPool.SignalPool变量.HWVar1;
回报率的String.Empty;
}
}
}


解决方案

我可以声明在一个文本文件中的变量说type1和导入文本文件在我的csproj文件,然后指定相应的参考,以我的应用程序吗?



是不是的的描述?



查找定义的变量的 SomeProject 的在配置文件中的ProjectReferences.txt的这是我从.csproj的文件加载。



ProjectReferences.txt

 <项目的xmlns =htt​​p://schemas.microsoft.com/developer/msbuild/2003> 
<的PropertyGroup条件=$(配置)=='调试随着项目引用'>
< SomeProject> .. \SomeProject< / SomeProject>
< /&的PropertyGroup GT;
< /项目>



.csproj的文件

 <导入项目=.. \ProjectReferences.txt/> 
<选择与GT;
<当条件=存在($(SomeProject))>
<&的ItemGroup GT;
< ProjectReference包括=$(SomeProject)\SomeProject.csproj>
<项目> {6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}< /项目>
<名称>&SomeProject LT; /名称>
< / ProjectReference>
< /&的ItemGroup GT;
< /当>
<否则>
<&的ItemGroup GT;
<参考包括=SomeProject>
< HintPath> .. \Libraries\SomeProject.dll< / HintPath>
< /参考和GT;
< /&的ItemGroup GT;
< /&否则GT;
< /选择与GT;


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;
    }
}
}
解决方案

"Can i declare a variable in a text file say type1 and import that text file in my csproj file and assign the appropriate reference to my application ?"

Isn't that exactly what I describe in the blog post you link to?

Look for the defined variable 'SomeProject' in the configuration file 'ProjectReferences.txt' which I load 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>

这篇关于从文本文件变量的csproj文件中的多个路径的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:21