黑马程序员技术交流社区
标题:
dynamic的使用
[打印本页]
作者:
♂张耕明
时间:
2012-11-10 23:15
标题:
dynamic的使用
本帖最后由 ♂张耕明 于 2012-11-10 23:22 编辑
dynamic:dynamic是FrameWork4.0的新特性。dynamic的出现让C#具有了弱语言类型的特性。编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性。编译器对dynamic进行了优化,比没有经过缓存的反射效率快了很多。
[AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = false)]//可应用任何元素、允许应用多次、不继承到派生类
class GetUserInfo : System.Attribute
{
public string Name { get; set; }
public int Age { get; set; }
public GetUserInfo(string name, int age)
{
this.Name = name;
this.Age = age;
}
public void Show()
{
MessageBox.Show(string.Format("姓名:{0},年龄:{1}", Name, Age));
}
}
[GetUserInfo("张耕明", 28)]
public class Demo { }
调用:
dynamic demo = Attribute.GetCustomAttribute(typeof(Demo), typeof(GetUserInfo));
demo.Show();//这是dynamic的亮点
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2