0

Short: I want to post process IL code after C# code is compiled but before it is outputted by MSBuild and before it is run by IDE

Long: I want to create a game, which will have client and server. Most of code will be shared by them, but some types, fields and methods will be exclusively available only on one side. Such elements will have the Side attribute which will have side parameter and will specify which side does the type, method or field belong to. I want to have two build configurations: client and server and I want to remove types, methods and fields from assemblies, which they don't belong to.

I tried to work with MSBuild tasks. I created test solution with two projects: DynamicCodeTest, which is target for modifications, and Task, which contains MSBuild task class Class1 (naming is very poor but this is a test project). Task csproj file does not have anything unusual except that it depends on Microsoft.Build. I tried writing this in DynamicCodeTest.csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\Task\Task.csproj" />
    </ItemGroup>
    <UsingTask TaskName="Task.Class1" AssemblyName="Task"></UsingTask>
    <Target Name="HelloWorld" AfterTargets="AfterCompile">
        <Task.Class1></Task.Class1>
    </Target>
</Project>

but UsingTask was not able to find Task assembly. Then I tried writing this it it:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\Task\Task.csproj" />
    </ItemGroup>
    <UsingTask TaskName="Task.Class1" AssemblyFile="..\Task\bin\$(Configuration)\net6.0\Task.dll"></UsingTask>
    <Target Name="HelloWorld" AfterTargets="AfterCompile">
        <Task.Class1></Task.Class1>
    </Target>
</Project>

but msbuild could not copy Task.dll from obj/... to build/... because it was in use by other process. What is the proper way to do this? If there is a way to do so, I would prefer to make thing with AssemblyName work, because AssemblyFile seems sort of hacky.

0 Answers0