Материал из .Net Framework эксперт
AttributeUsage(AttributeTargets)
using System;
[AttributeUsage(AttributeTargets.Class)]
public class RandomSupplier : Attribute
{
public RandomSupplier()
{
}
}
[AttributeUsage(AttributeTargets.Method )]
public class RandomMethod : Attribute
{
public RandomMethod()
{
}
}
[RandomSupplier]
public class OriginalRandom
{
[RandomMethod]
public int GetRandom()
{
return 5;
}
}
using System;
[RandomSupplier]
public class NewRandom
{
[RandomMethod]
public int ImprovedRandom()
{
Random r = new Random();
return r.Next(1, 100);
}
}
public class AnotherClass
{
public int NotRandom()
{
return 1;
}
}
using System;
using System.Reflection;
class MainClass
{
public static void Main(string[] args)
{
RandomSupplier rs;
RandomMethod rm;
foreach(string s in args)
{
Assembly a = Assembly.LoadFrom(s);
foreach(Type t in a.GetTypes())
{
rs = (RandomSupplier) Attribute.GetCustomAttribute(
t, typeof(RandomSupplier));
if(rs != null)
{
Console.WriteLine("Found RandomSupplier class {0} in {1}",
t, s);
foreach(MethodInfo m in t.GetMethods())
{
rm = (RandomMethod) Attribute.GetCustomAttribute(
m, typeof(RandomMethod));
if(rm != null)
{
Console.WriteLine("Found RandomMethod method {0}"
, m.Name );
}
}
}
}
}
}
}