您的位置:首页 > 编程语言 > C#

C#反射取得类的字段与方法信息

2015-09-08 20:42 459 查看
using System;using System.Reflection;namespace TestReflect{ class BaseClass { public int MyFieldBase=1; public int getfieldBase() { return MyFieldBase; } } class DerivedClass : BaseClass { public int MyFieldDerived=2; public int getfieldBase(int i) { return MyFieldDerived; } } class Program { static void Main(string[] args) { Type tbc=typeof(DerivedClass); Console.WriteLine("类型名:{0}.", tbc.Name); Console.WriteLine("它有如下字段:"); FieldInfo[] fi = tbc.GetFields(); MethodInfo[] me = tbc.GetMethods(); foreach (var f in fi) { Console.WriteLine("字段类型{0},字段名{1}",f.FieldType,f.Name); } Console.WriteLine(); Console.WriteLine("它有如下方法:"); foreach (var f in me) { Console.WriteLine("返回值类型:{0},函数名:{1}",f.ReturnType ,f.Name); ParameterInfo[] paramsInfo = f.GetParameters(); foreach (var p in paramsInfo) { Console.WriteLine("参数类型:{0}参数名:{1}",p.ParameterType,p.Name); } Console.WriteLine(); } Console.ReadKey(); } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: