特定の.Netタイプが数値であるかどうかを判断する方法はありますか?たとえば、System.UInt32/UInt16/Doubleすべて数字です。の長いスイッチケースを避けたいですType.FullName。
特定の.Netタイプが数値であるかどうかを判断する方法はありますか?たとえば、System.UInt32/UInt16/Doubleすべて数字です。の長いスイッチケースを避けたいですType.FullName。
回答:
これを試して:
Type type = object.GetType();
bool isNumber = (type.IsPrimitiveImple && type != typeof(bool) && type != typeof(char));
プリミティブ型は、Boolean、Byte、SByte、Int16、UInt16、Int32、UInt32、Int64、UInt64、Char、Double、およびSingleです。
撮影ギヨームのソリューションをさらに少し:
public static bool IsNumericType(this object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
使用法:
int i = 32;
i.IsNumericType(); // True
string s = "Hello World";
s.IsNumericType(); // False
decimal型は数値ではありませんか?
decimal が数値であることに疑いの余地はありません。それがプリミティブではないからといって、それが数値ではないという意味ではありません。あなたのコードはこれを説明する必要があります。
スイッチを使用しないでください-セットを使用してください:
HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(decimal), typeof(byte), typeof(sbyte),
typeof(short), typeof(ushort), ...
};
編集:型コードを使用することに対するこれの1つの利点は、新しい数値型が.NETに導入されると(BigIntegerやComplexなど)、簡単に調整できることです。これらの型は型コードを取得しません。
switch単純に動作しないTypeので、できません。TypeCodeもちろんスイッチを入れることもできますが、それは別の問題です。
どのソリューションもNullableを考慮に入れていません。
Jon Skeetのソリューションを少し変更しました:
private static HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(int),
typeof(uint),
typeof(double),
typeof(decimal),
...
};
internal static bool IsNumericType(Type type)
{
return NumericTypes.Contains(type) ||
NumericTypes.Contains(Nullable.GetUnderlyingType(type));
}
Nullable自体を自分のHashSetに追加できることはわかっています。しかし、このソリューションは、特定のNullableをリストに追加し忘れる危険を回避します。
private static HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(int),
typeof(int?),
...
};
public static bool IsNumericType(Type type)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
削除された最適化に関するメモ(enziコメントを参照)
そして本当に最適化したい場合(読みやすさと安全性が失われます...):
public static bool IsNumericType(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);
//The TypeCode of numerical types are between SByte (5) and Decimal (15).
return (int)typeCode >= 5 && (int)typeCode <= 15;
}
return unchecked((uint)Type.GetTypeCode(type) - 5u) <= 10u;によって導入されたブランチを削除することになり&&ます。
基本的にスキートのソリューションですが、次のようにNullable型で再利用できます。
public static class TypeHelper
{
private static readonly HashSet<Type> NumericTypes = new HashSet<Type>
{
typeof(int), typeof(double), typeof(decimal),
typeof(long), typeof(short), typeof(sbyte),
typeof(byte), typeof(ulong), typeof(ushort),
typeof(uint), typeof(float)
};
public static bool IsNumeric(Type myType)
{
return NumericTypes.Contains(Nullable.GetUnderlyingType(myType) ?? myType);
}
}
基づいたアプローチフィリップの提案を強化、SFun28の内側の型チェックのためのNullableタイプ:
public static class IsNumericType
{
public static bool IsNumeric(this Type type)
{
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
case TypeCode.Object:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return Nullable.GetUnderlyingType(type).IsNumeric();
//return IsNumeric(Nullable.GetUnderlyingType(type));
}
return false;
default:
return false;
}
}
}
なんでこれ?私は与えられたものType typeが数値型であるかどうかをチェックしなければならず、任意のものobject oが数値型であるかどうかはチェックしませんでした。
C#7では、この方法を使用すると、スイッチをオンにしTypeCodeたり、HashSet<Type>:
public static bool IsNumeric(this object o) => o is byte || o is sbyte || o is ushort || o is uint || o is ulong || o is short || o is int || o is long || o is float || o is double || o is decimal;
テストは次のとおりです。
public static class Extensions
{
public static HashSet<Type> NumericTypes = new HashSet<Type>()
{
typeof(byte), typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong), typeof(short), typeof(int), typeof(long), typeof(decimal), typeof(double), typeof(float)
};
public static bool IsNumeric1(this object o) => NumericTypes.Contains(o.GetType());
public static bool IsNumeric2(this object o) => o is byte || o is sbyte || o is ushort || o is uint || o is ulong || o is short || o is int || o is long || o is decimal || o is double || o is float;
public static bool IsNumeric3(this object o)
{
switch (o)
{
case Byte b:
case SByte sb:
case UInt16 u16:
case UInt32 u32:
case UInt64 u64:
case Int16 i16:
case Int32 i32:
case Int64 i64:
case Decimal m:
case Double d:
case Single f:
return true;
default:
return false;
}
}
public static bool IsNumeric4(this object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
var count = 100000000;
//warm up calls
for (var i = 0; i < count; i++)
{
i.IsNumeric1();
}
for (var i = 0; i < count; i++)
{
i.IsNumeric2();
}
for (var i = 0; i < count; i++)
{
i.IsNumeric3();
}
for (var i = 0; i < count; i++)
{
i.IsNumeric4();
}
//Tests begin here
var sw = new Stopwatch();
sw.Restart();
for (var i = 0; i < count; i++)
{
i.IsNumeric1();
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
for (var i = 0; i < count; i++)
{
i.IsNumeric2();
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
for (var i = 0; i < count; i++)
{
i.IsNumeric3();
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
for (var i = 0; i < count; i++)
{
i.IsNumeric4();
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds);
}
Type.IsPrimitiveを使用して、次のようなBooleanとCharタイプを整理できます。
bool IsNumeric(Type type)
{
return type.IsPrimitive && type!=typeof(char) && type!=typeof(bool);
}
編集:数値と見なさない場合は、IntPtrおよびUIntPtr型も除外することをお勧めします。
decimal型は数値ではありませんか?
null型をサポートする型拡張。
public static bool IsNumeric(this Type type)
{
if (type == null) { return false; }
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = type.GetGenericArguments()[0];
}
switch (Type.GetTypeCode(type))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}
修正スキートのとarvimanのソリューションを活用しGenerics、Reflection、とC# v6.0。
private static readonly HashSet<Type> m_numTypes = new HashSet<Type>
{
typeof(int), typeof(double), typeof(decimal),
typeof(long), typeof(short), typeof(sbyte),
typeof(byte), typeof(ulong), typeof(ushort),
typeof(uint), typeof(float), typeof(BigInteger)
};
に続く:
public static bool IsNumeric<T>( this T myType )
{
var IsNumeric = false;
if( myType != null )
{
IsNumeric = m_numTypes.Contains( myType.GetType() );
}
return IsNumeric;
}
使用法(T item):
if ( item.IsNumeric() ) {}
null falseを返します。
最悪の状況のメソッドがすべてのタイプを通過するたびに、スイッチは少し遅くなります。私は、Dictonaryを使用する方がいいと思います。この状況では、次のようになりますO(1)。
public static class TypeExtensions
{
private static readonly HashSet<Type> NumberTypes = new HashSet<Type>();
static TypeExtensions()
{
NumberTypes.Add(typeof(byte));
NumberTypes.Add(typeof(decimal));
NumberTypes.Add(typeof(double));
NumberTypes.Add(typeof(float));
NumberTypes.Add(typeof(int));
NumberTypes.Add(typeof(long));
NumberTypes.Add(typeof(sbyte));
NumberTypes.Add(typeof(short));
NumberTypes.Add(typeof(uint));
NumberTypes.Add(typeof(ulong));
NumberTypes.Add(typeof(ushort));
}
public static bool IsNumber(this Type type)
{
return NumberTypes.Contains(type);
}
}
C#のTypeSupport nugetパッケージを試してください。(他の多くの機能の中で)すべての数値型の検出をサポートしています。
var extendedType = typeof(int).GetExtendedType();
Assert.IsTrue(extendedType.IsNumericType);
残念ながら、これらの型はすべて値型であることを除いて、共通点はあまりありません。しかし、長いスイッチケースを回避するために、これらすべてのタイプで読み取り専用リストを定義し、指定されたタイプがリスト内にあるかどうかを確認するだけで済みます。
これらはすべて値型です(ブール値およびおそらく列挙型を除く)。だからあなたは単に使うことができます:
bool IsNumberic(object o)
{
return (o is System.ValueType && !(o is System.Boolean) && !(o is System.Enum))
}
struct...それはあなたが望むことではないと思います。
編集:まあ、私はよりパフォーマンスが高くなるように以下のコードを変更し、それに対して@Hugoによって投稿されたテストを実行しました。速度は@HugoのIFとほぼ同じで、彼のシーケンスの最後の項目(10進数)を使用しています。ただし、最初のアイテム「バイト」を使用する場合、彼は簡単に理解できますが、パフォーマンスに関しては明らかに順序が重要です。以下のコードを使用すると、記述が簡単になり、コストの一貫性が高まりますが、保守や将来の保証はできません。
Type.GetTypeCode()からConvert.GetTypeCode()に切り替えると、パフォーマンスが大幅にスピードアップし、約25%、VS Enum.Parse()が10倍遅くなりました。
私はこの記事が古いです知っているが、IFこのようなものになるだろうされたTypeCode列挙法を使用して、最も簡単な(そしておそらく最も安いです):
public static bool IsNumericType(this object o)
{
var t = (byte)Convert.GetTypeCode(o);
return t > 4 && t < 16;
}
TypeCodeの次の列挙型定義があるとします。
public enum TypeCode
{
Empty = 0,
Object = 1,
DBNull = 2,
Boolean = 3,
Char = 4,
SByte = 5,
Byte = 6,
Int16 = 7,
UInt16 = 8,
Int32 = 9,
UInt32 = 10,
Int64 = 11,
UInt64 = 12,
Single = 13,
Double = 14,
Decimal = 15,
DateTime = 16,
String = 18
}
私はそれを徹底的にテストしていませんが、基本的なC#数値型の場合、これでカバーできるようです。ただし、@ JonSkeetが述べたように、この列挙型は、将来.NETに追加される追加の型については更新されません。
おっとっと!質問を誤解してください!個人的には、スキートのと一緒に転がるでしょう。
データをDoSomethingオンTypeにしたいようです。あなたができることは次のとおりです
public class MyClass
{
private readonly Dictionary<Type, Func<SomeResult, object>> _map =
new Dictionary<Type, Func<SomeResult, object>> ();
public MyClass ()
{
_map.Add (typeof (int), o => return SomeTypeSafeMethod ((int)(o)));
}
public SomeResult DoSomething<T>(T numericValue)
{
Type valueType = typeof (T);
if (!_map.Contains (valueType))
{
throw new NotSupportedException (
string.Format (
"Does not support Type [{0}].", valueType.Name));
}
SomeResult result = _map[valueType] (numericValue);
return result;
}
}