c#generics cast from和to derived [英] c# generics casting from and to derived

查看:90
本文介绍了c#generics cast from和to derived的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类组


类RowBase {int a; }


类历史< T> :列表< T>其中T:RowBase {int b; }


类SymbolRow:RowBase {int c;}


class SymbolHistory:History< SymbolRow> {


符号S;


}


我需要转换此作业


历史< RowBase> cache = new SymbolHistory();

解决方案

这不是一个新问题。互联网上有很多关于它的讨论,例如https://stackoverflow.com/questions/3528821/generics-and-casting-cannot-cast-inherited-class-to-base-class。

I have the following class group

class RowBase { int a; }

class History<T> : List<T> where T: RowBase { int b; }

class SymbolRow : RowBase { int c;}

class SymbolHistory : History<SymbolRow>{

Symbol S;

}

I need to cast this assignment

History<RowBase> cache = new SymbolHistory();

解决方案

It is not a new problem. There are a lot of discussions about it on the Internet, for example, here https://stackoverflow.com/questions/3528821/generics-and-casting-cannot-cast-inherited-class-to-base-class .


这篇关于c#generics cast from和to derived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆