C
C#5d ago
David

MSBuild Condition for Design-Time and Compile-Time

I have the following MSBuild Snippet
<ItemGroup>
<Compile Remove="Program.cs"></Compile>
<Compile Update="Program2.cs"></Compile>
<None Visible="true" Include="Program.cs"></None>
</ItemGroup>
<ItemGroup>
<Compile Remove="Program.cs"></Compile>
<Compile Update="Program2.cs"></Compile>
<None Visible="true" Include="Program.cs"></None>
</ItemGroup>
Program2.cs represents Generated Code. I want to achieve this: * Do not compile Program.cs * Enable Intelisense on Program.cs. In general treat it as if it was Compiled during design time * Program2.cs should not be referenceable from other Code. Redirecting the Debugger from Program2.cs to Program.cs using #line directives already works. How can this be achieved? I think it may be possible using some Condition for Design/Compile-Time or using Targets.
4 Replies
lycian
lycian5d ago
This sounds like a very odd use case but if you need to do something in design time only follow https://github.com/dotnet/project-system/blob/main/docs/design-time-builds.md#determining-whether-a-target-is-running-in-a-design-time-build
GitHub
project-system/docs/design-time-builds.md at main · dotnet/project-...
The .NET Project System for Visual Studio. Contribute to dotnet/project-system development by creating an account on GitHub.
Mayor McCheese
Can you state your goal? What are you trying to achieve?
David
DavidOP5d ago
Thanks. In the meantime I found an alternative:
<ItemGroup>
<Compile Remove="Program2.cs"></Compile>
<None Visible="true" Include="Program2.cs"></None>
</ItemGroup>

<Target Name="MyTarget" AfterTargets="BuildOnlySettings">
<ItemGroup>
<Compile Remove="Program.cs"></Compile>
<Compile Include="Program2.cs"></Compile>
</ItemGroup>
</Target>
<ItemGroup>
<Compile Remove="Program2.cs"></Compile>
<None Visible="true" Include="Program2.cs"></None>
</ItemGroup>

<Target Name="MyTarget" AfterTargets="BuildOnlySettings">
<ItemGroup>
<Compile Remove="Program.cs"></Compile>
<Compile Include="Program2.cs"></Compile>
</ItemGroup>
</Target>
Not sure which is better I guess it is called Metaprogramming. I know there is Fody, but IL is not easy and I think debugging could get hard. But honestly I have never used it, so I may be wrong. Metalama is propriotary and Source Generators are limited.
Mayor McCheese
Fody does il weaving and is frequently labeled under AOP, aspect oriented programming. Fody and postsharp both have weird licensing models Some more advanced DI containers do offer some things that fody and postsharp ( and other weavers ) offer, but at a performance cost, sometimes a high performance cost. Back in the day things like caching method results were pretty popular. For example:
[CacheResult(key = "GetPeopleResult", expiration = "30m")]
public List<Person> GetPeople()
{
return dbContext.People.ToList();
}

[CacheResult(key = "GetPersonResult", expiration = "30m")]
public Person? GetPerson(int id)
{
return dbContext.People.FirstOrDefault(p=>p.Id == id);
}

[InvalidateCache(key = "GetPeopleResult")]
[InvalidateCache(key = "GetPersonResult")]
public void AddPerson(Person person)
{
dbContext.People.Add(person);
}

[InvalidateCache(key = "GetPeopleResult")]
[InvalidateCache(key = "GetPersonResult")]
public void DeletePerson(Person person)
{
dbContext.People.Delete(person);
}
[CacheResult(key = "GetPeopleResult", expiration = "30m")]
public List<Person> GetPeople()
{
return dbContext.People.ToList();
}

[CacheResult(key = "GetPersonResult", expiration = "30m")]
public Person? GetPerson(int id)
{
return dbContext.People.FirstOrDefault(p=>p.Id == id);
}

[InvalidateCache(key = "GetPeopleResult")]
[InvalidateCache(key = "GetPersonResult")]
public void AddPerson(Person person)
{
dbContext.People.Add(person);
}

[InvalidateCache(key = "GetPeopleResult")]
[InvalidateCache(key = "GetPersonResult")]
public void DeletePerson(Person person)
{
dbContext.People.Delete(person);
}
In this scenario, read operations are cached, and continue to be cached until a write operation is executed, or the time expires. Autofac supports things like this with some custom work. I don't think that's what you're looking for though.
Want results from more Discord servers?
Add your server