[英]How to make a list of something that implements abstract class and interface (C#)?
[英]Can't add instance of class that implements an interface to a List of said interface in C#
我有这个错误:
无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.List'
对于以下代码:
当我尝试添加到列表时:
if (information != null) // not empty
{
foreach (CharacterInformation info in information)
{
chButtons.Add(new GuiSelectCharacterButton(
selectGui,
new Rectangle(100, 100, 450, 100)
));
chButtons[chButtons.Count - 1].ImageTexture = GUISprites.Instance().GetTexture(UITexture.RainbowDash);
chButtons[chButtons.Count - 1].Texture = GUISprites.Instance().GetTexture(UITexture.CommonButtonNotPressed);
chButtons[chButtons.Count - 1].PressedTexture = GUISprites.Instance().GetTexture(UITexture.CommonButtonPressed);
chButtons[chButtons.Count - 1].ImageTextureWidth = 90;
chButtons[chButtons.Count - 1].ImageTextureHeight = 90;
chButtons[chButtons.Count - 1].ButtonPressed += LoginCharacterHandler;
chButtons[chButtons.Count - 1].Font = GameFonts.Instance().GetSpriteFont(FontType.UI);
chButtons[chButtons.Count - 1].Information = information[chButtons.Count - 1];
}
}
// error at this line
buttoncontainer.ButtonList = chButtons;
舱位声明:
public class GuiSelectCharacterButton : GUIImageButton, IGameUserInterfaceImageButton
{
public GuiSelectCharacterButton(GUI parent, Rectangle area)
: base(parent, area)
{
}
}
接口:
public interface IGameUserInterfaceImageButton : IGameUserInterfaceComponent
{
bool InsideContainer { get; set; }
Rectangle InsideContainerArea { get; set; }
}
IGameUserInterfaceImageButton列表:
private List<IGameUserInterfaceImageButton> buttonList;
public List<IGameUserInterfaceImageButton> ButtonList
{
get
{
return buttonList;
}
set
{
buttonList = value;
scrollbarThumbTotalHeight = marginButtons;
scrollbarThumbY = 0;
int yhelper = 0;
foreach (IGameUserInterfaceImageButton button in buttonList)
{
yhelper += marginButtons;
button.InsideContainer = true;
button.InsideContainerArea = new Rectangle(
marginXbutton,
yhelper,
button.Area.Width,
button.Area.Height
);
yhelper += marginButtons + button.Area.Height;
scrollbarThumbTotalHeight += button.Area.Height + marginButtons * 2;
}
scrollbarThumbDrawHeight = 0;
if (scrollbarThumbTotalHeight > Area.Height)
{
scrollbarThumbDrawHeight = (int)Math.Pow(AreaScrollbar.Height, 2) / scrollbarThumbTotalHeight;
}
}
}
我不知道我的错误在哪里。 基类(GUIImageButton)实现所述接口的所有内容。 我在我的程序的其他部分做了这个,但由于任何奇怪的原因,它在这里不起作用。
如果我们有一个接口和一个实现它的类:
public interface IFoo { }
public class Foo : IFoo { }
这不起作用 :
var x = new List<Foo>();
x.Add(new Foo());
var y = new List<IFoo>();
y = x;
这确实有效:
var x = new List<IFoo>();
x.Add(new Foo());
var y = new List<IFoo>();
y = x;
您的问题不在于尝试将实例添加到列表中; chButtons.Add()
方法工作正常。
你的问题是试图分配时chButtons
到buttoncontainer.ButtonList
。 这两个标识符的类型不同。 你已经说过什么类型的ButtonList
,但你还没有说chButtons
是什么。
如果我怀疑chButtons
是List<GuiSelectCharacterButton>
,那么你正在做的事情将不起作用。 即使Foo : IFoo
,也不能将List<Foo>
分配给List<IFoo>
; 这称为“协方差”,C#不支持具体类。
如果定义chButtons
有确切的相同类型ButtonList
,如List<IGameUserInterfaceImageButton>
那么你的代码应该很好地工作。
当A
继承自IA
时,您无法直接从List<A>
为List<IA>
事件。
你可以做的是使chButtons
也是IGameUserInterfaceImageButton
类型。 然后在添加时将每个项目转换为界面:
var chButtons = new List<IGameUserInterfaceImageButton>();
chButtons.Add(
(IGameUserInterfaceImageButton)new GuiSelectCharacterButton(
// etc ...
)
);
// this is now valid
buttoncontainer.ButtonList = chButtons
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.