2019年.net学习心得-范文汇编

上传人:杰猫 文档编号:118985526 上传时间:2020-01-03 格式:DOC 页数:9 大小:15.35KB
返回 下载 相关 举报
2019年.net学习心得-范文汇编_第1页
第1页 / 共9页
2019年.net学习心得-范文汇编_第2页
第2页 / 共9页
2019年.net学习心得-范文汇编_第3页
第3页 / 共9页
2019年.net学习心得-范文汇编_第4页
第4页 / 共9页
亲,该文档总共9页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《2019年.net学习心得-范文汇编》由会员分享,可在线阅读,更多相关《2019年.net学习心得-范文汇编(9页珍藏版)》请在金锄头文库上搜索。

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

2、null) public classperson(string strname) name = strname; private string name; private string sex; private int age; public string name get return name; set name = value; public string sex get return sex; set sex = value; public int age get return age; set age = value; public void sayhello() if (null=

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

4、consoleapplication4 public class program static void main(string args) console.writeline(列出程序集中的所有类型); assembly ass = assembly.loadfrom(classlib.dll); type mytype = ass.gettypes(); type classperson = null; foreach (type p in mytype) console.writeline(p.name); if (p.name=classperson) classperson = p;

5、 console.writeline(列出classpersonl类中的所有的方法); methodinfo md = classperson.getmethods(); foreach(methodinfo m in md) console.writeline(m.name); console.writeline(实例化classperson类,并调用sayhello方法); object obj = activator.createinstance(classperson); object objname=activator.createinstance(classperson,飞鹰);

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

7、person类,并调用sayhello方法hello worldhello,飞鹰2.using的作用(1)引入命名空间,如:using system。(2)using别名。格式:using 别名=包括详细命名空间信息的具体的类型 例如:在两个命名空间(namespace1,namespace2)里各有一个myclass类,这时可以这样引入命名空间,using aclass=namespace1.myclass;using bclass=namespace2.myclass;实例化时:aclass my1=new aclass;bclass my2=new bclass;(3)using定义范围即时释放资源,在范围结束时处理对象。例如:using(class1 cls1=new class1()在这个代码段结束时会触发cls1的dispose方法释放资源。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号