英文:
Accessing property of derived type in a class derived from a generic class
问题
我对C#还很陌生,无法理解这个问题。
我有两个类,它们都派生自一个泛型类,其中T派生自BackgroundWorker(如下所示)。如何在另一个类中有一个字段,能够容纳这两个派生类之一,并访问派生的BackgroundWorker字段?我需要能够订阅派生BackgroundWorker的事件,并发出CancelAsync等命令...
我有一个泛型类:
public class GenericClass<T> where T : BackgroundWorker
{
public T BgWorker;
}
并从中派生了两个类:
public class DerivedOne : GenericClass<BackgroundWorkerOne>
{ }
public class DerivedTwo : GenericClass<BackgroundWorkerTwo>
{ }
其中:
public class BackgroundWorkerOne : BackgroundWorker
{ }
public class BackgroundWorkerTwo : BackgroundWorker
{ }
然而,我如何在另一个类中创建一个字段/属性,能够根据某些其他变量来容纳DerivedOne或DerivedTwo之一?
我尝试过
public class AnotherClass
{
public GenericClass<BackgroundWorker> Derived;
public void DoSomething()
{
Derived = new DerivedOne();
}
}
但我得到了"无法隐式转换类型 'DerivedOne' 到 'GenericClass
Derived = (GenericClass<BackgroundWorker>)new DerivedOne();
也不起作用。
如上所述,无论Derived是DerivedOne还是DerivedTwo,我都希望能够订阅Derived.BgWorker中的事件。
在这里使用泛型是否是最佳选择?或者是否有其他路径(接口、遮蔽或其他方法)会更好?任何帮助将不胜感激!
编辑
我发现,正如在将泛型
此外,我的问题更多地是关于找到一种方法来从同一个基类中访问BackgroundWorker(在不同类中具有不同类型的属性)。
有什么适当的替代方法来访问Derived属性的BackgroundWorker(无论是BackroundWorkerOne还是BackgroundWorkerTwo)属性?是否有可能这样做,还是我应该重新考虑我的概念?
英文:
I'm still new to C# and cannot wrap my head around this issue.
I have a two classes deriving from a generic class with T deriving from BackgroundWorker (see below). How can I have a field in another class that is able to hold either of the two derived classes and access the field with the derived BackgroundWorker? I need to be able to subscribe to the derived BackgroundWorker's events and issue e.g. CancelAsync command...
I have a generic class:
public class GenericClass<T> where T : BackgroundWorker
{
public T BgWorker;
}
and derive two classes from it:
public class DerivedOne : GenericClass<BackgroundWorkerOne>
{ }
public class DerivedTwo : GenericClass<BackgroundWorkerTwo>
{ }
where:
public class BackgroundWorkerOne : BackgroundWorker
{ }
public class BackgroundWorkerOne : BackgroundWorker
{ }
However, how can I make a field/property in another class that is able to hold either DerivedOne or DerivedTwo depending on some other variable?
I have tried
public class AnotherClass
{
public GenericClass<BackgroundWorker> Derived;
public void DoSomething()
{
Derived = new DerivedOne();
}
}
But I get the "Cannot implicitly convert type 'DerivedOne' to 'GenericClass<BackgroundWorker>'" error. Casting it
Derived = (GenericClass<BackgroundWorker>)new DerivedOne();
does not work either.
As mentioned, I would like to subscribe to the events in Derived.BgWorker regardless if the Derived is of type DerivedOne or DerivedTwo.
Is using generics here the best option? Or would another path (interface, shadowing or something else entirely) be a better solution?
Any help would be appreciated!
EDIT
I see that my approach, as has been pointed out in Cast Generic<Derived> to Generic<Base>, is not really feasible. Thank you for that clarification. The code I've shown above is only what I have tried so far, not necesarrily a fixed direction in which the code has to develop.
Furthermore, my question was more geared towards finding a way to access the BackgroundWorker (property with varying types in different classes) from the same base class.
What would be a suitable alternative to be able to access the BackgroundWorker (wheather BackroundWorkerOne or BackgroundWorkerTwo) property of the Derived property? Is there a possiblity to do so or should I rethink my concept?
答案1
得分: 1
也许协变性是您的代码中缺少的部分?
您可以拥有这个接口
public interface IGenericClass<out T> where T : BackgroundWorker
{
T BackgroundWorker { get; }
}
然后您可以像这样使用这个接口
public class AnotherClass
{
public IGenericClass<BackgroundWorker> Derived;
public void DoSomething()
{
Derived = new DerivedOne();
}
}
public class DerivedOne : IGenericClass<BackgroundWorkerOne>
{
public BackgroundWorkerOne BackgroundWorker { get; }
}
英文:
Maybe covariance is what is missing in your code ?
You can have this interface
public interface IGenericClass<out T> where T : BackgroundWorker
{
T BackgroundWorker { get; }
}
Then you can use this interface like this
public class AnotherClass
{
public IGenericClass<BackgroundWorker> Derived;
public void DoSomething()
{
Derived = new DerivedOne();
}
}
public class DerivedOne : IGenericClass<BackgroundWorkerOne>
{
public BackgroundWorkerOne BackgroundWorker { get; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论