如何使用Reflection获取命名空间、类、方法及其参数
本文关键字:方法 参数 Reflection 获取 命名空间 何使用 | 更新日期: 2023-09-27 18:28:23
我需要使用reflection
加载dll
,并且必须为该dll
获取namespace
、class
、methods
及其arguments
。此外,我需要将这些信息写入日志文件中。
我使用了以下代码。但我只上了一节已经写入日志文件的课。
Assembly dll = Assembly.LoadFile(@"D:'Assemblies'Myapplication.dll");
foreach (Type type in dll.GetTypes())
{
var name = "Myapplication~" + type.FullName;
File.AppendAllText("Assembly Details.log", "Myapplication~" + type.FullName + "'r'n'r'n");
Console.WriteLine(type.FullName);
}
在日志文件中,需要按照以下方式写入信息。
Myapplication~namespace.class~method(arguments)
例如:
Myapplication~copypassembly.class~Mymethod(String,Type)
任何建议都将大有帮助。
您可以编写类似于此的代码
Assembly dll = Assembly.LoadFile(@"C:'YourDirectory'YourAssembly.dll");
foreach (Type type in dll.GetTypes())
{
var details = string.Format("Namespace : {0}, Type : {1}", type.Namespace, type.Name);
//write details to your log file here...
Console.WriteLine(details);
foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
{
var methodDetails = string.Format("{0} {1}({2})", method.ReturnParameter, method.Name,
string.Join(",", method.GetParameters().Select(p => p.ParameterType.Name)));
//write methodDetails to your log file here...
Console.WriteLine("'t" + methodDetails);
}
}
谁会写这些细节
Namespace : MyNamespace.Helpers, Type : Constants
System.String ToString()
Boolean Equals(Object)
Int32 GetHashCode()
System.Type GetType()
Namespace : MyNamespace.Helpers, Type : FileHelper
Void WriteToFile(String,String,String,String)
Void UpdateFile(String,String,Boolean)
在这里,您可以[1]根据需要更改BindingFlags
[2]根据需要更改日志字符串的格式(details/methodDetails)[3]格式集合/泛型类型
编辑
如果你想在没有返回类型的情况下显示,只需像一样格式化即可
var methodDetails = string.Format("{1}({2})",
method.Name,
string.Join(",", method.GetParameters().Select(p => p.ParameterType.Name)));
对于公共方法
MethodInfo[] myArrayMethodInfo = type.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
对于非公开方法
MethodInfo[] myArrayMethodInfo1 = type.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
然后使用提取信息
MethodInfo[] myArrayMethodInfo
// Display information for all methods.
for(int i=0;i<myArrayMethodInfo.Length;i++)
{
MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
Console.WriteLine("'nThe name of the method is {0}.",myMethodInfo.Name);
ParameterInfo[] pars = myMethodInfo.GetParameters();
foreach (ParameterInfo p in pars)
{
Console.WriteLine(p.ParameterType);
}
}
使用一种LINQ可以通过这种方式实现
var dll = Assembly.LoadFile(@"D:'Trabalho'Temp'ConsoleApplication1'bin'Debug'ConsoleApplication1.exe");
var logItems = dll.GetTypes()
.SelectMany(t => t.GetMethods(), Tuple.Create)
.OrderBy(t => t.Item1.Namespace)
.ThenBy(t => t.Item1.Name)
.ThenBy(t => t.Item2.Name)
.Select(t => $"{"ConsoleApplication1"}~{t.Item1.FullName}~{t.Item2.Name}({string.Join(", ", t.Item2.GetParameters().Select(p => p.ParameterType.Name))})");
Console.WriteLine(string.Join(Environment.NewLine, logItems));
然后这个输出
ConsoleApplication1~MyNamespace.Program~Equals(对象)
ConsoleApplication1~MyNamespace.Program~GetHashCode()
ConsoleApplication1~MyNamespace.Program~GetType()
ConsoleApplication1~MyNamespace.Program~Main(String[])
ConsoleApplication1~MyNamespace.Program~ToString()
然后你可以把它保存在文件中
File.AppendAllLines("Assembly Details.log", logItems);