【发布时间】:2013-07-01 12:00:26
【问题描述】:
我想知道在将方法标记为过时时是否可以应用日期?我所说的日期是指这个日期代表应该永久删除这个功能的“日期”。
还有一个 NooB 问题,有没有办法使用 MessageBox.show() 在 winform 中显示错误/警告消息?
这意味着当在这个:
[ObsoleteAttribute("This class is obsolete; use class B instead")]
有什么方法可以调用ObsoleteAttribute的Message函数吗?
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[ObsoleteAttribute("This class is obsolete; use class B instead")]
class A
{
public void F() { }
}
class B
{
public void F() { }
}
private void button1_Click(object sender, EventArgs e)
{
A a = new A(); // Warning
a.F();
}
private void button2_Click(object sender, EventArgs e)
{
B b = new B();
b.F();
}
}
}
【问题讨论】:
-
...您能否提供一些背景信息,为什么您想要这些东西? Obsolete 属性的主要用途是在编译时引起警告/错误。
-
在消息中输入日期。并且不确定“调用 ObsoleteAttribute 的消息功能”是什么意思。你能详细说明一下吗?
-
您可以编写自己的属性,将其称为 NewObsoleteAttribute,例如抛出异常 ObsoleteException,捕获它(可以设为泛型)并显示消息
-
只是为了测试,没有更重要的,如果它只是为了编译,那没关系:)
标签: c# windows visual-studio-2010 computer-science