您的位置:首页 > 其它

.NET ObsoleteAttribute

2015-09-11 22:00 357 查看
对于有不同版本的程序代码,obsoleteattribute显得大有用武之地。如果开发了一个新的方法,并且可以确定不再希望使用某方法,那么就可以使用Obsoleteattribute来修饰该方法,编译代码后,按警告/错误提示信息逐一更正原来的代码。在一个大型工程中,有助于协调不同的程序员所采用的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpAttribute
{
    class Program
    {
        // Mark OldProperty As Obsolete.
        [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", 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 obsolete. Call CallNewMethod instead.", true)] 
        public static string CallOldMethod()
        {
            return "You have called CallOldMethod.";
        }

        public static string CallNewMethod()
        {
            return "You have called CallNewMethod.";
        }   
        static void Main(string[] args)
        {
            Console.WriteLine(OldProperty);
            Console.WriteLine(NewProperty);
            Console.WriteLine();
            //Console.WriteLine(CallOldMethod());
            Console.WriteLine(CallNewMethod());

            Console.ReadKey();
        }
    }
}
结果:

The old property value.
The new property value.

You have called CallNewMethod.
调用过时方法是无法通过编译的:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: