简单的Attribute实现.
C# Attribute class
[AttributeUsage(AttributeTargets.Method)]
public class FastyouAttribute:Attribute
{
private string _name = string.Empty;
public FastyouAttribute(string name)
{
_name = name;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
应用这个类public class FastyouAttribute:Attribute
{
private string _name = string.Empty;
public FastyouAttribute(string name)
{
_name = name;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
}
public class AttributeClass
{
[Fastyou("what's your name")]
public void getName()
{
//string name = "name";
}
}
访问结果{
[Fastyou("what's your name")]
public void getName()
{
//string name = "name";
}
}
public void AttributeFastyou()
{
Type type = typeof(AttributeClass);
foreach (MethodInfo mothod in type.GetMethods())
{
foreach (Attribute at in mothod.GetCustomAttributes(true))
{
FastyouAttribute att = at as FastyouAttribute;
if (att != null)
{
string sss = att.Name;//"what's your name"
string ddd = mothod.Name;//getName
}
}
}
}
{
Type type = typeof(AttributeClass);
foreach (MethodInfo mothod in type.GetMethods())
{
foreach (Attribute at in mothod.GetCustomAttributes(true))
{
FastyouAttribute att = at as FastyouAttribute;
if (att != null)
{
string sss = att.Name;//"what's your name"
string ddd = mothod.Name;//getName
}
}
}
}