将反射类型的通用列表传递给反射类型中的方法
passing generic list of reflected type to method in reflected type
我在运行时使用第三方程序集。该程序集公开了一个接受结构的通用列表的方法。该结构在第三方程序集本身中定义。
<thirdpartassembly>
public struct stStruct
{
public string member1;
public decimal member2;
}
public class businessType
{
public string processItems(List<stStruct> processItems)
{
//process items, then return result
}
}
</thirdpartassembly>
给定在运行时使用反射创建的 [businessType] 实例,我试图将项目列表 [listofStructItems] 传递给 [processItems] 方法。
我如何define/create[listofStructItems]?
到目前为止我尝试了什么:
- 传递包含 stStruct 项的列表。
- 在我的代码中创建一个与 stStruct (cstStruct) 具有相同定义的结构,然后将一个 List 传递给 processItems 方法。 (无法将 x 列表转换为 y 列表)。
环境:
- 第三方程序集:.Net v.2
- 我的程序集:v.2 以上的任何内容
有什么想法吗?我也很感激解释为什么上面的选项 none 有效。
What I tried so far:
- Passing a List containing stStruct items.
没有代码很难说错在哪里。
- Creating a struct that has the same definition as stStruct (cstStruct) in my code, then passing a List to the processItems method. (cannot convert list of x to list of y).
这就是类型安全语言的工作原理,它是一种不同的类型。
解决这个问题的关键是Type.MakeGenericType
方法创建一个编译时类型参数未知的泛型类型。请记住,Assembly.GetType()
方法需要包含命名空间的类型名称。您可以内联其中一些临时变量,我创建它们只是为了更清楚地了解一般反射过程。
var businessTypeInstance = ...;
var processItemsMethod = businessTypeInstance.GetType().GetMethod("processItems");
var stStructType = businessTypeInstance.GetType().Assembly.GetType("stStruct");
var openListType = typeof(List<>);
var closedListType = openListType.MakeGenericType(stStructType);
var listOfStruct = Activator.CreateInstance(closedListType);
var result = processItemsMethod.Invoke(businessTypeInstance, new [] { listOfStruct });
只是为了拓宽思路,即使我不推荐这样做,您也可以从参数本身获得 List<stStruct>
类型:
var closedListType = processItemsMethod.GetParameters()[0].ParameterType;
我在运行时使用第三方程序集。该程序集公开了一个接受结构的通用列表的方法。该结构在第三方程序集本身中定义。
<thirdpartassembly>
public struct stStruct
{
public string member1;
public decimal member2;
}
public class businessType
{
public string processItems(List<stStruct> processItems)
{
//process items, then return result
}
}
</thirdpartassembly>
给定在运行时使用反射创建的 [businessType] 实例,我试图将项目列表 [listofStructItems] 传递给 [processItems] 方法。
我如何define/create[listofStructItems]?
到目前为止我尝试了什么:
- 传递包含 stStruct 项的列表。
- 在我的代码中创建一个与 stStruct (cstStruct) 具有相同定义的结构,然后将一个 List 传递给 processItems 方法。 (无法将 x 列表转换为 y 列表)。
环境:
- 第三方程序集:.Net v.2
- 我的程序集:v.2 以上的任何内容
有什么想法吗?我也很感激解释为什么上面的选项 none 有效。
What I tried so far:
- Passing a List containing stStruct items.
没有代码很难说错在哪里。
- Creating a struct that has the same definition as stStruct (cstStruct) in my code, then passing a List to the processItems method. (cannot convert list of x to list of y).
这就是类型安全语言的工作原理,它是一种不同的类型。
解决这个问题的关键是Type.MakeGenericType
方法创建一个编译时类型参数未知的泛型类型。请记住,Assembly.GetType()
方法需要包含命名空间的类型名称。您可以内联其中一些临时变量,我创建它们只是为了更清楚地了解一般反射过程。
var businessTypeInstance = ...;
var processItemsMethod = businessTypeInstance.GetType().GetMethod("processItems");
var stStructType = businessTypeInstance.GetType().Assembly.GetType("stStruct");
var openListType = typeof(List<>);
var closedListType = openListType.MakeGenericType(stStructType);
var listOfStruct = Activator.CreateInstance(closedListType);
var result = processItemsMethod.Invoke(businessTypeInstance, new [] { listOfStruct });
只是为了拓宽思路,即使我不推荐这样做,您也可以从参数本身获得
List<stStruct>
类型:
var closedListType = processItemsMethod.GetParameters()[0].ParameterType;