編譯器錯誤 CS1618
更新:2007 年 11 月
錯誤訊息
無法以 'method' 建立委派,因為它有 Conditional 屬性
您不能以條件式方法建立委派,因為某些組建 (Build) 中可能沒有該方法。
下列範例會產生 CS1618:
// CS1618.cs
using System;
using System.Diagnostics;
delegate void del();
class MakeAnError {
public static void Main() {
del d = new del(ConditionalMethod); // CS1618
// Invalid because on builds where DEBUG is not set,
// there will be no "ConditionalMethod".
}
// To fix the error, remove the next line:
[Conditional("DEBUG")]
public static void ConditionalMethod()
{
Console.WriteLine("Do something only in debug");
}
}