I have a class named "baseClass". From this class I inherit a class names "inheritedClass" (public class inheritedClass: baseClass)
我有一个名为“baseClass”的类。从这个类我继承了一个类名“inheritedClass”(公共类inheritedClass:baseClass)
The baseClass contains a public function that returns a HashSet<baseClass>
. When called from the inheritedClass, the return type is obviously still HashSet<baseClass>
, but I need a HashSet<inheritedClass>
.
baseClass包含一个返回HashSet
A conversion ala (HashSet<inheritedClass>
)returnValue, where returnValue is of Type HashSet<baseClass>
doesn't work.
转换ala(HashSet
Is there a way to convert the HashSet-Type from baseClass to inheritedClass without converting each element manually?
有没有办法将HashSet-Type从baseClass转换为inheritedClass而不手动转换每个元素?
Thanks in advance, Frank
先谢谢你,弗兰克
2 个解决方案
#1
Do you really mean C# in the tags? HashMap
is a Java type. Also it generally has two type parameters rather than one...
你真的是指标签中的C#吗? HashMap是一种Java类型。它通常有两个类型参数而不是一个......
In C#, generic classes are always invariant. Some interfaces will be variant in C# 4, but very few (only those which either only use the type parameter in an output positiion, e.g. IEnumerable<T>
, or only use the type parameter in an input position, e.g. IComparable<T>
).
在C#中,泛型类总是不变的。有些接口在C#4中是变体的,但很少(只有那些只在输出位置使用type参数的接口,例如IEnumerable
If you can provide more precise information about your situation, we'll probably be able to help come up with a simple solution - particularly if you can use LINQ with its Cast<T>()
method.
如果您可以提供有关您的情况的更准确信息,我们可能会帮助提出一个简单的解决方案 - 特别是如果您可以使用LINQ及其Cast
EDIT: Okay, with HashSet<T>
:
编辑:好的,使用HashSet
HashSet<BaseType> baseSet = ...;
var derivedSet = new HashSet<DerivedType>(baseSet.Cast<DerivedType>());
Note that even with C# 4 this would be necessary because the compiler doesn't know that every value in baseSet
is an instance of DerivedType
- there has to be an execution-time check. The reverse (creating a HashSet<BaseType>
from a HashSet<DerivedType>
) would work in C# 4.
请注意,即使使用C#4,这也是必要的,因为编译器不知道baseSet中的每个值都是DerivedType的实例 - 必须进行执行时检查。反向(从HashSet
FURTHER EDIT: If you just want to use UnionWith
, that doesn't require a HashSet<DerivedType>
- it requires an IEnumerable<DerivedType>
. I suggest you do:
进一步编辑:如果您只想使用UnionWith,那不需要HashSet
HashSet<BaseType> baseSet = ...;
HashSet<DerivedType> derivedSet = ...;
derivedSet.UnionWith(baseSet.Cast<DerivedType>());
#2
Here's the solution
这是解决方案
//**Your BaseClass**
public class BaseClass<T> where T : BaseClass<T>
{
public HashSet<T> GetHashSet()
{
HashSet<T> _hSet = new HashSet<T>();
//do some work
//create a HashSet<T> and return;
return _hSet;
}
}
//**Your Inherited/Derived Class**
public class InheritedClass : BaseClass<InheritedClass>
{
//you have the method inherited as you need.}
}
#1
Do you really mean C# in the tags? HashMap
is a Java type. Also it generally has two type parameters rather than one...
你真的是指标签中的C#吗? HashMap是一种Java类型。它通常有两个类型参数而不是一个......
In C#, generic classes are always invariant. Some interfaces will be variant in C# 4, but very few (only those which either only use the type parameter in an output positiion, e.g. IEnumerable<T>
, or only use the type parameter in an input position, e.g. IComparable<T>
).
在C#中,泛型类总是不变的。有些接口在C#4中是变体的,但很少(只有那些只在输出位置使用type参数的接口,例如IEnumerable
If you can provide more precise information about your situation, we'll probably be able to help come up with a simple solution - particularly if you can use LINQ with its Cast<T>()
method.
如果您可以提供有关您的情况的更准确信息,我们可能会帮助提出一个简单的解决方案 - 特别是如果您可以使用LINQ及其Cast
EDIT: Okay, with HashSet<T>
:
编辑:好的,使用HashSet
HashSet<BaseType> baseSet = ...;
var derivedSet = new HashSet<DerivedType>(baseSet.Cast<DerivedType>());
Note that even with C# 4 this would be necessary because the compiler doesn't know that every value in baseSet
is an instance of DerivedType
- there has to be an execution-time check. The reverse (creating a HashSet<BaseType>
from a HashSet<DerivedType>
) would work in C# 4.
请注意,即使使用C#4,这也是必要的,因为编译器不知道baseSet中的每个值都是DerivedType的实例 - 必须进行执行时检查。反向(从HashSet
FURTHER EDIT: If you just want to use UnionWith
, that doesn't require a HashSet<DerivedType>
- it requires an IEnumerable<DerivedType>
. I suggest you do:
进一步编辑:如果您只想使用UnionWith,那不需要HashSet
HashSet<BaseType> baseSet = ...;
HashSet<DerivedType> derivedSet = ...;
derivedSet.UnionWith(baseSet.Cast<DerivedType>());
#2
Here's the solution
这是解决方案
//**Your BaseClass**
public class BaseClass<T> where T : BaseClass<T>
{
public HashSet<T> GetHashSet()
{
HashSet<T> _hSet = new HashSet<T>();
//do some work
//create a HashSet<T> and return;
return _hSet;
}
}
//**Your Inherited/Derived Class**
public class InheritedClass : BaseClass<InheritedClass>
{
//you have the method inherited as you need.}
}