Özet - Attribute'lar
Özel attribute ve reflection'ı birleştirerek bir komut işleyici yazarsın.
Özet - Attribute'lar
Bu grupta attribute temellerini, özel attribute yazmayı, reflection ile okumayı, positional ve named parametreleri ve yerleşik attribute'ları öğrendin. Bir özel attribute yazıp reflection ile okuyarak, komut işleyici gibi davranışsal sistemler kurabilirsin.
Şimdi bunları birleştirerek bir komut işleyici yazacaksın: önce [Command] attribute'ını tanımlayıp bir metoda uygulayacaksın, sonra reflection ile attribute'ı okuyup metodu çağıracaksın.
Uygulama Command attribute ile metot bul
Aşağıdaki CommandAttribute sınıfını tanımla: [AttributeUsage(AttributeTargets.Method)] ile işaretle, Attribute'ten türet, Name özelliği ve string name parametreli oluşturucu ekle. Handler sınıfındaki Hello metoduna [Command("merhaba")] uygula. Main içinde Attribute.GetCustomAttribute(m, typeof(CommandAttribute)) as CommandAttribute ile her metodu gez ve attribute varsa Console.WriteLine(attr.Name) ile adını yazdır.
Örnek çözümü göster
[AttributeUsage(AttributeTargets.Method)] public class CommandAttribute : Attribute { public string Name { get; set; } public CommandAttribute(string name) { Name = name; } }. Hello'ya [Command("merhaba")] ekle. Main: foreach (var m in typeof(Handler).GetMethods()) { var attr = Attribute.GetCustomAttribute(m, typeof(CommandAttribute)) as CommandAttribute; if (attr != null) { Console.WriteLine(attr.Name); } }
Örnek çözüm:
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.Method)]
public class CommandAttribute : Attribute
{
public string Name { get; set; }
public CommandAttribute(string name) { Name = name; }
}
class Handler
{
[Command("merhaba")]
public void Hello() { }
}
class Program
{
static void Main()
{
foreach (var m in typeof(Handler).GetMethods())
{
var attr = Attribute.GetCustomAttribute(m, typeof(CommandAttribute)) as CommandAttribute;
if (attr != null)
{
Console.WriteLine(attr.Name);
}
}
}
}
Uygulama Command metodu Invoke ile çağır
Aşağıdaki iskelette Handler sınıfındaki Hello metoduna [Command("merhaba")] uygula. Main içinde Handler h = new Handler(); ile örnek üret. GetMethods ile gez; attr.Name == "merhaba" olduğunda m.Invoke(h, null) ile metodu çağır.
Örnek çözümü göster
foreach (var m in typeof(Handler).GetMethods()) { var attr = Attribute.GetCustomAttribute(m, typeof(CommandAttribute)) as CommandAttribute; if (attr != null && attr.Name == "merhaba") { m.Invoke(h, null); } }
Örnek çözüm:
using System;
using System.Reflection;
[AttributeUsage(AttributeTargets.Method)]
public class CommandAttribute : Attribute
{
public string Name { get; set; }
public CommandAttribute(string name) { Name = name; }
}
class Handler
{
[Command("merhaba")]
public void Hello() => Console.WriteLine("Merhaba dünya!");
}
class Program
{
static void Main()
{
Handler h = new Handler();
foreach (var m in typeof(Handler).GetMethods())
{
var attr = Attribute.GetCustomAttribute(m, typeof(CommandAttribute)) as CommandAttribute;
if (attr != null && attr.Name == "merhaba")
{
m.Invoke(h, null);
}
}
}
}
Kendini test et
-
Özel attribute hangi sınıftan türetilir?
- MemberInfo
- Attribute
- Type
- MethodInfo
Cevabı göster
Attribute — Doğru. Özel attribute Attribute sınıfından türetilir.
-
Öğeyi amacına eşle
- AttributeUsage
- Hedef kısıtı
- GetCustomAttribute
- Okur
- Obsolete
- Eski işaretler
- Conditional
- Çağrı koşulu