c#Activator.createInstance如何强制转换为泛型基类型?
我正在尝试使用基于西装、颜色、等级派生类型的泛型构建一副牌(有许多不同类型的牌)。我遇到了一个问题,如何使用反射创建它们,然后将生成的对象投射到基本PlayingCard类型PlayingCardc#Activator.createInstance如何强制转换为泛型基类型?,c#,generics,reflection,casting,C#,Generics,Reflection,Casting,我正在尝试使用基于西装、颜色、等级派生类型的泛型构建一副牌(有许多不同类型的牌)。我遇到了一个问题,如何使用反射创建它们,然后将生成的对象投射到基本PlayingCard类型PlayingCard public class Name {} public class SpadesName : Name {} public class Color {} public class BlackColor : Color {} public class Rank
public class Name {}
public class SpadesName : Name {}
public class Color {}
public class BlackColor : Color {}
public class Rank {}
public class AceRank : Rank {}
public class PlayingCard<TName, TColor, TRank>
where TName: Name, new()
where TColor: Color, new()
where TRank: Rank, new()
{}
public class DeckOfCards
{
public PlayingCard<Name, Color, Rank>[] cards;
public DeckOfCards() {}
public void BuildDeckOfCards()
{
this.cards = new PlayingCard<Name, Color, Rank>[52];
Type[] fpcTypeArgs = { typeof(SpadesName), typeof(BlackColor), typeof(AceRank) };
Type fpcType = typeof(PlayingCard<,,>);
Type constructable = fpcType.MakeGenericType(fpcTypeArgs);
// the problem is here.. this will not cast.
// how do I create an object using reflection and cast it to the generic base type PlayingCard<Name, Color, Rank>
var fpc = Activator.CreateInstance(constructable);
this.cards[0] = fpc;
}
}
公共类名{}
公共类SpadesName:名称{}
公共类颜色{}
公共类BlackColor:Color{}
公共类秩{}
公共类AcerBank:秩{}
公共课玩牌
其中TName:Name,new()
其中TColor:Color,new()
其中TRank:Rank,new()
{}
公营甲板
{
公众玩牌[]张牌;
公共卡({}
公屋证()
{
this.cards=新游戏卡[52];
Type[]fpcTypeArgs={typeof(SpadesName)、typeof(BlackColor)、typeof(AceRank)};
类型fpcType=类型(播放卡);
类型constructable=fpcType.MakeGenericType(fpcTypeArgs);
//问题就在这里..这不会强制转换。
//如何使用反射创建对象并将其转换为通用基本类型播放卡
var fpc=Activator.CreateInstance(可构造);
此.cards[0]=fpc;
}
}
使用系统;
公共类名{}
公共类SpadesName:名称{}
公共类颜色{}
公共类BlackColor:Color{}
公共类秩{}
公共类AcerBank:秩{}
公共接口IPlayingCard
//其中TName:Name,new()
//其中TColor:Color,new()
//其中TRank:Rank,new()
{ }
公共类播放卡:IPlayingCard
其中TName:Name,new()
其中TColor:Color,new()
其中TRank:Rank,new()
{ }
公营甲板
{
公共IPlayingCard[]卡;
公共卡({}
公屋证()
{
this.cards=新的IPlayingCard[52];
Type[]fpcTypeArgs={typeof(SpadesName)、typeof(BlackColor)、typeof(AceRank)};
类型fpcType=类型(播放卡);
类型constructable=fpcType.MakeGenericType(fpcTypeArgs);
//问题就在这里..这不会强制转换。
//如何使用反射创建对象并将其转换为通用基本类型播放卡
var fpc=Activator.CreateInstance(可构造);
此.cards[0]=(IPlayingCard)fpc;
}
}
这是我多年来见过的最严重的仿制药滥用。为什么你会用这种方式使用泛型?只需创建一个Card
类,其中包含Suit
和Rank
属性。@在这两者之间,我知道这些类型的评论将在创纪录的时间内发布。。正如博文所述,我正试图用泛型来解决这个问题。这里有一张你看不到的大图。我很清楚,这是可以具体做到的。请尊重这篇文章的原因。运行时的任何反思都不会改变PlayingCard
和PlayingCard
是不同类型的事实。如果创建后者的实例,则不能将其存储在前者的数组中。你真正想要的可能是,也可能不是,但是如果你不想展示这是什么,也不想让其他人在回答中提到这一点,我看不到任何方法可以给这个问题提供有用的答案。这里有一幅你看不到的更大的图景。那么,从一开始就这么说吧,如果你期待着误解,那就更应该这样说。看看c#generic type variance,关于这个主题肯定有无数的问题,所以你会发现阅读这个主题相当容易。长话短说,Bowl
和Bowl
是两种不同的类型,只有在非常特殊的情况下才能使用其中一种来代替另一种。@。现在一、二、三是班级。如何将IPlayingCard
与IPlayingCard
进行比较,以确定哪个更大?你如何确定五张卡片是否构成任何序列?21点-我有一辆七轮坦克和一辆金兰克。我怎么知道我是否应该再拿一张卡?@ScottHannen使用完全和部分特化方法重载:等级有一个值(取决于游戏),允许你根据该值创建一个通用的比较器和分类器。重载的CountSuit(card)方法对每个套装名称都有专门化,它将统计手牌的套装数量,并寻找一个有5个或更多(同花顺)的套装。基于类型的代码体系结构是一种方法。
using System;
public class Name { }
public class SpadesName : Name { }
public class Color { }
public class BlackColor : Color { }
public class Rank { }
public class AceRank : Rank { }
public interface IPlayingCard<out TName, out TColor, out TRank>
//where TName : Name, new()
//where TColor : Color, new()
//where TRank : Rank, new()
{ }
public class PlayingCard<TName, TColor, TRank> : IPlayingCard<Name, Color, Rank>
where TName : Name, new()
where TColor : Color, new()
where TRank : Rank, new()
{ }
public class DeckOfCards
{
public IPlayingCard<Name, Color, Rank>[] cards;
public DeckOfCards() { }
public void BuildDeckOfCards()
{
this.cards = new IPlayingCard<Name, Color, Rank>[52];
Type[] fpcTypeArgs = { typeof(SpadesName), typeof(BlackColor), typeof(AceRank) };
Type fpcType = typeof(PlayingCard<,,>);
Type constructable = fpcType.MakeGenericType(fpcTypeArgs);
// the problem is here.. this will not cast.
// how do I create an object using reflection and cast it to the generic base type PlayingCard<Name, Color, Rank>
var fpc = Activator.CreateInstance(constructable);
this.cards[0] = (IPlayingCard<Name, Color, Rank>)fpc;
}
}