Selectively Suppress Custom Obsolete Warnings

Selectively suppress custom Obsolete warnings

Use #pragma warning disable:

using System;

class Test
{
[Obsolete("Message")]
static void Foo(string x)
{
}

static void Main(string[] args)
{
#pragma warning disable 0618
// This one is okay
Foo("Good");
#pragma warning restore 0618

// This call is bad
Foo("Bad");
}
}

Restore the warning afterwards so that you won't miss "bad" calls.

How to ignore compiler warning when using Obsolete attribute on a class used with a KnownType attribute

Use this to disable the corresponding warnings just before the offending line:

#pragma warning disable 612, 618

And reenable the warnings after it:

#pragma warning restore 612, 618

Curiously enough, there're 2 warnings related to this: CS0612 and CS0618 - one is for [Obsolete] and the other for [Obsolete("Message")]. Go figure...

Disabling OBSOLETE error in C#

Following works for me:

#pragma warning disable 612,618
request.CommandLineArguments = arguments;
#pragma warning restore 612,618

notice no leading 0 in the numbers

EDIT:
Okay, your assembly has the "true" argument in the ObsoleteAttribute constructor. This means you can't use the property and not get an error.

If you can't re-write your code to avoid using this property, you'll have to invoke the property setter via reflection, for example:

request.GetType().GetProperty("Number").SetValue(request, arguments, null);

and getting is similar:

(string)request.GetType().GetProperty("CommandLineArguments").GetValue(request, null);

Can I make an attribute suppress warnings?

You can assign a default value, like null. That will make the warning disappear. Your assignment and also inspector assignments will overwrite the default value later on.

[Assign(AssignmentTime.Awake)]
private Rigidbody _rigidBody = null;

Prevent ObsoleteAttribute warning bubbling up call stack

Can I get round this so that I only suppress the warning in the method where the assembly is called?

Yes - by using the normal way of suppressing warnings in C# - using #pragma warning disable.

Here's an example:

using System;

public class Test
{
static void Main()
{
#pragma warning disable 0618
ObsoleteMethod();
#pragma warning restore 0618
}

[Obsolete("Don't call me")]
static void ObsoleteMethod()
{
}
}

Compiler Warning (level 2) CS0618 how to fix this

The docs for the BindableProperty.Create method show that the generic version of this method has been deprecated since version 2.1.0 meaning you shouldn't be using it. Instead you should use the non-generic overload, so your code should look like this:

BindableProperty.Create("Uri", typeof(string), typeof(CustomWebView), default(string));

How to disable compiler warnings in only generated code without editing file(s)

You said that you consider using a script to update the files to add #pragma a hack, but I can't think of another solution.

I think that you can do this easily with a MSBuild Task by adding something like this to your .csproj file:

<Target Name="DisableWarnings" BeforeTargets="CoreCompile">
<ItemGroup>
<AutoGeneratedFiles Include="**/*.Designer.cs" />
</ItemGroup>
<WriteLinesToFile File="%(AutoGeneratedFiles.FullPath)"
Condition="!$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath)).StartsWith("#pragma warning"))"
Lines="$([System.String]::Concat("#pragma warning disable 1591",$([System.IO.File]::ReadAllText(%(AutoGeneratedFiles.FullPath))),"#pragma warning restore 1591"))"
Overwrite="true"
Encoding="Unicode" />
</Target>

Globally suppress c# compiler warnings

Use the C# commandline option /nowarn http://msdn.microsoft.com/en-us/library/7f28x9z3(VS.80).aspx

To do this within visual studio goto Project properties->Build->(Errors and warnings) Suppress Warnings and then specify a comma separated list of warnings which need to be suppressed.



Related Topics



Leave a reply



Submit