2017-10-28 73 views
-1

我试图用西装,颜色,等级派生类型的泛型来构建一副牌(有许多不同类型的牌)。我在如何使用反射来创建它们,然后一个问题投产生的对象基本游戏牌类游戏牌c#Activator.CreateInstace如何转换为泛型基类型?

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; 
    } 
} 
+3

这是关于最糟糕的仿制药滥用我在几年看到。为什么你会以这种方式使用泛型?只需使用'Suit'和'Rank'属性创建一个Card类。 – InBetween

+1

@InBetween是的,我知道这些类型的评论将张贴,记录时间不会少于..作为帖子说,我试图解决这个使用泛型。这里有一张你看不到的大图。我很清楚,这可以具体完成。请尊重帖子的原因。 – Kirk

+1

没有任何运行时反射会改变'PlayingCard <名称,颜色,等级>和'PlayingCard '的不同类型。如果创建后者的实例,则不能将其存储在前者的数组中。你真正追求的东西可能或不可能,但如果你不想要显示那是什么,也不希望别人在答案中解决这个问题,我看不出有什么办法给出有用的答案对这个问题。 – hvd

回答

-1
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; 
    } 
} 
+0

这就是我一直在寻找......谢谢。 – Kirk

+0

对不起,但这是非常糟糕的。现在一,二和三是*类。*你如何比较'IPlayingCard '和'IPlayingCard '以确定哪个更大?你如何确定五张牌是否形成任何顺序?二十一点 - 我有一个SevenRank和一个KingRank。我怎么知道我是否应该再拿一张卡? –

+0

@ScottHannen使用完全和部分特化方法重载:等级有一个值(取决于游戏),这将允许您创建一个通用比较器和基于该值的分类器。重载的CountSuit(卡)方法针对每个套装名称进行专业化处理,并计算手的套装数量并查找5个或更多(冲水)的套装。基于类型的代码架构就是这种方式。 – Kirk

相关问题