Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set MSBuild properties on Build target input items based on metadata

Tags:

msbuild

I'm using a code-generation tool (Entity Framework, in this case) in one of my projects, and it generates code that causes the compiler to emit warnings. I'd like to ignore those warnings for a particular set of files. My first thought was that I might be able to set up an ItemGroup to set per-file properties for the compiler, something semantically like this:

<ItemGroup>
    <Files Include="Migrations/**/*.cs">
        <Properties>
            <DisabledWarnings>CS12345;CS4321</DisabledWarnings>
        </Properties>
    </Files>
</ItemGroup>

I recognize that this isn't valid MSBuild syntax, but it expresses the essence of what I'd like to do.

This question seems somewhat related: Using Item functions on metadata values

Is there a way to do this?

like image 329
Ben Collins Avatar asked Dec 20 '25 11:12

Ben Collins


1 Answers

You can't use MSBuild to disable compiler warnings per file. Instead, you need to use #pragma statements in the code.

For example, lets say you wan to disable warnings about usage of obsolete APIs (build warning CS0618). You can surround the code like this to suppress the warning.

#pragma warning disable CS0618

public void Method()
{
   new Class1().CallSomeObsoleteMessage();
}

#pragma warning restore CS0618

Alternatively, you can disable warnings for an entire project.

<PropertyGroup>
  <NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
like image 120
natemcmaster Avatar answered Dec 23 '25 12:12

natemcmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!