C#工程批量添加dll和脚本

出于想在没有unity的环境下用unity的dll来写代码的目的(嗯,很sb的需求),自己建了个空的c#工程,再把动态库和代码放到工程目录下,用python脚本给工程文件批量添加dll和cs文件。

1.分析

首先得知道在用Visual Studio给解决方案添加引用的dll和代码文件时都发生了什么。打开解决方案文件(.csproj)基本结构如下,可以看到其实就是个xml文件。看到最后会发现有两个ItemGroup节点,一个描述了项目所引用的dll,一个描述了项目所有的代码文件。所以,只需要写个工具修改xml文件就可以了,我自己是选择了用python来做。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{B0E60D7F-C693-4738-8979-1476A6E5E384}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>XFrameworkWithoutUnity</RootNamespace>
    <AssemblyName>XFrameworkWithoutUnity</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
    <Reference Include="UnityEditor">
      <HintPath>Temp\DLL\UnityEditor.dll</HintPath>
    </Reference>
    <Reference Include="UnityEditor.UI">
      <HintPath>Temp\DLL\UnityEditor.UI.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine">
      <HintPath>Temp\DLL\UnityEngine.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.CoreModule">
      <HintPath>Temp\DLL\UnityEngine.CoreModule.dll</HintPath>
    </Reference>
    <Reference Include="UnityEngine.UI">
      <HintPath>Temp\DLL\UnityEngine.UI.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Assets\XFramework\Core\Editor\AssetOpenModeWindow.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

2.撸工具

没啥可说的了,python也好,其它语言也好,修改一下两个节点就行了。


版权声明:本文为xdedzl原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。