欢迎来到天天文库
浏览记录
ID:48289416
大小:125.48 KB
页数:6页
时间:2019-11-18
《net学习心得范文》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、net学习心得范文 1.反射:反射是.中的重要机制,通过反射可以在运行时获得.中每一个类型,包括类、结构、委托和枚举的成员,包括方法、属性、事件,以及构造函数等。有了反射,既可以对每一个类型了如指掌。 下面来演示一下反射的实例 (1)新建一个类库项目。在解决方案上单击右键选择添加“新建项目”,在弹出来的框中选择“类库”,在下面名字栏中输入classlib。然后删除class1类,新添加一个类“classperson”,添加如下代码: namespaceclasslib { publ
2、icclassclassperson { publicclassperson():this(null) { } publicclassperson(stringstrname) { name=strname; } privatestringname; privatestringsex; privateintage; publicstringname { get{returnname;} set{name=value;} }
3、 publicstringsex { get{returnsex;} set{sex=value;} } publicintage { get{returnage;} set{age=value;} } publicvoidsayhello() { if(null==name) console.writeline("helloworld"); else console.writeline("hello,"+name);
4、 } } } 添加完之后编译生成一下,就会在这个类库项目中的bindebug中有一个classlib.dll文件。然后添加一个控制台应用程序。引入system.reflaction的命名空间。添加的代码如下: usingsystem; usingsystem.collections.generic; usingsystem.linq; usingsystem.text; usingsystem.reflection;//添加反射的命名空间 namespacec
5、onsoleapplication4 { publicclassprogram { staticvoidmain(string[]args) { console.writeline("列出程序集中的所有类型"); assemblyass=assembly.loadfrom("classlib.dll"); type[]mytype=ass.gettypes(); typeclassperson=null; foreach(typepinmytype)
6、{ console.writeline(p.name); if(p.name=="classperson") { classperson=p; } } console.writeline("列出classpersonl类中的所有的方法"); methodinfo[]md=classperson.getmethods(); foreach(methodinfominmd) { console.writeline(m.name); } cons
7、ole.writeline("实例化classperson类,并调用sayhello方法"); objectobj=activator.createinstance(classperson); objectobjname=activator.createinstance(classperson,"飞鹰"); methodinfomysayhello=classperson.getmethod("sayhello"); mysayhello.invoke(obj,null);//无参数构造函数
8、 mysayhello.invoke(objname,null);//有参构造函数 console.readkey(); } } } 运行之后的结果是: 列出程序集中的所有类型 classperson 列出classpersonl类中的所有的方法 get_name set_name
此文档下载收益归作者所有