获取包含详细信息的程序集的构造函数
Get constructor(s) of an assembly with details
根据模拟 .Net 程序集,我试图获取具有参数名称和参数数据类型的程序集的构造函数。
我使用此代码:
SampleAssembly = Assembly.LoadFrom("");
ConstructorInfo[] constructor = SampleAssembly.GetTypes()[0].GetConstructors();
foreach (ConstructorInfo items in constructor)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
{
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}
但是似乎什么都没有itema
但是我在方法上实现了相同的场景并且有效! (我确定我的程序集包含超过 2 个具有不同参数的构造函数)。
那么有什么建议可以检索带参数的程序集的构造函数吗?!
编辑:我在主代码上使用了正确的路径。在:Assembly.LoadFrom("");
提前致谢。
对我有用。但是您忘记了指定程序集路径:
SampleAssembly = Assembly.LoadFrom("");
应该是这样的:
SampleAssembly = Assembly.LoadFrom("C:\Stuff\YourAssembly.dll");
编辑:
要回复您的评论,请设置断点并查看 GetTypes()[0]
包含的内容。即使您只显式创建 1 class,也可能有例如匿名 classes。你不应该假设你想反思的 class 确实是一个并且是唯一的。
如果你写这样的代码:
class Program
{
static void Main()
{
Type t = typeof(Program);
ConstructorInfo[] constructor = t.GetConstructors();
foreach (ConstructorInfo items in constructor)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}
public Program() {}
public Program(String s) {}
}
您将看到提取参数类型和名称的代码应该并且将会起作用,所以问题在于定位 class。尝试通过完全限定名称查找 class。
我认为你的问题是你只在索引 [0] 处使用 Type
的构造函数。
看看这是否有效:
List<ConstructorInfo> constructors = new List<ConstructorInfo>();
Type[] types = SampleAssembly.GetTypes();
foreach (Type type in types)
{
constructors.AddRange(type.GetConstructors());
}
foreach (ConstructorInfo items in constructors)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
{
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}
根据模拟 .Net 程序集,我试图获取具有参数名称和参数数据类型的程序集的构造函数。 我使用此代码:
SampleAssembly = Assembly.LoadFrom("");
ConstructorInfo[] constructor = SampleAssembly.GetTypes()[0].GetConstructors();
foreach (ConstructorInfo items in constructor)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
{
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}
但是似乎什么都没有itema
但是我在方法上实现了相同的场景并且有效! (我确定我的程序集包含超过 2 个具有不同参数的构造函数)。
那么有什么建议可以检索带参数的程序集的构造函数吗?!
编辑:我在主代码上使用了正确的路径。在:Assembly.LoadFrom("");
提前致谢。
对我有用。但是您忘记了指定程序集路径:
SampleAssembly = Assembly.LoadFrom("");
应该是这样的:
SampleAssembly = Assembly.LoadFrom("C:\Stuff\YourAssembly.dll");
编辑:
要回复您的评论,请设置断点并查看 GetTypes()[0]
包含的内容。即使您只显式创建 1 class,也可能有例如匿名 classes。你不应该假设你想反思的 class 确实是一个并且是唯一的。
如果你写这样的代码:
class Program
{
static void Main()
{
Type t = typeof(Program);
ConstructorInfo[] constructor = t.GetConstructors();
foreach (ConstructorInfo items in constructor)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}
public Program() {}
public Program(String s) {}
}
您将看到提取参数类型和名称的代码应该并且将会起作用,所以问题在于定位 class。尝试通过完全限定名称查找 class。
我认为你的问题是你只在索引 [0] 处使用 Type
的构造函数。
看看这是否有效:
List<ConstructorInfo> constructors = new List<ConstructorInfo>();
Type[] types = SampleAssembly.GetTypes();
foreach (Type type in types)
{
constructors.AddRange(type.GetConstructors());
}
foreach (ConstructorInfo items in constructors)
{
ParameterInfo[] Params = items.GetParameters();
foreach (ParameterInfo itema in Params)
{
System.Windows.Forms.MessageBox.Show(itema.ParameterType + " " + itema.Name);
}
}