[英]C# how to create a generic type argument
I have existing test code which needs to be extended. 我有现有的测试代码,需要扩展。
I would like to keep this as generic as possible in order to reduce code duplication, here is a simplified snippet of the current code: 我想尽可能地保持通用,以减少代码重复,这是当前代码的简化片段:
public class FilterValueOne { }
[TestClass]
public class TestClass
{
[TestMethod]
public void FilterValueOne_TestMethod()
{
ManipulateData(BuildFilter());
}
private void ManipulateData(FilterValueOne filter)
{
// Do some stuff with filter
}
private FilterValueOne BuildFilter()
{
var filter = new FilterValueOne();
// Initialize filter data members here...
return filter;
}
}
This works, but it is limited to the "FilterValueOne" type. 这可行,但仅限于“ FilterValueOne”类型。
I want to expand this and make it more generic by implementing a generic type argument. 我想扩展它,并通过实现泛型类型参数使其更通用。
The code snippet below is what I am after: 下面的代码片段是我想要的:
// Enum will expand as the app grows
public enum TypeEnum
{
ValueOne,
ValueTwo,
ValueThree
}
// More classes will be added as the app grows
public class FilterValueOne { }
public class FilterValueTwo { }
public class FilterValueThree { }
[TestClass]
public class TestClassGeneric
{
// _filter to be used as the generic type argument
private object _filter;
// Constructor
public TestClassGeneric(TypeEnum val)
{
switch (val)
{
case TypeEnum.ValueOne:
_filter = GetTestObject<FilterValueOne>();
break;
case TypeEnum.ValueTwo:
_filter = GetTestObject<FilterValueTwo>();
break;
case TypeEnum.ValueThree:
_filter = GetTestObject<FilterValueThree>();
break;
default:
_filter = null;
break;
}
}
private T GetTestObject<T>()
where T : class
{
// Code simplified
object returnValue = new object();
return (T)returnValue;
}
[TestMethod]
public void Generic_FilterValue_TestMethod()
{
// Error: The type _filter could not be found. (However it exists)
ManipulateData(BuildFilter<_filter>());
}
private void ManipulateData<T>(T filter)
{
// Do some stuff with filter
}
private T BuildFilter<T>()
where T : class
{
// I want filter to be dynamically assigned to whatever _filter was
// assigned to in the constructor (FilterValueOne/Two/Three), instead
// of "FilterValueOne"
var filter = typeof(T);
// Initialize filter data members here...
return filter;
}
}
I ultimately want to use "_filter" as a generic type argument in my test method, but I can't get it to work. 我最终希望在测试方法中使用“ _filter”作为泛型类型参数,但无法使其正常工作。 I receive an error stating that "_filter could not be found".
我收到一条错误消息,指出“找不到_filter”。
I have tried multiple ways of using typeof(_filter) etc. but no success. 我尝试了使用typeof(_filter)等的多种方法,但是没有成功。
I don't understand generics and its potential fully, I don't know if this is even possible or if I'm simply missing something. 我不完全了解泛型及其潜力,也不知道这是否有可能,或者我是否只是缺少一些东西。
The T
in BuildFilter<T>
is a type parameter. 该
T
在BuildFilter<T>
是一种类型的参数。 You are passing an instance of a type, in your case an instance of object
(aka System.Object
) not a type. 您正在传递类型的实例,在这种情况下,您传递的是
object
(即System.Object
)而不是类型的实例。
You simply cannot do that, hence the compiler error ( _filter
is not a type; it's an instance and that's why the compiler can't find that "type"). 您根本无法做到这一点,因此会导致编译器错误(
_filter
不是类型;它是一个实例,这就是为什么编译器找不到该“类型”的原因)。
What you want to do instead is instantiate a specific BuildFilter<FilterValueOne>
, etc. and create tests that test each of these things. 相反,您要实例化一个特定的
BuildFilter<FilterValueOne>
等,并创建测试这些功能的测试。
For example 例如
[TestMethod]
public void Generic_FilterValueOne_TestMethod()
{
ManipulateData(new BuildFilter<FilterValueOne>());
}
It looks like you're using MS Test; 看来您正在使用MS Test; AFAIK MS Test does not support generic tests, so you will have to create a test for each type you're interested in.
AFAIK MS Test不支持通用测试,因此您必须为感兴趣的每种类型创建一个测试。
It's easier to use generic test class, but if you prefer to pass filter type through contructor or as a method parameter you can use Activator
class. 使用通用测试类更容易,但是如果您希望通过构造器传递过滤器类型,或者将其用作方法参数,则可以使用
Activator
类。
public class TestClassGeneric<T> where T : new()
{
public void Generic_FilterValue_TestMethod()
{
var filter = new T();
// ManipulateData(filter);
}
}
public class TestClassConstructorArg
{
private readonly Type type;
public TestClassConstructorArg(Type type)
{
this.type = type;
}
public void Generic_FilterValue_TestMethod()
{
var filter = Activator.CreateInstance(type);
// var filter = Activator.CreateInstance(type, BindingFlags.CreateInstance, arguments...);
// ManipulateData(filter);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.