public class MyProperty
{
//定义字段
private string _name;
private int _age;
//定义属性,封装_name字段
public string Name
{
get { return (_name == null) ? string.Empty : _name; }
set { _name = value; }
}
//定义属性,封装_age字段
//加入范围控制
public int Age
{
get { return _age; }
set
{
if( value > 0 && value<150)
{
_age = value;
}
else
{
throw new Exception(" Not a real age");
}
}
}
}
public class MyTest
{
public static void Mian( string[] args)
{
MyProperty myProperty = new MyProperty();
//触发set访问器
myProperty.Name = " Anytao";
//触发get访问器
Console.WriteLine(myProperty.Name);
myProperty.Age = 66;
Console.WriteLine(myProperty.Age.ToString());
Console.ReadLine();
}
}
using System;
namespace Anytao.net
{
[assembly: MyAttribute(1)] //应用于程序集
[moduel: MyAttribute(2)] //应用于模块
pubic class Attribute_how2 do
{
//
}
}
[AttributeUsageAttribute(AttributeTarget.All),
AllowMultiple = true,
Inherited = true]
class MyNewAttribute: System.Attribute
{
//
}
enum Animal
{
Dog = 0x0001,
Cat = 0x0002,
Duck = 0x0004,
Chicken = 0x0008
}
using System;
using System.Runtime.InteropServices;
namespace Anytao.net
{
class MainClass
{
[DllImport(" User32.dll")]
public static extern int MessageBox( int hParent, string msg, string caption, int type);
static int Main()
{
return MessageBox(0, " How to use attribute in .NET", " Anytao_net", 0);
}
}
}
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Method,
Inherited = true)]
public class TestAttribute : System.Attribute
{
public TestAttribute( string message)
{
throw new Exception(" error:" + message);
}
public void RunTest()
{
Console.WriteLine(" TestAttribute here.");
}
}
应用目标元素 [Test(" Error Here.")]
[Test(" Error Here.")]
public void CannotRun()
{
//
}
public static void Main( string[] args)
{
Tester t = new Tester();
t.CannotRun();
Type tp = typeof(Tester);
TestAttribute myAtt = (TestAttribute)Attribute.GetCustomAttribute((MemberInfo)tp, typeof(TestAttribute));
myAtt.RunTest();
}
using System;
using System.Reflection; //应用反射技术获得特性信息
namespace Anytao.net
{
//定制特性也可以应用在其他定制特性上,
//应用AttributeUsage,来控制如何应用新定义的特性
[AttributeUsageAttribute(AttributeTargets.All, //可应用任何元素
AllowMultiple = true, //允许应用多次
Inherited = false)] //不继承到派生类
//特性也是一个类,
//必须继承自System.Attribute类,
//命名规范为:"类名"+Attribute。
public class MyselfAttribute : System.Attribute
{
//定义字段
private string _name;
private int _age;
private string _memo;
//必须定义其构造函数,如果不定义有编译器提供无参默认构造函数
public MyselfAttribute()
{
}
public MyselfAttribute( string name, int age)
{
_name = name;
_age = age;
}
//定义属性
//显然特性和属性不是一回事儿
public string Name
{
get { return _name == null ? string.Empty : _name; }
}
public int Age
{
get { return _age; }
}
public string Memo
{
get { return _memo; }
set { _memo = value; }
}
//定义方法
public void ShowName()
{
Console.WriteLine(" Hello, {0}", _name == null ? " world." : _name);
}
}
//应用自定义特性
//可以以Myself或者MyselfAttribute作为特性名
//可以给属性Memo赋值
[Myself(" Emma", 25, Memo = " Emma is my good girl.")]
public class Mytest
{
public void SayHello()
{
Console.WriteLine(" Hello, my.net world.");
}
}
public class Myrun
{
public static void Main( string[] args)
{
//如何以反射确定特性信息
Type tp = typeof(Mytest);
MemberInfo info = tp;
MyselfAttribute myAttribute =
(MyselfAttribute)Attribute.GetCustomAttribute(info, typeof(MyselfAttribute));
if (myAttribute != null)
{
//嘿嘿,在运行时查看注释内容,是不是很爽
Console.WriteLine(" Name: {0}", myAttribute.Name);
Console.WriteLine(" Age: {0}", myAttribute.Age);
Console.WriteLine(" Memo of {0} is {1}", myAttribute.Name, myAttribute.Memo);
myAttribute.ShowName();
}
//多点反射
object obj = Activator.CreateInstance( typeof(Mytest));
MethodInfo mi = tp.GetMethod(" SayHello");
mi.Invoke(obj, null);
Console.ReadLine();
}
}
}