¿Es posible excluir tipos específicos del conjunto de tipos posibles que se pueden usar en un parámetro genérico? Si es así, cómo.Excluyendo tipos en las restricciones genéricas (¿posible?)
Por ejemplo
Foo<T>() : where T != bool
significaría cualquier tipo excepto por el bool tipo.
Editar
¿Por qué?
El siguiente código es mi intento de imponer la restricción negativa.
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; }
}
}
}
Como se puede ver se trata de un poco de fe en la resolución de sobrecarga de ser correcta, y el bit de código maligno @jonskeet esque.
Comenta la sección de ofertas con el ejemplo de tipo inferido y no funciona.
Sería mucho mejor tener la restricción genérica excluida.
¿Cuál sería el uso? 'donde T: x' significa' T' tiene la capacidad de 'x'. Una restricción negativa no parece tener sentido. –
¿Por qué querrías hacer eso? ¿Qué estás intentando lograr? – zmbq
Ver la edición actualizada en la publicación original. –