C# 获取类的名,构造方法信息,字段信息,方法信息
程序员文章站
2022-05-07 19:41:56
...
(1)使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例。
(2)使用Module了解包含模块的程序集以及模块中的类等,还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。
(3)使用ConstructorInfo了解构造函数的名称、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。使用Type的GetConstructors或GetConstructor方法来调用特定的构造函数。
(4)使用MethodInfo了解方法的名称、返回类型、参数、访问修饰符(如pulic 或private)和实现详细信息(如abstract或virtual)等。使用Type的GetMethods或GetMethod方法来调用特定的方法。
(5)使用FiedInfo了解字段的名称、访问修饰符(如public或private)和实现详细信息(如static)等,并获取或设置字段值。
(6)使用EventInfo了解事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等,添加或移除事件处理程序。
(7)使用PropertyInfo了解属性的名称、数据类型、声明类型、反射类型和只读或可写状态等,获取或设置属性值。
(8)使用ParameterInfo了解参数的名称、数据类型、是输入参数还是输出参数,以及参数在方法签名中的位置等。
页面设计
按钮方法
//获取类的信息
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
string Tablename = "";//类名
string[] name = null;//类的字段
object[] value = null;//值
string[] valueType = null;//值的类型
int count = 0;//字段数量
Class6 model = new Class6();
Class5.GetClassData<Class6>(model, ref Tablename, ref name, ref value, ref valueType, ref count);
richTextBox1.AppendText("类的名称:" + Tablename + "\t\n");
richTextBox1.AppendText("类的字段名字,值类型,值" + "\t\n");
if (name == null || value == null || valueType == null)
return;
for (int i = 0; i < count; i++)
{
richTextBox1.AppendText(name[i] + " , " + valueType[i] + " , " + value[i].ToString() + "\t\n");
}
}
//获取类的方法
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
string Tablename = "";//类名
string[] Methodname = null;//方法名
string[] Methoddata = null;//方法参数类型,参数名
Class5.GetClassData<String>(ref Tablename, ref Methodname, ref Methoddata);
richTextBox1.AppendText("类的名称:" + Tablename + "\t\n");
if (Methodname == null || Methoddata == null)
return;
for (int i = 0; i < Methodname.Length; i++)
{
richTextBox1.AppendText("方法名称:" + Methodname[i] + Environment.NewLine);
richTextBox1.AppendText(" 参数:" + Methoddata[i] + Environment.NewLine);
}
}
//获取类的构造方法
private void button3_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
string Tablename = "";//类名
string Methodname = "";//构造方法名
string[] Methoddata = null;//构造方法参数
Class5.GetClassData<String>(ref Tablename, ref Methodname, ref Methoddata);
richTextBox1.AppendText("类的名称:" + Tablename + "\t\n");
richTextBox1.AppendText("构造方法名:" + Methodname + "\t\n");
if (Methoddata == null)
return;
for(int i=0;i<Methoddata.Length;i++)
{
richTextBox1.AppendText("构造方法参数:" + Methoddata[i] + "\t\n");
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Data
{
public class Class5
{
/// <summary>
/// 获取类的信息
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="model">类</param>
/// <param name="Tablename">表名</param>
/// <param name="name">字段</param>
/// <param name="value">值</param>
/// <param name="valueType">值类型</param>
/// <param name="count">字段个数</param>
public static void GetClassData<T>(T model, ref string Tablename, ref string[] name, ref object[] value, ref string[] valueType, ref int count)
{
Tablename = "";
if (model == null)
return;
Type type = model.GetType();
Tablename = type.Name;//类名
#region 获取类的信息
System.Reflection.PropertyInfo[] properties = model.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
count = properties.Length;//元素个数
if (count <= 0)
{
return;
}
name = new string[count];//字段名
value = new object[count];//值
valueType = new string[count];//值类型
int i = 0;
foreach (System.Reflection.PropertyInfo item in properties)
{
name[i] = item.Name;
value[i] = item.GetValue(model, null);
valueType[i] = item.PropertyType.Name.ToString();
i++;
}
#endregion
}
/// <summary>
/// 获取类的信息
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="Tablename">表名</param>
/// <param name="Methodname">方法名string[]</param>
/// <param name="Methoddata">方法参数string[] 参数1|参数2....</param>
public static void GetClassData<T>(ref string Tablename, ref string[] Methodname, ref string[] Methoddata)
{
Tablename = "";
Type type = typeof(T);
Tablename = type.Name;//类名
int count = type.GetMethods().Length;//方法数量
if (count <= 0)
return;
Methodname = new string[count];
Methoddata = new string[count];
int i = 0;
int datacount = 0;
foreach (MethodInfo meth in type.GetMethods())
{
Methodname[i] = meth.Name;
StringBuilder sb = new StringBuilder();
datacount = meth.GetParameters().Length;
if (datacount > 0)
{
foreach (ParameterInfo data in meth.GetParameters())
{
//参数类型 参数名
sb.Append(data.ParameterType+","+data.Name + "|");
}
string str=sb.ToString();
int strlen=str.Length;
Methoddata[i] = str.Substring(0, strlen - 1);
}
else
{
Methoddata[i] = "";
}
i++;
}
}
/// <summary>
/// 获取类的信息
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="Tablename">表名</param>
/// <param name="Methodname">构造方法名 构造方法名1|构造方法名2....</param>
/// <param name="Methoddata">构造方法参数 参数类型1,参数1|参数类型2,参数2....</param>
public static void GetClassData<T>(ref string Tablename, ref string Methodname, ref string[] Methoddata)
{
Tablename = "";
Type type = typeof(T);
Tablename = type.Name;
StringBuilder sb = new StringBuilder();
StringBuilder msb = new StringBuilder();
int i = 0;
int count = type.GetConstructors().Length;
if (count <= 0)
return;
Methoddata = new string[count];
int datacount = 0;
foreach(ConstructorInfo info in type.GetConstructors())
{
msb.Append(info.Name + "|");
datacount = info.GetParameters().Length;
if (datacount > 0)
{
foreach (ParameterInfo data in info.GetParameters())
{
sb.Append(data.ParameterType+","+data.Name + "|");
}
string str = sb.ToString();
int strlen = str.Length;
Methoddata[i] = str.Substring(0, strlen - 1);
}
else
{
Methoddata[i] = "";
}
i++;
}
string mstr = msb.ToString();
int mstrlen = mstr.Length;
Methodname = mstr.Substring(0, mstrlen - 1);
}
}
}
测试类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Data
{
public class Class6
{
/// <summary>
/// 用于类的获取
/// </summary>
public Class6()
{
str = Guid.NewGuid().ToString("N");
int10 = 10;
bools = false;
Time = DateTime.Now;
doubles = 1000.02;
bytes = 01;
decimals = 10.0M;
floats = 2.33F;
}
/// <summary>
/// String
/// </summary>
[DisplayName("String")]
public string str { get; set; }
/// <summary>
/// int
/// </summary>
[DisplayName("int")]
public int int10 { get; set; }
/// <summary>
/// bool
/// </summary>
[DisplayName("bool")]
public bool bools { get; set; }
/// <summary>
/// DateTime
/// </summary>
[DisplayName("DateTime")]
public DateTime Time { get; set; }
/// <summary>
/// Double
/// </summary>
[DisplayName("Double")]
public double doubles { get; set; }
/// <summary>
/// Byte
/// </summary>
[DisplayName("Byte")]
public byte bytes { get; set; }
/// <summary>
/// decimal
/// </summary>
[DisplayName("decimal")]
public decimal decimals { get; set; }
/// <summary>
/// float
/// </summary>
[DisplayName("float")]
public float floats { get; set; }
}
}