!如何取得方法中所有调用的类!

kfdsaas 2008-07-07 09:15:35
如何在不运行某方法的情况下取得某个方法中所有直接调用(依赖)的类的信息。
如:
public class Root
{
public void test(Animal a)
{
Person p = new Person('kfdsaas');
a.Cry();
}
}

我想取得Type[]数组其中包括 Person,Animal。
Person,Animal中成员的类型可以不用取得
...全文
261 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
kfdsaas 2008-07-13
  • 打赏
  • 举报
回复
虽然没有找到比较好的解决方案,还是要谢谢大家的参与
kfdsaas 2008-07-10
  • 打赏
  • 举报
回复
目前看来似乎只有21楼的思路具有一定可行性
kfdsaas 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 35 楼 shoushii 的回复:]
微软动态加载了所有的动态链接库DLL到程序集缓冲池,所以可以着色。如果该程序集没有被加载,也不会着色的。
[/Quote]

但是具体又是如何取得方法中的类型的呢?
yagebu1983 2008-07-10
  • 打赏
  • 举报
回复
关注一下!!!
shoushii 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 kfdsaas 的回复:]
引用 35 楼 shoushii 的回复:
微软动态加载了所有的动态链接库DLL到程序集缓冲池,所以可以着色。如果该程序集没有被加载,也不会着色的。


但是具体又是如何取得方法中的类型的呢?
[/Quote]
没深入分析过,呵呵。等你有了解决方案希望能分享。
21楼的应该有可行性
HimeTale 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 kfdsaas 的回复:]
引用 6 楼 dolphin_sail 的回复:
反射对象实体, 解析字符串吧

如何分辨一个字符串是类名还是变量?还有类名的命名空间是哪个?
[/Quote]
如果只调了某个类的静态方法不算依赖关系的话,
声明过并且后面调用new了的就是。
wss1801 2008-07-09
  • 打赏
  • 举报
回复
jf
xhan2000 2008-07-09
  • 打赏
  • 举报
回复
有点意思
suyiming 2008-07-09
  • 打赏
  • 举报
回复
有想法,可惜我不会~
烈火焚身 2008-07-09
  • 打赏
  • 举报
回复
有想法,可惜我不会~
begonia_ref 2008-07-09
  • 打赏
  • 举报
回复

public Type[] Test1()
{
Type t = typeof(Root);
MethodInfo info = t.GetMethod("test");
Type[] types = new Type[info.GetParameters().Length + info.GetMethodBody().LocalVariables.Count+1];
int i = 0;
foreach (ParameterInfo para in info.GetParameters())//参数
{
//Response.Write(para.ParameterType+"<br/>");
types[i] = para.ParameterType;
i++;
}

foreach (LocalVariableInfo var in info.GetMethodBody().LocalVariables)//局部变量
{
//Response.Write(var.LocalType+"<br/>");
types[i] = var.LocalType;
i++;
}

//Response.Write(info.ReturnParameter.ParameterType);//返回类型
types[i] = info.ReturnParameter.ParameterType;
return types;
}
begonia_ref 2008-07-09
  • 打赏
  • 举报
回复

public void Test1()
{
Type t = typeof(Root);
MethodInfo info = t.GetMethod("test");
foreach (ParameterInfo para in info.GetParameters())//参数
{
Response.Write(para.ParameterType+"<br/>");
}

foreach (LocalVariableInfo var in info.GetMethodBody().LocalVariables)//局部变量
{
Response.Write(var.LocalType+"<br/>");
}

Response.Write(info.ReturnParameter.ParameterType);//返回类型

}
kfdsaas 2008-07-09
  • 打赏
  • 举报
回复
没有办法吗?
GengWH 2008-07-09
  • 打赏
  • 举报
回复
public void hello()
{
Type t = typeof(Root);
MethodInfo info = t.GetMethod("test");
foreach (ParameterInfo para in info.GetParameters())//参数
{
Response.Write(para.ParameterType+"<br/>");
}

foreach (LocalVariableInfo var in info.GetMethodBody().LocalVariables)//局部变量
{
Response.Write(var.LocalType+"<br/>");
}

Response.Write(info.ReturnParameter.ParameterType);//返回类型

}
shoushii 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kfdsaas 的回复:]
vs2005的语法着色是怎么做到的?
[/Quote]
微软动态加载了所有的动态链接库DLL到程序集缓冲池,所以可以着色。如果该程序集没有被加载,也不会着色的。

关于反射工具,应该是通过IL语言,同时也进行了程序集加载,生成反编译代码。
Catmaoo 2008-07-09
  • 打赏
  • 举报
回复
Assembly
fellowcheng 2008-07-09
  • 打赏
  • 举报
回复
直接分析IL代码吧
datahandler2 2008-07-09
  • 打赏
  • 举报
回复
我也做不到。俄~~~~好难的问题。有点钻牛角尖了。也许以后钻出来就是牛人了。期待~~~
guying999 2008-07-09
  • 打赏
  • 举报
回复

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)
{
if (i.Name.ToLower().IndexOf("load") > -1)
{
MessageBox.Show(i.Name);
}
}

}

}
}

kfdsaas 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 winner2050 的回复:]
肯定能作出来.VS 就已经实现了.
[/Quote]
winner2050 有什么好的建议吗?
加载更多回复(21)

61,821

社区成员

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

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

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

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