0

I was wondering if there is a way to apply date when marking a method as obsolete? What I mean about a date is that this date represents the "date" when this function should be removed for good.

And a NooB question, is there a way to display error/warning message in winform using MessageBox.show()?
Which means that when in this :

[ObsoleteAttribute("This class is obsolete; use class B instead")]

Is there any way to get to call the Message function of ObsoleteAttribute?

Code:

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();
        }
    }
}
4

1 回答 1

1

Putting a date on your obsolescence is not a good idea. There could always be stuff that delays things. Plus, you probably want to check for usage of the obsolete element before removing it. Maybe some people ignored your warning. Maybe everyone did. Do you really want to break all their code, just because a day on the calendar has arrived?

So, since this is not a good idea, it is not supported. Of course, you can write a date inside your obsolescence warning text.

And to answer your second question, this is how to access the obsolescence warning text at runtime so you could display it as a message box.

ObsoleteAttribute attribute = typeof(A).GetCustomAttribute<ObsoleteAttribute>();
if (attribute != null)
{
    MessageBox.Show(attribute.Message);
}

Do be advised though that this is really a misuse of the obsolescence concept. Obsolescence is meant as a programmer warning, not as an end user warning. The end user doesn't care about how obsolete or deprecated the running code is, he just wants his program to work. It is the programmers duty to make sure that this happens. Therefore, the programmer gets the warning message at compile time. The user should not be bothered with that.

于 2013-07-01T12:22:07.340 回答