我有一个抽象类
public abstract class PairingMethod : IPairingMethod
{
virtual string Name { get; } = "Default Pairing";
protected ICollection<IPlayer> PlayersToPair { get; set; }
protected PairingMethod(ICollection<IPlayer> players )
{
PlayersToPair = players;
}
public virtual void GeneratePairingsForRound(IRound round)
{
throw new System.NotImplementedException();
}
}
现在,我已尝试基于派生自上述基类的所有类型创建组合框。我创建了组合框,它使用类名作为项目,但是当触发组合框更改事件时,我需要知道选择了哪个派生类。然后我可以创建该类的一个实例,用于生成配对。
我尝试使用 PairingMethods 作为项目实现我自己的组合框,但无法让它工作。
有什么想法/建议吗?
C
最佳答案
感谢 Mong Zhu 的建议,我做了以下事情
public class PairingComboBox : ComboBox
{
private List<Type> _availableMethod = DerivedTypes.FindAllDerivedTypes<PairingMethod>();
public PairingComboBox()
{
DataSource = DerivedTypes.FindAllDerivedTypes<PairingMethod>();
DisplayMember = "Name";
}
}
public static IPairingMethod CreateInstanceBinder
(string pairingMethodName, ICollection<IPlayer> players)
{
var t = Type.GetType(pairingMethodName + ",Pairings");
return (PairingMethod)Activator.CreateInstance(t, players);
}
我在组合框更改时调用 CreatenIstanceBuilder。传递来自联盟的球员。
关于c# 派生类型到组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141388/