如何利用反射构建元数据查看器
时间:2021-05-27 00:11:05
原理比较简单,引入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: ********
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#实现多线程局域网聊天系统
springboot接口参数校验JSR303的实现
![](https://img.aspxhome.com/file/2023/4/72374_0s.png)
为Xamarin.Forms的导航栏增加搜索功能
![](https://img.aspxhome.com/file/2023/9/76389_0s.jpg)
详解Spring Cloud负载均衡重要组件Ribbon中重要类的用法
MyBatis字段名和属性名不一致的解决方法
SpringBoot注解梳理(小结)
基于servlet实现统计网页访问次数
![](https://img.aspxhome.com/file/2023/7/82047_0s.jpg)
eclipse maven 插件的安装和配置详解
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)
Java聊天室之使用Socket实现通信功能
![](https://img.aspxhome.com/file/2023/7/69117_0s.png)
Java AtomicInteger类使用方法实例讲解
c#和net存取cookies操作示例
Springboot 在普通类型注入Service或mapper
Spring-Data-JPA整合MySQL和配置的方法
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
C#编程获取资源文件中图片的方法
Java8 Stream流多字段求和、汇聚的实例
java写卷积神经网络(CupCnn简介)
![](https://img.aspxhome.com/file/2023/7/81917_0s.png)
Spring MVC4.1服务器端推送实现过程解析
![](https://img.aspxhome.com/file/2023/9/69949_0s.jpg)
Spring Data JPA带条件分页查询实现原理
Java获取文件的路径及常见问题解决方案
![](https://img.aspxhome.com/file/2023/9/79939_0s.png)