将 ICollection 作为参数传递
我已经被这个看起来简单的代码困了一个多小时了...... 我有几个类...和这样的方法:
abstract class ClassBase<SampleInterface> {//Some methods};
public class ClassAction1: ClassBase<MyInterface> {//Some methods};
public class ClassAction2: ClassBase<MyInterface> {//Some methods};
class SomeClass
{
public void AddClassRange(ICollection<ClassBase<MyInterface>> range)
{
foreach (ClassBase<MyInterface> ClassBase in range)
AddClass(ClassBase);
}
public void AddClass(ClassBase<MyInterface> behavior)
{
// Something
}
}
现在,我尝试在不同的类中使用这些代码:
var arg1 = new ClassAction1 {//Something};
var arg2 = new ClassAction2 {//Something};
//try1
sampleElement.AddClassRange(new [] { arg1 }); // works fine
//try2
sampleElement.AddClassRange(new [] { arg2 }); // works fine
我想组合 try1 和 try2:
// Something like this (try3)
sampleElement.AddClassRange(new [] { arg1, arg2 }); // Error
Error : No best type found for implicitly typed array
正如我所想,在 try1 和 try2 运行时正在决定 类型
关键字 new
基于传递给它的参数。但在 try3 中,参数的类型不同,运行时无法为 new
关键字决定最佳的 type
。
谢谢。如果需要更多信息,请告诉我。
I am stuck with this simple looking code for over an hour now...
I have few classes...and methods like this:
abstract class ClassBase<SampleInterface> {//Some methods};
public class ClassAction1: ClassBase<MyInterface> {//Some methods};
public class ClassAction2: ClassBase<MyInterface> {//Some methods};
class SomeClass
{
public void AddClassRange(ICollection<ClassBase<MyInterface>> range)
{
foreach (ClassBase<MyInterface> ClassBase in range)
AddClass(ClassBase);
}
public void AddClass(ClassBase<MyInterface> behavior)
{
// Something
}
}
Now, I am trying to use these codes in a different class:
var arg1 = new ClassAction1 {//Something};
var arg2 = new ClassAction2 {//Something};
//try1
sampleElement.AddClassRange(new [] { arg1 }); // works fine
//try2
sampleElement.AddClassRange(new [] { arg2 }); // works fine
I want to combine try1 and try2:
// Something like this (try3)
sampleElement.AddClassRange(new [] { arg1, arg2 }); // Error
Error : No best type found for implicitly typed array
As I think, In try1 and try2 runtime is deciding the type
for keyword new
based on the parameter passed to it. But in try3, parameters are of different types and runtime fails to decide for the best type
for the new
keyword.
Thanks. Let me know if need more info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当数组元素的类型与其中一个元素的编译时类型完全相同时,隐式类型数组才有效。换句话说,编译器将元素类型集视为候选类型集,然后尝试准确地找到所有其他类型都可以隐式转换为的类型之一。
在您的情况下,候选类型集是
ClassAction1
和ClassAction2
,这两个类型都不能隐式转换为另一个 - 这就是您收到编译器错误的原因。因此,您需要明确声明所需的元素类型 - 大概是ClassBase
:或者,您可以强制转换其中一个或两个元素:
Implicitly typed arrays only work when the type of the array element is the exact compile-time type of one of the elements. In other words, the compiler treats the set of element types as the set of candidate types, and then tries to find exactly one of those types to which all the other types can be implicitly converted.
In your case, the set of candidate types is
ClassAction1
andClassAction2
, neither of which is implicitly convertible to the other - which is why you're getting a compiler error. So you need to state the desired element type explicitly - presumablyClassBase<MyInterface>
:Alternatively, you can cast either or both of the elements:
就这样做:
事实上,在一个数组中不能有不同的对象类型。在您的情况下,您应该将它们转换为相同的对象类型,因为最后您的 SomeClass 中有一个
ClassBase
类型的对象,因此您应该将其转换为ClassBase< /代码>。在您的第一个示例中,可以隐式完成此转换,但在第二个示例中则不能。
just do:
In fact, in one array you cannot have different object types. You should cast them to the same object type in your case because at last you have an object of type
ClassBase<MyInterface>
in your SomeClass, So you should cast it toClassBase<MyInterface>
. In your first sample this cast can be done implicitly but in your second sample it cannot.