width:1px;
white-space: nowrap;
отлично работает у меня:)
Вы можете написать метод расширения для объекта, чтобы получить синтаксис вроде:
if (obj.Is<byte, int, long>()) { ... }
Что-то вроде этого (используйте несколько версий для меньшего или большего количества общих аргументов:
public static bool Is<T1, T2, T3>(this object o)
{
return o is T1 || o is T2 || o is T3;
}
Только:
static readonly HashSet<Type> types = new HashSet<Type>
{ typeof(byte), typeof(int), typeof(long) etc };
...
if (types.Contains(obj.GetType())
{
}
Или используйте obj.GetType (). GetTypeCode ()
.
Я бы бросил это в метод чтобы немного упростить:
private static bool ObjIsNumber(object obj)
{
return (obj is byte || obj is int || obj is long ||
obj is decimal || obj is double || obj is float);
}
Почему бы вам этого не сделать?
bool IsRequestedType(object obj)
{
if (obj is byte || obj is int || obj is long || obj is decimal || obj is double || obj is float)
return true;
return false;
}
Или вам может сойти с рук
obj is IComparable
public static bool IsOneOf(object o, params Type[] types)
{
foreach(Type t in types)
{
if(o.GetType() == t) return true;
}
return false;
}
long l = 10;
double d = 10;
string s = "blah";
Console.WriteLine(IsOneOf(l, typeof(long), typeof(double))); // true
Console.WriteLine(IsOneOf(d, typeof(long), typeof(double))); // true
Console.WriteLine(IsOneOf(s, typeof(long), typeof(double))); // false
Создайте вспомогательную функцию для выполнения вашего теста.
Что-то вроде
public static Boolean IsNumeric(Object myObject) {
return (obj is byte || obj is int || obj is long || obj is decimal || obj is double|| obj is float);
}