反射的任务

None.gif //  Programming C# by O'Reilly & Associates,Inc
None.gif
//  
None.gif
//
None.gif
// ----------------------------------------------------------------------------------- 
None.gif
//  反射常用于以下四个任务
None.gif
//   (1) 浏览元数据           
None.gif
//   (2) 查询类型 :利用反射查看配件的类型,方法,性质,事件,以及和改类型相关的一切信息
None.gif
//   (3) 与方法和性质迟绑定 (动态调用)
None.gif
//   (4) 在运行时创建类型   (反射发送)
None.gif
// -----------------------------------------------------------------------------------
None.gif
using  System;
None.gif
using  System.Reflection;
None.gif
using  System.Collections;
None.gif
namespace  Programing_CSharp_Reflection
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
//测试类
InBlock.gif
    public class App
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MyMath mm 
= new MyMath();
InBlock.gif            
int result = mm.Function_Sum(10,15);
InBlock.gif            Console.WriteLine(
"Result is {0}",result);
InBlock.gif            Console.WriteLine();
InBlock.gif            
// (1)浏览元数据
InBlock.gif
            System.Reflection.MemberInfo mbrInfo = typeof(MyMath);
InBlock.gif            
object[] attributes = mbrInfo.GetCustomAttributes(typeof(BugFixAttribute),false);
InBlock.gif            
foreach(object attribute in attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BugFixAttribute bfa 
= (BugFixAttribute) attribute;
InBlock.gif                Console.WriteLine(
" BugID  :{0}",bfa.BugID);
InBlock.gif                Console.WriteLine(
"Progammer:{0}",bfa.Programmer);
InBlock.gif                Console.WriteLine(
"Date     :{0}",bfa.Date);
InBlock.gif                Console.WriteLine(
"Comment  :{0}",bfa.Comment);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// (2)查询类型
InBlock.gif
            Assembly asb = Assembly.Load("Mscorlib.dll");
InBlock.gif            Type[] types 
= asb.GetTypes();
InBlock.gif            
foreach(Type t in types)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Type is  {0}",t);
InBlock.gif                MemberInfo[] mbrInfoArray 
= t.GetMembers();
InBlock.gif                
//也可以查询一个类型的成员,甚至可以查找特定的成员,例如只查找"Get"打头的方法
InBlock.gif                
// MemberInfo[] mbrInfoArray = t.FindMembers(MemberTypes.Method,BindingFlags.Default,Type.FilterName,"Get*")
InBlock.gif
                foreach(MemberInfo mbr in mbrInfoArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
"{0} IS A {1}",mbr,mbr.MemberType);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine(types.Length);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif   
// 自定义的属性类
InBlock.gif
   public class BugFixAttribute : System.Attribute
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif       
private int bugID;
InBlock.gif       
private string programmer;
InBlock.gif       
private string date;
InBlock.gif       
private string comment;
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public int BugID dot.gif{getdot.gif{return bugID;}setdot.gif{bugID=value;}}
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public string Programmer dot.gif{getdot.gif{return programmer;}setdot.gif{programmer=value;}}
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public string Date dot.gif{getdot.gif{return date;}setdot.gif{date=value;}}
ExpandedSubBlockStart.gifContractedSubBlock.gif       
public string Comment dot.gif{getdot.gif{return comment;} setdot.gif{comment=value;}} 
InBlock.gif       
//构造函数
InBlock.gif
       public BugFixAttribute(int bugID,string programmer,string date)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           
this.bugID = bugID;
InBlock.gif           
this.programmer = programmer;
InBlock.gif           
this.date = date;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
//将属性应用到一个类上
InBlock.gif
   [BugFixAttribute(100,"zh-bin@163.com","2004/07/31",Comment="添加加法运算")]
InBlock.gif   
public class MyMath
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif       
public int Function_Sum(int i,int j)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           
return i+j;
ExpandedSubBlockEnd.gif       }

InBlock.gif       
public int Function_Sub(int i,int j)
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
InBlock.gif           
return i-j;
ExpandedSubBlockEnd.gif       }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xpoint/archive/2004/07/31/28921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值