C# 如何将泛型类型强制转换为其他类型?
如显示的第一个类所示,我需要将Activité强制转换为Réunion(Réunion扩展Activité),但编译器告诉我不能。为什么?我将提出一个方案,以便您更好地了解我的类结构以及所有其他类。多谢各位C# 如何将泛型类型强制转换为其他类型?,c#,generics,casting,C#,Generics,Casting,如显示的第一个类所示,我需要将Activité强制转换为Réunion(Réunion扩展Activité),但编译器告诉我不能。为什么?我将提出一个方案,以便您更好地了解我的类结构以及所有其他类。多谢各位 class Employé<T> { private string nom; private Local bureau; private LinkedList<Activité<T>> activités; public
class Employé<T>
{
private string nom;
private Local bureau;
private LinkedList<Activité<T>> activités;
public Employé(string nom, Local bureau)
{
this.nom = nom;
this.bureau = bureau;
}
public void AjouteActivité(params Activité<T>[] activités)
{
foreach(Activité<T> activité in activités)
{
if (activité as Réunion != null)
// here's the problem !!! ((Réunion)activité).EmployéConvoqués = activité;
}
}
}
classé
{
私有字符串名称;
私营地方局;
私有链接列表
以下是其他课程:
abstract class Activité<T>
{
private string label;
private DateTime début, fin;
private T lieu;
private readonly int id;
private static int CPT = 0;
public Activité(string label, DateTime début, DateTime fin, T lieu)
{
this.label = label;
this.début = début;
this.fin = fin;
this.lieu = lieu;
this.id = ++CPT;
}
public override string ToString()
{
return $"{id} : {label}(de {début} à {fin}) - {DescriptionLieu()}";
}
public double Duree()
{
return fin.Subtract(début).TotalMinutes;
}
public int Id
{
//tester get; seulement
get
{
return id;
}
}
public T Lieu
{
get
{
return lieu;
}
}
public abstract string DescriptionLieu();
}
抽象类活动
{
私有字符串标签;
私人约会时间début,fin;
私人住宅;
私有只读int-id;
私有静态int CPT=0;
公共活动(字符串标签、日期时间début、日期时间fin、T LIUE)
{
this.label=标签;
这个.début=début;
this.fin=fin;
this.liu=liu;
this.id=++CPT;
}
公共重写字符串ToString()
{
返回$“{id}:{label}(de{début}{fin})-{DescriptionLieu()}”;
}
公开双被告()
{
返回fin.Subtract(début).TotalMinutes;
}
公共整数Id
{
//测试器获取;seulement
得到
{
返回id;
}
}
公共图书馆
{
得到
{
退票;
}
}
公共抽象字符串DescriptionLieu();
}
class ActivitéExtérieure:Activité
{
public ActivitéExtérieure(字符串标签,DateTime début,DateTime fin,string liue):base(标签,début,fin,liue){}
公共重写字符串DescriptionLieu()
{
退票;
}
}
class ActivitéInterne:Activité
{
公共内部活动(字符串标签、日期时间début、日期时间fin、本地LIUE):基本(标签、début、fin、LIUE)
{
(本);
}
公共重写字符串DescriptionLieu()
{
返回$“local::{liue.NumComplet}”;
}
}
classé
{
私有字符串名称;
私营地方局;
私人社交活动;
公共雇员(字符串名称,地方局)
{
this.nom=nom;
这个局=局;
}
公共无效活动(参数Activité[]Activités)
{
foreach(活动中的活动)
{
if(活动作为联盟!=null)
(Réunion)activité);
}
}
}
本地类
{
私人投资;
私人整数;
私人楼宇拥有无线网络;
私人词典历史事件;
公共整数
{
得到
{
返回数字;
}
设置
{
如果(值<0 | |值>99)
抛出新的IndexOutOfRangeException();
其他的
数值=数值;
}
}
公共int NumComplet
{
得到
{
返回日期*100+数字;
}
}
公共场所拥有无线网络
{
得到
{
返回所拥有的wifi;
}
}
公共本地(国际互联网、本地无线网络、国际数字)
{
this.etage=etage;
this.possedeWifi=possedeWifi;
Numero=Numero;
}
公共本地(int-etage,int-numero):这个(etage,true,numero){}
公共本地(int-Local,bool-possedeWifi):此(本地/100,possedeWifi,本地%100){}
公共活动(内部活动a)
{
历史问题活动添加(a.Id,a);
}
}
class Réunion:ActivitéInterne
{
私有HashSet-employeeésConvoqués;
公共Réunion(字符串标签、日期时间début、日期时间fin、本地LIUE):基(标签、début、fin、LIUE){}
公共雇佣车队
{
设置
{
雇佣新员工增加(价值);
}
}
}
错误消息显示“强制转换是冗余的”。这是因为您已经测试了“activitéas Réunion!=null”.编译器发现,在'if'子句中,此条件已经为true,因此强制转换没有意义。另一方面,您无法访问activité.EmployeeéConvalqusés,因为activité的静态类型不是Réunion
您所要做的就是在测试类型时引入一个新变量。如下所示:
if (activité is Réunion réunion) {
réunion.EmployéConvoqués = activité;
}
但是,如果您尝试这样做,您将看到分配无法完成,因为您正在尝试将活动分配给雇员。这些类型不兼容。可能您的意思是
foreach (Activité<T> activité in activités) {
if (activité is Réunion réunion && this is Employé<Local> employéLocal) {
réunion.EmployéConvoqués = employéLocal;
}
}
foreach(活动中的活动){
if(activitéis Réunion Réunion&这是EmployéEmployéLocal){
réunion.Employéconvaqués=EmployéLocal;
}
}
注释:在Réunion的定义中,在设置属性Employeeéemployeeéconvaqués时,您将添加到HashSet EmployeeésConvoqués。从样式角度来看,这很奇怪,因为人们通常认为Employeeé类型的属性将表示单个Employeeé,而不是Employeeé的集合。我建议您删除setter,而不是define
public void Ajoute( Employé<Local> employéConvoqué) {
this.employésConvoqués.Add(employéConvoqué);
}
public void Ajoute(雇佣雇佣车队){
这个.employésConvoqués.Add(employéconvaqué);
}
不是问题,因为你没有将activité
转换为employeeé
?你能说得更准确一点吗?我不明白Réunion is activité
不是真的,如果t
不是Local
。编译器无法确保activité
是activité您告诉我应该如何以及在哪里修复它?您正在尝试将一个值(类型为Activité
)设置为一个完全不同类型的属性(Employéconvalqusés
)
class Local
{
private int etage;
private int numero;
private bool possedeWifi;
private Dictionary<int, ActivitéInterne> historiquesActivités;
public int Numero
{
get
{
return numero;
}
set
{
if (value < 0 || value > 99)
throw new IndexOutOfRangeException();
else
numero = value;
}
}
public int NumComplet
{
get
{
return etage * 100 + numero;
}
}
public bool PossedeWifi
{
get
{
return possedeWifi;
}
}
public Local(int etage, bool possedeWifi, int numero)
{
this.etage = etage;
this.possedeWifi = possedeWifi;
Numero = numero;
}
public Local(int etage, int numero) : this(etage, true, numero) { }
public Local(int local, bool possedeWifi) : this(local / 100, possedeWifi, local % 100) { }
public void AjouteActivité(ActivitéInterne a)
{
historiquesActivités.Add(a.Id, a);
}
}
class Réunion : ActivitéInterne
{
private HashSet<Employé<Local>> employésConvoqués;
public Réunion(string label, DateTime début, DateTime fin, Local lieu) : base(label, début, fin, lieu) { }
public Employé<Local> EmployéConvoqués
{
set
{
employésConvoqués.Add(value);
}
}
}
if (activité is Réunion réunion) {
réunion.EmployéConvoqués = activité;
}
foreach (Activité<T> activité in activités) {
if (activité is Réunion réunion && this is Employé<Local> employéLocal) {
réunion.EmployéConvoqués = employéLocal;
}
}
public void Ajoute( Employé<Local> employéConvoqué) {
this.employésConvoqués.Add(employéConvoqué);
}