我有以下代碼:通用接口與多態
public abstract class Operand<T>
{
public T Value { get; protected set; }
public bool IsEmpty { get; protected set; }
public override string ToString()
{
return IsEmpty ? Value.ToString() : string.Empty;
}
}
public class DoubleOperand : Operand<Double> {}
public interface IOperandFactory<T>
{
Operand<T> CreateEmptyOperand();
Operand<T> CreateOperand(T value);
}
public class DoubleFactory: IOperandFactory<double>
{
public Operand<Double> CreateEmptyOperand()
{
//implementation
}
public Operand<Double> CreateOperand(double value)
{
//implementation
}
}
我的代碼運動簡化到只顯示結構。 現在我需要associationDictionary將返回IOperandFactory
所需Type
: 事情是這樣的:
var factoryDict =
new Dictionary<Type, IOperandFactory<>>() { { typeof(double), new DoubleFactory() } };
你能幫我實現它,如果它是可能的嗎?
感謝您快速的解答。 – Danil 2012-01-16 09:05:55
@Danil我編輯了一個完整的例子,如果它有幫助 – 2012-01-16 09:09:19
是的,謝謝,我認爲它也會對其他用戶有用。 – Danil 2012-01-16 09:14:48