Ben Collins
Ben Collins

Reputation: 20686

Set MSBuild properties on Build target input items based on metadata

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?

Upvotes: 3

Views: 398

Answers (1)

natemcmaster
natemcmaster

Reputation: 26773

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>

Upvotes: 1

Related Questions