C# 如何运行时获得一个类的属性个数及其名字、类型?

flashlove2008 2009-03-24 04:17:51
如何运行时获得一个类的属性个数及其名字、类型
...全文
1243 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我姓区不姓区 2009-03-24
  • 打赏
  • 举报
回复
记得using System.Reflection;
我姓区不姓区 2009-03-24
  • 打赏
  • 举报
回复
参考:

static void Main(string[] args)
{
MyClass mc = new MyClass();
Type t = mc.GetType();
Console.WriteLine("字段:");
foreach (FieldInfo fi in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
Console.WriteLine("名称:" + fi.Name + ",类型:" + fi.FieldType.Name);
Console.WriteLine("属性:");
foreach(PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
Console.WriteLine("名称:" + pi.Name + ",类型:" + pi.PropertyType.Name);
Console.WriteLine("方法:");
foreach(MethodInfo mi in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
Console.WriteLine("名称:"+mi.Name+",返回类型:"+mi.ReturnType.Name);
}
class MyClass
{
private int i;
public int I;

private string s;
public string S
{
get { return s; }
set { s = value; }
}

public void Method()
{ }
}
满衣兄 2009-03-24
  • 打赏
  • 举报
回复
winform的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
MessageBox.Show(i.Name);
}

}

}
}



微软写的例子(这个例子在vs安装之后的目录里有):

//版权所有 (C) Microsoft Corporation。保留所有权利。

// AttributesTutorial.cs
// 本示例表明类属性和方法属性的用法。

using System;
using System.Reflection;
using System.Collections;

// IsTested 类是用户定义的自定义属性类。
// 它可以应用于任何声明,包括
// - 类型(结构、类、枚举、委托)
// - 成员(方法、字段、事、属性、索引器)
// 使用它时不带参数。
public class IsTestedAttribute : Attribute
{
public override string ToString()
{
return "Is Tested";
}
}

// AuthorAttribute 类是用户定义的属性类。
// 它只能应用于类和结构声明。
// 它采用一个未命名的字符串参数(作者的姓名)。
// 它有一个可选的命名参数 Version,其类型为 int。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class AuthorAttribute : Attribute
{
// 此构造函数为属性类指定未命名的参数。
public AuthorAttribute(string name)
{
this.name = name;
this.version = 0;
}

// 此属性为只读(它没有 set 访问器)
// 因此,不能将其用作此属性的命名参数。
public string Name
{
get
{
return name;
}
}

// 此属性可读写(它有 set 访问器)
// 因此,将此类用作属性类时,
// 可以将其用作命名参数。
public int Version
{
get
{
return version;
}
set
{
version = value;
}
}

public override string ToString()
{
string value = "Author : " + Name;
if (version != 0)
{
value += " Version : " + Version.ToString();
}
return value;
}

private string name;
private int version;
}

// 此处,将用户定义的自定义属性 AuthorAttribute 附加
// 到 Account 类。创建属性时,会将未命名的
// 字符串参数传递到 AuthorAttribute 类的构造函数。
[Author("Joe Programmer")]
class Account
{
// 将自定义属性 IsTestedAttribute 附加到此方法。
[IsTested]
public void AddOrder(Order orderToAdd)
{
orders.Add(orderToAdd);
}

private ArrayList orders = new ArrayList();
}

// 将自定义属性 AuthorAttribute 和 IsTestedAttribute 附加
// 到此类。
// 请注意 AuthorAttribute 的命名参数“Version”的用法。
[Author("Jane Programmer", Version = 2), IsTested()]
class Order
{
// 在此处添加资料...
}

class MainClass
{
private static bool IsMemberTested(MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is IsTestedAttribute)
{
return true;
}
}
return false;
}

private static void DumpAttributes(MemberInfo member)
{
Console.WriteLine("Attributes for : " + member.Name);
foreach (object attribute in member.GetCustomAttributes(true))
{
Console.WriteLine(attribute);
}
}

public static void Main()
{
// 显示 Account 类的属性
DumpAttributes(typeof(Account));

// 显示已测试成员的列表
foreach (MethodInfo method in (typeof(Account)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();

// 显示 Order 类的属性
DumpAttributes(typeof(Order));

// 显示 Order 类的方法的属性
foreach (MethodInfo method in (typeof(Order)).GetMethods())
{
if (IsMemberTested(method))
{
Console.WriteLine("Member {0} is tested!", method.Name);
}
else
{
Console.WriteLine("Member {0} is NOT tested!", method.Name);
}
}
Console.WriteLine();
}
}




更多看这里:http://topic.csdn.net/u/20081212/16/4f466cee-8fa2-4300-93ed-325d74a2ea40.html
dwlwm_cherry 2009-03-24
  • 打赏
  • 举报
回复
2l正解
满衣兄 2009-03-24
  • 打赏
  • 举报
回复
风骑士之怒 2009-03-24
  • 打赏
  • 举报
回复

Type t = typeof(System.Net.Sockets.Socket);
foreach (PropertyInfo pi in t.GetProperties())
{
//你继续
}

满衣兄 2009-03-24
  • 打赏
  • 举报
回复
反射

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧