如何把某一对象x的全部类成员名称都打印出来。能递归打印更好。

zcdg909 2009-06-09 11:28:09
如何把某一对象x的全部类成员名称都打印出来。能递归打印更好。
...全文
69 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zenowolf 2009-06-09
  • 打赏
  • 举报
回复

using system;
using system.reflection;
using system.reflection.emit ;

public class testreflection {
private string file = @"testreflection.exe";

static void main(string[] args) {
testreflection test = new testreflection();
test.displaymodules();
test.displaytypes();
test.displaymethods();
test.invokestaticmethod();
test.invokemethod();
}
//显示任何模块名
public void displaymodules() {
console.writeline("display modules ...");
assembly assembly = assembly.loadfrom(file);
module[] modules = assembly.getmodules();
foreach( module module in modules ) {
console.writeline("module name:" + module.name );
}
}
//显示任何类型名
public void displaytypes() {
console.writeline("display types ...");
assembly assembly = assembly.loadfrom(file);
type[] types = assembly.gettypes();
foreach( type type in types ) {
console.writeline("type name:" + type.fullname );
}
}
//先是个类型中的任何方法名
public void displaymethods() {
console.writeline("display methods in testreflection type ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testreflection");
methodinfo[] methods = type.getmethods();
foreach(methodinfo method in methods) {
console.writeline("method name:" + method.name);
}
}
//调用一个类中的静态方法
public void invokestaticmethod() {
console.writeline("invoke static method ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testsubclass");
type.invokemember ("sayhello", bindingflags.invokemethod,null,null,new object[]{});
}
//调用一个类中的非静态方法
public void invokemethod() {
console.writeline("invoke method ...");
assembly assembly = assembly.loadfrom(file);
type type = assembly.gettype("testsubclass");
object obj = activator.createinstance(type);
testclass tc = (testclass)obj ;
type.invokemember ("test1", bindingflags.invokemethod,null,tc,new object[]{});
type.invokemember ("test2", bindingflags.invokemethod,null,tc,new object[]{});
}
}
public interface testclass {
void test1();
void test2();
}
public class testsubclass : testclass{
public void test1() {
console.writeline("this is testsubclass.test1");
}
public void test2() {
console.writeline("this is testsubclass.test2");
}
public static void sayhello() {
console.writeline("this is testsubclass.sayhello");
}
}

wuyq11 2009-06-09
  • 打赏
  • 举报
回复
Assembly assembly=Assembly.LoadFrom(fileName);
Type[] types=assembly.GetTypes();
for(int i=0;i<types.GetLength(0);++i)
{

}


Assembly assembly = Assembly.LoadFrom(toolStripTextBox1.Text);
foreach (System.Type t in assembly.GetTypes())
{
Console.WriteLine(t.Name);
}
参考
feiyun0112 2009-06-09
  • 打赏
  • 举报
回复
Class1 c = new Class1();
Type t = c.GetType();
foreach (MemberInfo info in t.GetMembers())
{
Console.WriteLine(info.Name);
}

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/
mabaolin 2009-06-09
  • 打赏
  • 举报
回复
查询反射
缥缈大木头 2009-06-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 raineo 的回复:]
C# code
using system;
using system.reflection;
using system.reflection.emit ;

public class testreflection {
private string file = @"testreflection.exe";

static void main(string[] args) {
testreflection test = new testreflection();
test.displaymodules();
test.displaytypes();
test.displaymethods();
test.invokestaticmethod();
test.invokemethod();
}
//显示任何模块名

[/Quote]

这个很彪悍啊,支持

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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