是否有一种干净的方法来用泛型参数模拟类?假设我必须模拟一个类Foo<T>,我需要传递到一个方法,期望Foo<Bar>。我可以很容易地做到以下几点:
Foo mockFoo = mock(Foo.class);
when(mockFoo.getValue).thenReturn(new Bar());
假设getValue()返回泛型类型t,但当我后来将它传递给一个期望Foo<Bar>的方法时,它将有小猫。选角是唯一的方法吗?
是否有一种干净的方法来用泛型参数模拟类?假设我必须模拟一个类Foo<T>,我需要传递到一个方法,期望Foo<Bar>。我可以很容易地做到以下几点:
Foo mockFoo = mock(Foo.class);
when(mockFoo.getValue).thenReturn(new Bar());
假设getValue()返回泛型类型t,但当我后来将它传递给一个期望Foo<Bar>的方法时,它将有小猫。选角是唯一的方法吗?
根据MSDN中==操作符的文档,
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings. User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described above for both predefined and user-defined reference types.
那么为什么这个代码片段编译失败呢?
bool Compare<T>(T x, T y) { return x == y; }
我得到错误操作符'=='不能应用于类型为'T'和'T'的操作数。我想知道为什么,因为据我所知==操作符是预定义的所有类型?
编辑:谢谢大家。一开始我并没有注意到这个语句只是关于引用类型的。我还认为为所有值类型提供了逐位比较,现在我知道这是不正确的。
但是,如果我使用引用类型,==操作符是否会使用预定义的引用比较,或者如果类型定义了引用比较,它是否会使用操作符的重载版本?
编辑2:通过反复试验,我们了解到==操作符在使用不受限制的泛型类型时将使用预定义的引用比较。实际上,编译器将使用它可以为受限类型参数找到的最佳方法,但不会再寻找其他方法。例如,下面的代码将始终打印true,即使当Test。test<B>(new B(), new B())调用:
class A { public static bool operator==(A x, A y) { return true; } }
class B : A { public static bool operator==(B x, B y) { return false; } }
class Test { void test<T>(T a, T b) where T : A { Console.WriteLine(a == b); } }
为什么在同一个类中有以下两个方法是不合法的?
class Test{
void add(Set<Integer> ii){}
void add(Set<String> ss){}
}
我得到编译错误
方法add(Set)与类型Test中的另一个方法具有相同的擦除添加(Set)。
虽然我可以绕过它,但我想知道为什么javac不喜欢这样。
我可以看到,在许多情况下,这两个方法的逻辑非常相似,可以用一个方法来代替
public void add(Set<?> set){}
方法,但情况并非总是如此。
如果你想有两个接受这些参数的构造函数,这是非常烦人的,因为你不能只改变其中一个构造函数的名字。
我想有一个Class对象,但我想强制它所代表的任何类扩展类a并实现接口B。
我可以:
Class<? extends ClassA>
Or:
Class<? extends InterfaceB>
但我不能两者兼得。有办法做到这一点吗?
是否有一种通用的方法将T类型的单个项传递给期望IEnumerable<T>参数的方法?语言是c#,框架2.0版。
目前我正在使用一个帮助方法(它是。net 2.0,所以我有一大堆类似于LINQ的铸造/投影帮助方法),但这似乎很愚蠢:
public static class IEnumerableExt
{
// usage: IEnumerableExt.FromSingleItem(someObject);
public static IEnumerable<T> FromSingleItem<T>(T item)
{
yield return item;
}
}
当然,另一种方法是创建并填充一个List<T>或一个Array,并传递它而不是IEnumerable<T>。
[编辑]作为一个扩展方法,它可以命名为:
public static class IEnumerableExt
{
// usage: someObject.SingleItemAsEnumerable();
public static IEnumerable<T> SingleItemAsEnumerable<T>(this T item)
{
yield return item;
}
}
我是不是遗漏了什么?
[Edit2]我们发现someObject.Yield()(正如@Peter在下面的评论中建议的那样)是这个扩展方法的最佳名称,主要是为了简洁,所以如果有人想获取它,这里是它和XML注释:
public static class IEnumerableExt
{
/// <summary>
/// Wraps this object instance into an IEnumerable<T>
/// consisting of a single item.
/// </summary>
/// <typeparam name="T"> Type of the object. </typeparam>
/// <param name="item"> The instance that will be wrapped. </param>
/// <returns> An IEnumerable<T> consisting of a single item. </returns>
public static IEnumerable<T> Yield<T>(this T item)
{
yield return item;
}
}
决定不使用完全泛型的get方法的原因是什么 在java.util接口中。地图< K、V >。
为了澄清问题,该方法的签名为
V get(对象键)
而不是
V get(K键)
我想知道为什么(同样的事情为remove, containsKey, containsValue)。
有人能告诉我是否有一种方法可以用泛型将泛型类型参数T限制为:
Int16 Int32 Int64 UInt16 UInt32 UInt64
我知道哪里关键字,但不能找到只有这些类型的接口,
喜欢的东西:
static bool IntegerFunction<T>(T value) where T : INumeric
我试图在添加到列表时通过其构造函数创建一个T类型的新对象。
我得到一个编译错误:错误消息是:
'T':创建变量实例时不能提供参数
但是我的类确实有构造函数参数!我该怎么做呢?
public static string GetAllItems<T>(...) where T : new()
{
...
List<T> tabListItems = new List<T>();
foreach (ListItem listItem in listCollection)
{
tabListItems.Add(new T(listItem)); // error here.
}
...
}
在c# 3.0中,您可以使用以下语法创建匿名类
var o = new { Id = 1, Name = "Foo" };
有没有办法将这些匿名类添加到泛型列表中?
例子:
var o = new { Id = 1, Name = "Foo" };
var o1 = new { Id = 2, Name = "Bar" };
List<var> list = new List<var>();
list.Add(o);
list.Add(o1);
另一个例子:
List<var> list = new List<var>();
while (....)
{
....
list.Add(new {Id = x, Name = y});
....
}
可能的重复: 泛型方法和多重约束
我需要一个具有两个类型约束的泛型函数,每个约束都继承自不同的基类。我知道怎么用一种方法:
void foo<T>() where T : BaseClass
然而,我不知道如何做到这两种类型:
void foo<TOne, TTwo>() where TOne : BaseOne // and TTwo : BaseTwo ???
怎么做呢?(使用。net 2)