gpt4 book ai didi

c# - 为什么 type.getConstructor 将类型数组作为参数?

转载 作者:行者123 更新时间:2023-11-30 19:43:18 25 4
gpt4 key购买 nike

我目前正在尝试学习反射并看到了一些使用 GetConstructor 的示例,我想这是我需要了解的基本功能之一,以便能够开始使用反射。我认为我了解代码的作用以及我将如何使用它。

来自msdn的代码示例

http://msdn.microsoft.com/en-us/library/h93ya84h.aspx

types[0] = typeof(int);
// Get the constructor that takes an integer as a parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(types);

我对代码的唯一问题是,我不明白为什么我需要创建一个虚拟类型数组才能使用 GetConstructor 方法(除了“该方法是以这种方式构造的”)。在我看来,逻辑输入参数将是一个整数,其中包含我试图通过反射获取的构造函数的索引。如果存在原因,有人可以解释为什么使用类型数组作为参数吗?

最佳答案

构造函数按签名搜索,即按参数类型的有序列表搜索。这就是为什么您需要传递构造函数参数类型数组的原因。在我看来,这是非常合乎逻辑的。

关于c# - 为什么 type.getConstructor 将类型数组作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15482460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com