代码
[AttributeUsage(AttributeTargets.All,AllowMultiple= true,Inherited= false)]
public class ForeignAttribute:System.Attribute
{
private string _languageType;
private int _languageLevel;
private string _languageInfo;
//必须定义其构造函数,如果不定义 编译器提供无参默认构造函数
public ForeignAttribute()
{
}
public ForeignAttribute(string languageType, int languagelevel)
{
_languageType = languageType;
_languageLevel = languagelevel;
}
public string LanguageType
{
get { return _languageType == null ? string.Empty : _languageType; }
}
public int LanguageLevel
{
get { return _languageLevel; }
}
public string LanguageInfo
{
get { return _languageInfo; }
set { _languageInfo = value; }
}
public void CanSpeak()
{
Console.WriteLine("He can speak {0}.", _languageType == null ? "Chinese Only" : _languageType);
}
}
public class ForeignAttribute:System.Attribute
{
private string _languageType;
private int _languageLevel;
private string _languageInfo;
//必须定义其构造函数,如果不定义 编译器提供无参默认构造函数
public ForeignAttribute()
{
}
public ForeignAttribute(string languageType, int languagelevel)
{
_languageType = languageType;
_languageLevel = languagelevel;
}
public string LanguageType
{
get { return _languageType == null ? string.Empty : _languageType; }
}
public int LanguageLevel
{
get { return _languageLevel; }
}
public string LanguageInfo
{
get { return _languageInfo; }
set { _languageInfo = value; }
}
public void CanSpeak()
{
Console.WriteLine("He can speak {0}.", _languageType == null ? "Chinese Only" : _languageType);
}
}
代码
[ReflectorDemo.Foreign("English",8,LanguageInfo= "This is US English,but not England English")]
public class LanguageTeacher
{
public void SayHello()
{
Console.WriteLine("Hello,I'm a Foreign Language teacher,I can speak Foreign Language.");
}
}
//class Program
//{
// static void Main(string[] args)
// {
// Type type = typeof(LanguageTeacher);
// System.Reflection.MemberInfo memberinfo = type;
// ReflectorDemo.ForeignAttribute can = (ReflectorDemo.ForeignAttribute)Attribute.GetCustomAttribute(memberinfo,typeof(ReflectorDemo.ForeignAttribute));
// if (can != null)
// {
// Console.WriteLine("Class of LanguageTeacher Can Speak:" + can.LanguageType);
// Console.WriteLine("The English Level of LanguageTeacher is:" + can.LanguageLevel);
// Console.WriteLine("And," + can.LanguageInfo);
// }
// //反射创建LanguageTeacher 对象,执行它的SayHello方法
// Object obj = Activator.CreateInstance(type);
// System.Reflection.MethodInfo mi = type.GetMethod("SayHello");
// mi.Invoke(obj, null);
// }
//}
public class LanguageTeacher
{
public void SayHello()
{
Console.WriteLine("Hello,I'm a Foreign Language teacher,I can speak Foreign Language.");
}
}
//class Program
//{
// static void Main(string[] args)
// {
// Type type = typeof(LanguageTeacher);
// System.Reflection.MemberInfo memberinfo = type;
// ReflectorDemo.ForeignAttribute can = (ReflectorDemo.ForeignAttribute)Attribute.GetCustomAttribute(memberinfo,typeof(ReflectorDemo.ForeignAttribute));
// if (can != null)
// {
// Console.WriteLine("Class of LanguageTeacher Can Speak:" + can.LanguageType);
// Console.WriteLine("The English Level of LanguageTeacher is:" + can.LanguageLevel);
// Console.WriteLine("And," + can.LanguageInfo);
// }
// //反射创建LanguageTeacher 对象,执行它的SayHello方法
// Object obj = Activator.CreateInstance(type);
// System.Reflection.MethodInfo mi = type.GetMethod("SayHello");
// mi.Invoke(obj, null);
// }
//}
转自:http://www.cnblogs.com/cnliu/archive/2010/02/25/1673384.html