我需要使用dll
加载reflection
,并且必须获取namespace
,class
,methods
及其arguments
dll
。此外,我需要在日志文件中写入这些信息。
我使用了以下代码。但是我只得到了写入日志文件的课程。
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~copyassembly.class~Mymethod(String,Type)
任何建议都会有很大帮助。
答案 0 :(得分:3)
对于公共方法
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);
}
}
答案 1 :(得分:3)
您可以编写与此类似的代码
var temp = token.Root.SelectToken("AccessoryList.Item").ToString();
将写下这些细节
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);
}
}
在这里,您可以[1]根据需要更改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)
[2]根据需要更改日志字符串格式(details / methodDetails)[3]格式集合/泛型类型
修改强>
如果要在没有返回类型的情况下显示,只需将其格式化为
BindingFlags
答案 2 :(得分:1)
使用一种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〜等于(对象)
ConsoleApplication1〜MyNamespace.Program〜GetHashCode()方法
ConsoleApplication1〜MyNamespace.Program〜的GetType()
ConsoleApplication1〜MyNamespace.Program〜主(字符串[])
ConsoleApplication1〜MyNamespace.Program〜的ToString()
然后您可以将其保存在文件
中File.AppendAllLines("Assembly Details.log", logItems);