Wykluczanie typów w ogólnych ograniczeniach (możliwe?)
Czy można wykluczyć określone typy ze zbioru możliwych typów, które można wykorzystać w parametrze ogólnym? Jeśli tak to jak.
Na przykład
Foo<T>() : where T != bool
oznaczałoby dowolny typ z wyjątkiem typu bool.
Edytować
Czemu?
Poniższy kod jest moją próbą wymuszenia negatywnego ograniczenia.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var x1=Lifted.Lift("A");
var x2=Lifted.Lift(true);
}
static class Lifted
{
// This one is to "exclude" the inferred type variant of the parameter
[Obsolete("The type bool can not be Lifted", true)]
static public object Lift(bool value) { throw new NotSupportedException(); }
// This one is to "exclude" the variant where the Generic type is specified.
[Obsolete("The type bool can not be Lifted", true)]
static public Lifted<T> Lift<T>(bool value) { throw new NotSupportedException(); }
static public Lifted<T> Lift<T>(T value) { return new Lifted<T>(value); }
}
public class Lifted<T>
{
internal readonly T _Value;
public T Value { get { return this._Value; } }
public Lifted(T Value) { _Value = Value; }
}
}
}
Jak widać, wiąże się to z pewną wiarą w poprawność rozdzielczości przeciążenia i odrobiną złego kodu @jonskeet.
Skomentuj sekcję z ofertami z przykładowym typem wnioskowania i nie działa.
O wiele lepiej byłoby mieć wykluczone ogólne ograniczenie.