A generic interface is available that looks like this
interface IMatch<T>
{
bool MatchWithExpected(T actualValue);
}
Implementations of this interface for different types is available already (Int, struct, Enum etc.,).
I have a struct called ObjectType
. I want an implementation of the above interface that works with a list of ObjectType
.
The only way I was able to do this is by doing the following
class MultiStructMatch<T> : IMatch<T> where T is List<ObjectType>
Is there any way to make List<ObjectType>
generic that denotes List<struct>
? I tried List<System.ValueType>
but cannot convert List<ObjectType>
to List<System.ValueType>
.
You could pass List<T>
to IMatch<>
and apply the type constraint to T:
class MultiStructMatch<T> : IMatch<List<T>> where T: struct
{
}
and instantiate this with:
struct M
{
}
...
var m = new MultiStructMatch<M>;
You'll need a second generic parameter and declare your class like this:
class MultiStructMatch<T, TElement> : IMatch<T>
where T : List<TElement>
where TElement : struct
{}
And instantiate it eg:
var m = new MultiStructMatch<List<ObjectType>, ObjectType>();
This syntax is a little clumsy, because the extra type parameter seems redundant.
But I don't see a better way to apply the
struct
constraint.
Obviously Panagiotis found the better syntax.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.