[英]Reflection Get the internal variable result value
In our reporting environment we have a method to get the DataSources, which is looking like this: 在我们的报告环境中,我们有一种获取数据源的方法,如下所示:
protected override IEnumerable<ReportDataSource> GetDataSources(IEnumerable<ReportParameter> parameters)
{
return new List<ReportDataSource>
{
new ReportDataSource("DataSource1", GetDataSource1(parameters)),
new ReportDataSource("DataSource2", GetDataSource2(parameters))
};
}
The values from the methods called are just ICollections. 来自所调用方法的值只是ICollections。 My problem is, I need to know the internal Type of these collections for documentation-purposes, at best without having to invoke the method.
我的问题是,出于文档目的,我需要知道这些集合的内部类型,充其量不必调用该方法。 I just need the calls they're making, I broke this down to the local variable via:
我只需要他们正在拨打的电话,就可以通过以下方式将其分解为局部变量:
const string dataSourcesMethodName = "GetDataSources";
MethodInfo methodInfo = type.GetMethod(
dataSourcesMethodName,
BindingFlags.Instance | BindingFlags.NonPublic,
Type.DefaultBinder,
new[] { typeof(IEnumerable<ReportParameter>) },
null);
var methodBody = methodInfo.GetMethodBody();
var variable = methodBody.LocalVariables.First(f => f.LocalType == typeof(IEnumerable<ReportDataSource>));
Is it even possible to get the information I need without invoking this method? 甚至可以在不调用此方法的情况下获取我需要的信息?
Simply put, you can't without executing the method... some examples (see http://goo.gl/8QN19K ): 简而言之,您不能不执行该方法...一些示例(请参阅http://goo.gl/8QN19K ):
C#: C#:
public ICollection M1() {
ICollection col = new List<string>();
return col;
}
public ICollection M2() {
ArrayList col = new ArrayList();
col.Add("Hello");
return col;
}
IL code locals: IL代码本地人:
.locals init (
[0] class [mscorlib]System.Collections.ICollection,
[1] class [mscorlib]System.Collections.ICollection
)
and 和
.locals init (
[0] class [mscorlib]System.Collections.ArrayList,
[1] class [mscorlib]System.Collections.ICollection
)
Compiling in Release mode it is even worse, the locals could totally disappear... See for example http://goo.gl/yvWZHR 在发布模式下编译甚至更糟,本地人可能会完全消失...例如,请参见http://goo.gl/yvWZHR
In general those methods could use for example an ArrayList
, so an untyped collection (as in the M2
method). 通常,这些方法可以使用例如
ArrayList
,因此可以使用无类型的集合(如M2
方法中一样)。 Good luck finding the type of its elements without executing the method and parsing some elements. 幸运的是,无需执行该方法并解析某些元素即可找到其元素的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.