amirjalili
چهارشنبه 11 اسفند 1389, 14:19 عصر
سلام..
من دقیقا اهمیت attribute ها رو نفهمیدم..
همه جا میگن موقع کامپایل پیغام یا خطا میده و در واقع اطلاعات توضیحی برای متد ها و کلاس ها است ..
به نظر شما در این کد زیر چه اتفاقی باید با توجه به تعریف Attribute بیافته؟
using System;
namespace RegKeyAttributeTestor
{
[AttributeUsage(AttributeTargets.Method|AttributeTa rgets.Struct,
AllowMultiple=false,Inherited=true)]
public class MyAttribute:Attribute
{
private string regKey="a12nf";
public MyAttribute(string regKey)
{
if(this.regKey==regKey)
{
Console.WriteLine("Permitted to use this App");
}
else
{
Console.WriteLine("Not registered to use this App");
}
}
}
//End Attribute class code
class useAttrib
{
[MyAttribute("hello")]
public static string SayHello(string str)
{
return str;
}
static void Main()
{
Console.WriteLine(SayHello("Hello to Sufyan"));
Console.ReadLine();
}
}
}
من میگم باید ارور بده اما هیچ اتفاقی نمیفته..
من دقیقا اهمیت attribute ها رو نفهمیدم..
همه جا میگن موقع کامپایل پیغام یا خطا میده و در واقع اطلاعات توضیحی برای متد ها و کلاس ها است ..
به نظر شما در این کد زیر چه اتفاقی باید با توجه به تعریف Attribute بیافته؟
using System;
namespace RegKeyAttributeTestor
{
[AttributeUsage(AttributeTargets.Method|AttributeTa rgets.Struct,
AllowMultiple=false,Inherited=true)]
public class MyAttribute:Attribute
{
private string regKey="a12nf";
public MyAttribute(string regKey)
{
if(this.regKey==regKey)
{
Console.WriteLine("Permitted to use this App");
}
else
{
Console.WriteLine("Not registered to use this App");
}
}
}
//End Attribute class code
class useAttrib
{
[MyAttribute("hello")]
public static string SayHello(string str)
{
return str;
}
static void Main()
{
Console.WriteLine(SayHello("Hello to Sufyan"));
Console.ReadLine();
}
}
}
من میگم باید ارور بده اما هیچ اتفاقی نمیفته..