在我們的報告環境,我們有一個方法來獲取數據源,它看起來像這樣:反思獲取內部變量的結果值
protected override IEnumerable<ReportDataSource> GetDataSources(IEnumerable<ReportParameter> parameters)
{
return new List<ReportDataSource>
{
new ReportDataSource("DataSource1", GetDataSource1(parameters)),
new ReportDataSource("DataSource2", GetDataSource2(parameters))
};
}
從調用的方法爲不用ICollections。我的問題是,爲了文檔的目的,我需要知道這些集合的內部類型,最好不用調用該方法。我只需要他們正在做的叫聲,我通過打破了下來局部變量:
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>));
它甚至有可能獲得我需要在不調用此方法的信息?
如果你看看源代碼,GetDataSource1(parameters)會返回什麼?因爲如果它返回'ICollection',很容易提取'something' ...但是如果它返回一個'ICollection',幾乎不可能發現它正在做什麼。 –
xanatos
可悲只是一個ICollection,但我想變量類型有這些信息? –
只有該集合中的成員才能獲得此信息,因爲您可以在其中存儲任何東西。 – HimBromBeere