如何在 API w/o 中显示未实现的方法抛出异常并防止编译
How to show non implemented methods in API w/o throwing an exception and prevent compilation
我正在编写一个核心 API,将由客户端开发人员使用。
有些方法还没有开发,但我需要它们在 API 中,以便开发人员可以在 intellisense 中看到它们。
除了我提供给开发人员的 API 文档之外,我需要他在开发过程中知道一个方法尚未实现但它存在。
有没有办法在不抛出 NotImplementedException 的情况下通知开发人员该方法尚未实现,如果他尝试使用该方法将无法编译?
例如:
public class API
{
public void PrintToPDF()
{
// do not throw NotImplementedException
}
}
public class Client
{
public void Print()
{
API api = new API();
api.PrintToPDF(); // shouldn't compiled but can be see in intellisense. It can show a tooltip that it is in being developed.
}
}
您可以使用过时的属性:
public class API
{
[Obsolete("This isn't yet implemented")]
public void PrintToPDF()
{
// do not throw NotImplementedException
}
}
编译时不会产生错误,但会产生警告:
1>Example.cs(31,17,31,33): warning CS0618: 'API.PrintToPDF()' is obsolete: 'This isn't yet implemented'
使用过时的属性,它可以根据您的定义生成警告或错误。
using System;
using System.Reflection;
public class Example
{
// Mark OldProperty As Obsolete.
[ObsoleteAttribute("This property is for future use", false)]
public static string OldProperty
{ get { return "The old property value."; } }
public static string NewProperty
{ get { return "The new property value."; } }
// Mark CallOldMethod As Obsolete.
[ObsoleteAttribute("This method is for future use", true)]
public static string CallOldMethod()
{
return "You have called CallOldMethod.";
}
public static string CallNewMethod()
{
return "You have called CallNewMethod.";
}
public static void Main()
{
Console.WriteLine(OldProperty);
Console.WriteLine();
Console.WriteLine(CallOldMethod());
}
}
// The attempt to compile this example produces output like the following output:
// Example.cs(31,25): error CS0619: 'Example.CallOldMethod()' is obsolete:
// 'This method is for future use'
// Example.cs(29,25): warning CS0618: 'Example.OldProperty' is obsolete:
// 'This property is for future use'
您也可以创建自己的属性。
[Obsolete("Reserved for future use", true)]
public class ReservedForFutureUse : System.Attribute
{
}
我正在编写一个核心 API,将由客户端开发人员使用。
有些方法还没有开发,但我需要它们在 API 中,以便开发人员可以在 intellisense 中看到它们。
除了我提供给开发人员的 API 文档之外,我需要他在开发过程中知道一个方法尚未实现但它存在。
有没有办法在不抛出 NotImplementedException 的情况下通知开发人员该方法尚未实现,如果他尝试使用该方法将无法编译?
例如:
public class API
{
public void PrintToPDF()
{
// do not throw NotImplementedException
}
}
public class Client
{
public void Print()
{
API api = new API();
api.PrintToPDF(); // shouldn't compiled but can be see in intellisense. It can show a tooltip that it is in being developed.
}
}
您可以使用过时的属性:
public class API
{
[Obsolete("This isn't yet implemented")]
public void PrintToPDF()
{
// do not throw NotImplementedException
}
}
编译时不会产生错误,但会产生警告:
1>Example.cs(31,17,31,33): warning CS0618: 'API.PrintToPDF()' is obsolete: 'This isn't yet implemented'
使用过时的属性,它可以根据您的定义生成警告或错误。
using System;
using System.Reflection;
public class Example
{
// Mark OldProperty As Obsolete.
[ObsoleteAttribute("This property is for future use", false)]
public static string OldProperty
{ get { return "The old property value."; } }
public static string NewProperty
{ get { return "The new property value."; } }
// Mark CallOldMethod As Obsolete.
[ObsoleteAttribute("This method is for future use", true)]
public static string CallOldMethod()
{
return "You have called CallOldMethod.";
}
public static string CallNewMethod()
{
return "You have called CallNewMethod.";
}
public static void Main()
{
Console.WriteLine(OldProperty);
Console.WriteLine();
Console.WriteLine(CallOldMethod());
}
}
// The attempt to compile this example produces output like the following output:
// Example.cs(31,25): error CS0619: 'Example.CallOldMethod()' is obsolete:
// 'This method is for future use'
// Example.cs(29,25): warning CS0618: 'Example.OldProperty' is obsolete:
// 'This property is for future use'
您也可以创建自己的属性。
[Obsolete("Reserved for future use", true)]
public class ReservedForFutureUse : System.Attribute
{
}