I have a class like the following:
public class DropDownControl<T, Key, Value> : BaseControl
where Key: IComparable
{
private IEnumerable<T> mEnumerator;
private Func<T, Key> mGetKey;
private Func<T, Value> mGetValue;
private Func<Key, bool> mIsKeyInCollection;
public DropDownControl(string name, IEnumerable<T> enumerator, Func<T, Key> getKey, Func<T, Value> getValue, Func<Key, bool> isKeyInCollection)
: base(name)
{
mEnumerator = enumerator;
mGetKey = getKey;
mGetValue = getValue;
mIsKeyInCollection = isKeyInCollection;
}
And I want to add a convenience function for Dictionaries (because they support all operations efficiently on their own).
But the problem is that such a constructor would only specify Key and Value but not T directly, but T is just KeyValuePair. Is there a way to tell the compiler for this constructor T is KeyValuePair, like:
public DropDownControl<KeyValuePair<Key, Value>>(string name, IDictionary<Key, Value> dict) { ... }
Currently I use a static Create function as workaround, but I would like a direct constructor better.
public static DropDownControl<KeyValuePair<DKey, DValue>, DKey, DValue> Create<DKey, DValue>(string name, IDictionary<DKey, DValue> dictionary)
where DKey: IComparable
{
return new DropDownControl<KeyValuePair<DKey, DValue>, DKey, DValue>(name, dictionary, kvp => kvp.Key, kvp => kvp.Value, key => dictionary.ContainsKey(key));
}