手机
当前位置:查字典教程网 >编程开发 >C#教程 >如何利用反射构建元数据查看器
如何利用反射构建元数据查看器
摘要:原理比较简单,引入System.Reflection命名空间,利用反射查看某种Type下的方法,属性,字段和支持的接口等。复制代码代码如下:...

原理比较简单,引入System.Reflection命名空间,利用反射查看某种Type下的方法,属性,字段和支持的接口等。

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.IO;

using System.Data.SqlClient;

using System.Xml;

using System.Data;

using System.Reflection;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

while (true)

{

Console.WriteLine("Please input a type:");

string typeStr = Console.ReadLine();

if (typeStr == "exit" || typeStr == "quit")

break;

try

{

Type type = Type.GetType(typeStr);

ListFields(type);

ListMethods(type);

ListInterfaces(type);

}

catch (Exception ex)

{

Console.WriteLine("It is not a valid type!");

}

}

}

#region Methods

public static void ListFields(Type type)

{

Console.WriteLine("******** Fields: ********");

//foreach (FieldInfo item in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Default))

foreach (FieldInfo item in type.GetFields())

{

Console.WriteLine("->" + item.Name);

}

Console.WriteLine("");

}

public static void ListMethods(Type type)

{

Console.WriteLine("******** Methods: ********");

//foreach (var item in type.GetMethods(BindingFlags.Default | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic))

var methodInfo = type.GetMethods().Select(m => m.Name).Distinct();

foreach (var item in methodInfo)

{

Console.WriteLine("->" + item);

}

Console.WriteLine("");

}

public static void ListInterfaces(Type type)

{

Console.WriteLine("******** Interfaces: ********");

foreach (var item in type.GetInterfaces())

{

Console.WriteLine("->" + item.Name);

}

Console.WriteLine("");

}

public static void ListProperties(Type type)

{

Console.WriteLine("******** Properties: ********");

foreach (var item in type.GetProperties())

{

Console.WriteLine("->" + item.Name);

}

Console.WriteLine("");

}

#endregion

}

}

测试case 1:

复制代码 代码如下:

Please input a type:

System.Int32

******** Fields: ********

->MaxValue

->MinValue

******** Methods: ********

->CompareTo

->Equals

->GetHashCode

->ToString

->Parse

->TryParse

->GetTypeCode

->GetType

******** Interfaces: ********

->IComparable

->IFormattable

->IConvertible

->IComparable`1

->IEquatable`1

测试case 2:

复制代码 代码如下:

Please input a type:

System.Math

******** Fields: ********

->PI

->E

******** Methods: ********

->Acos

->Asin

->Atan

->Atan2

->Ceiling

->Cos

->Cosh

->Floor

->Sin

->Tan

->Sinh

->Tanh

->Round

->Truncate

->Sqrt

->Log

->Log10

->Exp

->Pow

->IEEERemainder

->Abs

->Max

->Min

->Sign

->BigMul

->DivRem

->ToString

->Equals

->GetHashCode

->GetType

******** Interfaces: ********

【如何利用反射构建元数据查看器】相关文章:

使用C#实现RTP数据包传输 参照RFC3550

如何在UpdatePanel中调用JS客户端脚本

C#中使用反射获取结构体实例及思路

C#短时间内产生大量不重复的随机数

基于反射解决类复制的实现方法

C# 得到某一天的起始和截止时间的代码

C# 如何在MVC3中取消备用控制器的选择

深入反射生成数组的详解

C#利用子线程刷新主线程分享教程

利用C#实现分布式数据库查询

精品推荐
分类导航