net学习心得范文.docx

上传人:李医生 文档编号:12583740 上传时间:2021-12-04 格式:DOCX 页数:7 大小:67.24KB
返回 下载 相关 举报
net学习心得范文.docx_第1页
第1页 / 共7页
net学习心得范文.docx_第2页
第2页 / 共7页
net学习心得范文.docx_第3页
第3页 / 共7页
net学习心得范文.docx_第4页
第4页 / 共7页
net学习心得范文.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《net学习心得范文.docx》由会员分享,可在线阅读,更多相关《net学习心得范文.docx(7页珍藏版)》请在三一文库上搜索。

1、net 学习心得范文1. 反射:反射是 . 中的重要机制,通过反射可以在运行时获得 .中每一个类型,包括类、结构、委托和枚举的成员, 包括方法、属性、事件,以及构造函数等。有了反射,既可以对每一个类型了如指掌。下面来演示一下反射的实例( 1)新建一个类库项目。在解决方案上单击右键选择添加“新建项目”,在弹出来的框中选择“类库”, 在下面名字栏中输入 classlib 。然后删除 class1 类,新添加一个类“classperson ”,添加如下代码:namespaceclasslibpublicclassclasspersonpublicclassperson():this(null)pub

2、licclassperson(stringstrname)name=strname;privatestringname;privatestringsex;privateintage;publicstringnamegetreturnname;setname=value;publicstringsexgetreturnsex;setsex=value;publicintagegetreturnage;setage=value;publicvoidsayhello()if(null=name)console.writeline("helloworld");elseconsole

3、.writeline("hello,"+name);添加完之后编译生成一下, 就会在这个类库项目中的bindebug中有一个 classlib.dll文件。然后添加一个控制台应用程序。引入system.reflaction的命名空间。添加的代码如下:usingsystem;usingsystem.collections.generic;usingsystem.linq;usingsystem.text;usingsystem.reflection;/ 添加反射的命名空间 namespaceconsoleapplication4publicclassprogramstatic

4、voidmain(stringargs)console.writeline("列出程序集中的所有类型");assemblyass=assembly.loadfrom("classlib.dll");typemytype=ass.gettypes();typeclassperson=null;foreach(typepinmytype)console.writeline(p.name);if(p.name="classperson")classperson=p;console.writeline("列出 classperson

5、l类中的所有的方法");methodinfomd=classperson.getmethods();foreach(methodinfominmd)console.writeline(m.name);console.writeline("实例化 classperson类,并调用 sayhello方法 ");objectobj=activator.createinstance(classperson);objectobjname=activator.createinstance(classperson,"飞鹰 ");methodinfomysay

6、hello=classperson.getmethod("sayhello");mysayhello.invoke(obj,null);/无参数构造函数mysayhello.invoke(objname,null);/有参构造函数console.readkey();运行之后的结果是:列出程序集中的所有类型classperson列出 classpersonl类中的所有的方法get_nameset_nameget_sexset_sexget_ageset_agesayhellotostringequalsgethashcodegettype实例化 classperson类,并调

7、用 sayhello方法helloworldhello,飞鹰2.using的作用( 1)引入命名空间,如: usingsystem 。( 2)using 别名。格式: using 别名 =包括详细命名空间信息的具体的类型例如:在两个命名空间(namespace1,namespace2)里各有一个myclass 类, 这时可以这样引入命名空间,usingaclass=namespace1.myclass;usingbclass=namespace2.myclass;实例化时:aclassmy1=newaclass;bclassmy2=newbclass;(3)using定义范围即时释放资源,在范围结束时处理对象。例如:using(class1cls1=newclass1()在这个代码段结束时会触发cls1 的 dispose 方法释放资源。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1