可能な型の集合から特定の型を除外することは可能であり、汎用型のパラメータで使用できますか?もしそうなら。一般制約内の型を除外(可能?)
Foo<T>() : where T != bool
例えば
は、bool型を除き、いずれかのタイプを意味しています。
編集
のはなぜ?
次のコードは、負の制約を適用しようとしたものです。
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; }
}
}
}
あなたはそれが過負荷正しいこと、解像度、および@jonskeet -esque悪コードのビットに信仰のビットを必要と見ることができるように。
推論された型の例を扱ったセクションをコメントアウトしても機能しません。
除外されたジェネリック制約を持つ方がずっと良いでしょう。
何が使用されますか? 'T:x'は' T'が 'x'の能力を持つことを意味します。負の制約は意味がないようです。 –
なぜそれをしたいですか?あなたは何を達成しようとしていますか? – zmbq
元の投稿の更新された編集を参照してください。 –