我有一个包含不同类的C#.exe文件。
我需要获取可执行文件中类内的属性。
现在我这样做:
Assembly a = Assembly.LoadFile(servicePath);
foreach (Type t in a.GetTypes())
{
Console.WriteLine(t.Name);
}
我可以看到该类是在控制台中编写的,现在我怎样才能获得该类的属性by the way I don't and won't have that class referenced in my project
?
这是我的班级:
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class SyncServiceInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public SyncServiceInstaller()
{
// This call is required by the Designer.
InitializeComponent();
}
}
我需要在该类中获取此变量:serviceInstaller1
有任何线索吗?
答案 0 :(得分:0)
我首先加载程序集,然后过滤掉你正在查找的程序集,然后在生成的程序集上调用GetField("...")
。
您尝试检索的项目是字段而非属性。
FieldInfo field = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(n => n.GetType().Equals(typeof(SyncServiceInstaller)))?.GetType()
.GetField("serviceInstaller1", BindingFlags.Instance
| BindingFlags.Public
| BindingFlags.NonPublic);