如果您这样更改方法签名(向其添加通用类型参数):
@BeforeMethod
public void beforeMethod() throws Exception {
initTest(testName here,suiteName here,description here);
}
@Test(testName = "Test Name",suiteName = "Suite Name",description = "Description")
public void Test01() throws Exception {
//test code
}
它应该起作用,因为它将从您传递给方法的集合中推断出通用参数。但是,当然,此方法中的代码不应对类型T作任何假设(除非您使用 where 关键字指定通用类型约束)
,
根据@StasIvanov答案,您只是在签名上缺少通用类型参数:
private void LoadCollection<T>(ObservableCollection<T> observableCollection)
{
}
如果您的类具有公共接口或类,则可以将其扩展为使用特定的泛型类型,例如:
private void LoadCollection<T>(ObservableCollection<T> observableCollection) where T : IMarkDeleted
{
//you can then use common properties within the method
observableCollection.Where(x => !x.MarkedAsDeleted);
}
此外,如果您计划在类中包含其他泛型方法,则可能会从中受益,在这种情况下,您只需要在类级别包含一次泛型规范即可,而您的方法则不需要它:
public class ObservableCollectionHelper<T> where T : IEntity,IMarkDeleted
{
public static T NewItem()
{
return Activator.CreateInstance<T>();
}
private void LoadCollection(ObservableCollection<T> observableCollection)
{
}
private void DeleteCollection(ObservableCollection<T> observableCollection)
{
}
}
本文链接:https://www.f2er.com/3013199.html