PDA

View Full Version : کاربرد صفت obsolete



Programmer 1
سه شنبه 10 بهمن 1391, 12:22 عصر
سلام ،

using System;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Sum(1,2).ToString());
Console.ReadLine();
}

[Obsolete("use methode b instedad",true)]
static private int Sum(int a, int b)
{
return (a + b);
}
}
}
صقت absolete در هنگام کامپایل اخطاری/خطایی (با توجه به False یا True) به کاربر نشون میده ، مثلا اینجا میگه از متد Sum استفاده نکنید ، به جاش از Add استفاده کنید ، خب چه دلیلی داره که متدی که نمی خواهیم استفاده بشه رو در برنامه قرار بدیم؟

RED-C0DE
سه شنبه 10 بهمن 1391, 14:01 عصر
بیشتر برای نگه داشتن signature ها و ساختارهای قبلی هست یا همون Compatibility.

فرض کنید شرکتی برنامه ای (فریمورکی) رو نوشته و در اختیار توسعه دهندگان مثل شما برای کارهاشون قرار داده. شما با متد Sum کار می کنید. 2 سال بعد نسخه جدیدی می دن بیرون ک در اون یک سری روالها عوض شده (ب دلایل مختلف ، ک یکیش می تونه عدم پیش بینی بعضی نیازها در آینده و ساختار نادرست تحلیل و پیاده سازی فریمورک در نسخه قبلی باشه). با حذف متود sum (ک در نسخه جدید دیگه جواب درستی نمی ده با توجه ب ساختار جدید) باعث می شن ک برنامه شما چون از این متد استفاده کرده در جاهای مختلف، ب خطا بخوره. شما مجبورید در این نسخه جدید فریمورک از متود b استفاده کنید و اینکار رو با attribute obsolete بهتون اعلام می کنن ، ک می تونه بسته ب اهمیت این تغییر ، یک compile time error یا یک warning باشه

RED-C0DE
سه شنبه 10 بهمن 1391, 14:07 عصر
از MSDN (http://msdn.microsoft.com/en-us/library/ee461502.aspx) :




The .NET Framework changes over time. Each new version adds new types and type members that provide new functionality. Existing types and their members also change over time. For example, some types become less important as the technology they support is replaced by a new technology, and some methods are superseded by newer methods that are either more convenient or more full-featured.

The .NET Framework and the common language runtime strive to support backward compatibility (allowing applications that were developed with one version of the .NET Framework to run on the next version of the .NET Framework). This makes it difficult to simply remove a type or a type member. Instead, the .NET Framework indicates that a type or a type member should no longer be used by marking it as obsolete or deprecated. Deprecating a type or a member involves marking it so that developers are aware it will go away and have time to respond to its removal. However, existing code that uses the type or member continues to run in the new version of the .NET Framework.

Programmer 1
سه شنبه 10 بهمن 1391, 17:05 عصر
بیشتر برای نگه داشتن signature ها و ساختارهای قبلی هست یا همون Compatibility.



The .NET Framework changes over time. Each new version adds new types and type members that provide new functionality. Existing types and their members also change over time. For example, some types become less important as the technology they support is replaced by a new technology, and some methods are superseded by newer methods that are either more convenient or more full-featured.

The .NET Framework and the common language runtime strive to support backward compatibility (allowing applications that were developed with one version of the .NET Framework to run on the next version of the .NET Framework). This makes it difficult to simply remove a type or a type member. Instead, the .NET Framework indicates that a type or a type member should no longer be used by marking it as obsolete or deprecated. Deprecating a type or a member involves marking it so that developers are aware it will go away and have time to respond to its removal. However, existing code that uses the type or member continues to run in the new version of the .NET Framework.

بسیار عالی ، ممنونم ،
در مورد صفات چند تا پیش فرض دارم یکی اینکه احساس میکنم و می بینیم که در اکثر برنامه هایی که می نویسیم از صفات خیلی کم استفاده شده ، حالا نمی دونم به خاطر کاربرد کمشه یا کاربرد سختشه یا ...؟ آموزش خوبی برای آشنایی با صفات سراغ دارید معرفی کنید؟(سایت Stackoverflow خوبه ولی یک منبع اطلاعاتی خوب و کارا می خوام) ، اگه تو همین سایت هم در این موردی چیزی نوشته شده لطفا آدرس تاپیک رو بدید ، خودم هر چی گشتم موضوع مناسبی نتونستم پیدا کنم .