Cs0067 c# das ereignis delegatecommand.canexecutechanged wird nie verwendet.

In my current project I am using a library that is delivered to me. The API in this library provide an interface that is a bit big! The interface requires my class to implement two events as follows:


#region INotifyPropertyChanged Members
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

#endregion
#region INotifyPropertyChanging Members
public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

These two events have no meaning in my class and I just have to implement them for the sake of the interface.

As I start to compile my code I see the Warning CS0067 that indicates that my events are not used anywhere, which is true.

As usual, I don't want my code gets spoiled with warnings en I would like to sort out ALL MY WARNINGS. So I came across some options:


  • Ignoring this warnig throughout the project.

    This is too much, I don't want to avoid the benefit of getting this nice warning that just highlighted my problem. But if I would like to do so I need to put the number 0067 in the project properties page in Build tab onder the Supress warnings


  • The less wide solution would be to ignore this warning only in that file and I could do this by putting the code just before my declaration:#pragma warning disable 0067
    and then restore it after my declaration using
    #pragma warning restore 0067
  • The last option I just found from MSDN which seems a neat solution is to tell the compiler that I am deliberately not supporting this event and even if it would be called at runtime that would be a mistake.

    To do so I need to throw an exception as follows:

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged
    {
    add { throw new NotSupportedException(); }
    remove { }
    }
    public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging
    {
    add { throw new NotSupportedException(); }
    remove { }
    }

イベントが定義されているインターフェイスを実装するとき、実装側でイベントを使わないと警告CS0067が出る。
この警告はうっかりイベントを使い忘れていたときに重宝するんだけど、意図的にイベントを使わない場合もあったりする。
警告が出っぱなしでも一応問題はないんだけど、気持ち悪いので回避する方法を探してみた。

うん、似たようなことを考える人はいるもんだ。

C# Warning CS0067: The event 'event' is never used
http://blogs.msdn.com/b/trevor/archive/2008/08/14/c-warning-cs0067-the-event-event-is-never-used.aspx


interface INotice
{
event EventHandler Notice;
}

class Class1 : INotice
{
// これだと警告が出る
public event EventHandler Notice = null;
}

class Class2 : INotice
{
public event EventHandler Notice = null;

// イベントを使うメソッドを作っておけば警告は出ない
// (このメソッドは別に呼び出されなくてもいい)
private void OnNotice()
{
var handler = this.Notice;
if (null != handler)
{
handler(this, EventArgs.Empty);
}
}
}

class Class3 : INotice
{
// 何もしない処理を明示的に書けば警告は出ない
public event EventHandler Notice
{
add { }
remove { }
}
}

いずれにせよ、「このイベントは使われないよ!」ということをコメントなり何なりにきちんと残しておくべきだな。

スポンサーサイト




  1. 2012/04/13(金) 00:14:22|
  2. C#.NET
  3. | トラックバック:0
  4. | コメント:0

たまにコンパイラの警告レベルを落とさず、特定のコードでのみ警告を表示させないようにしたい時がある。

たとえば、以下のようなコードを示す。

[csharp]
public class NetworkOrganizer : IOrganizer
{
public event Action<int> GeometryChanged;
public void Organize()
{
// 省略
}
}
[/csharp]

GeometryChangedイベントはIOrganizerに定義されていて、実装する必要がある。が、たまたまこのクラスでは不要だったとしよう。こういうことは往々にしてあると思う。

「いやいや、このイベントが必要なクラスと不要なクラスがあるという事自体が、まずい設計だということだ。インタフェースとしてうまく抽出できてないということだよ」という人も居るかもしれないが、そんなにちゃんとした設計ができてるような環境でぜひとも仕事してみたいもんだ。

で、まあ、こういう時は以下の様なエラーが出る。

警告 CS0067: イベント 'Project.NetworkOrganizer.GeometryChanged' は使用されませんでした。

こういう時は、「このクラスでは不要なのでこのイベントを使いません。よって警告を抑制します」という旨のコメントを書いて警告を抑制したい。結論から言うと、以下のようにすれば警告を抑制できる。

[csharp]
public class NetworkOrganizer : IOrganizer
{
#pragma warning disable 0067
// 本クラスでは使用しない
public event Action<int> GeometryChanged;
#pragma warning restore 0067

public void Organize()
{
// 省略
}
}
[/csharp]

(追記:上記コードは初出時「#pragma restore 0067」と書いていましたが、誤りだそうです。ご指摘ありがとうございました。修正しました)

ここで、「0067」は警告に固有の番号であり、VSからだと「出力」ウィンドウに出てくる上記警告メッセージを参照すると分かる。上記では「CS0067」となっているが、「CS」は不要なようだ。

で、このコード、なんかすげー汚い。私は元来、こういう#pragmaとか、コンパイラに直接何かを命令して助けてもらうタイプの処理が、小手先のテクニックであるような気がしてすごく嫌いだ。なるべくなら使いたくないと思っている。加えて、この場合は2行も書かなくてはならず、更に汚く醜い。こういうコードは書きたくないが、こうする以外にやり方が分からなかった。

はじめは、SuppressMessage属性が使用できると思っていたのだけど、どうもこれはコード分析ツールや、FxCop、StyleCopツールから出力される警告は抑制できるようだが、コンパイラの警告は抑制してくれないようだ。

もっと簡潔な書き方は無いのだろうか。