如何判断对象内部是否存在sbyte类型?
How to determine if there is an sbyte type inside the object?
如何判断对象内部是否存在sbyte类型?
void Do() {
object obj = func();
bool isSByte = obj.GetType() is sbyte; //not correct
}
object func() {
object obj = new sbyte(-124);
return obj;
}
是(此变体更可取)
bool isSByte = obj is sbyte;
获取类型
bool isSByte = obj.GetType() == typeof(sbyte);
检查继承:
// sbyte
bool isInherits = typeof(sbyte).IsAssignableFrom(obj.GetType()); // true
// for classes
bool isMemoryStreamInheritsStream = typeof(Stream).IsAssignableFrom(typeof(MemoryStream)); // true (MemoryStream based on Stream)
如何判断对象内部是否存在sbyte类型?
void Do() {
object obj = func();
bool isSByte = obj.GetType() is sbyte; //not correct
}
object func() {
object obj = new sbyte(-124);
return obj;
}
是(此变体更可取)
bool isSByte = obj is sbyte;
获取类型
bool isSByte = obj.GetType() == typeof(sbyte);
检查继承:
// sbyte
bool isInherits = typeof(sbyte).IsAssignableFrom(obj.GetType()); // true
// for classes
bool isMemoryStreamInheritsStream = typeof(Stream).IsAssignableFrom(typeof(MemoryStream)); // true (MemoryStream based on Stream)