本文整理汇总了C#中IConvertible.ToBoolean方法的典型用法代码示例。如果您正苦于以下问题:C# IConvertible.ToBoolean方法的具体用法?C# IConvertible.ToBoolean怎么用?C# IConvertible.ToBoolean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConvertible
的用法示例。
在下文中一共展示了IConvertible.ToBoolean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToBoolean
private static bool ToBoolean (object value, bool explicitConversion, IConvertible convertible)
{
TypeCode preferredType = GetTypeCode (value, convertible);
switch (preferredType) {
//undefined & null
case TypeCode.Empty:
case TypeCode.DBNull:
return false;
case TypeCode.Boolean:
return convertible.ToBoolean (null);
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
double d = convertible.ToDouble (null);
return ((d != 0.0) && !double.IsNaN (d));
case TypeCode.String:
string str = convertible.ToString ();
return str.Length != 0;
case TypeCode.Object:
return true;
//TODO datetime find behaviour maybe as a number
case TypeCode.DateTime:
return true;
}
throw new NotImplementedException ();
}
示例2: DefaultToType
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
BCLDebug.Assert(value!=null, "[Convert.DefaultToType]value!=null");
if (targetType==null) {
throw new ArgumentNullException("targetType");
}
if (value.GetType()==targetType) {
return value;
}
if (targetType==ConvertTypes[(int)TypeCode.Boolean])
return value.ToBoolean(provider);
if (targetType==ConvertTypes[(int)TypeCode.Char])
return value.ToChar(provider);
if (targetType==ConvertTypes[(int)TypeCode.SByte])
return value.ToSByte(provider);
if (targetType==ConvertTypes[(int)TypeCode.Byte])
return value.ToByte(provider);
if (targetType==ConvertTypes[(int)TypeCode.Int16])
return value.ToInt16(provider);
if (targetType==ConvertTypes[(int)TypeCode.UInt16])
return value.ToUInt16(provider);
if (targetType==ConvertTypes[(int)TypeCode.Int32])
return value.ToInt32(provider);
if (targetType==ConvertTypes[(int)TypeCode.UInt32])
return value.ToUInt32(provider);
if (targetType==ConvertTypes[(int)TypeCode.Int64])
return value.ToInt64(provider);
if (targetType==ConvertTypes[(int)TypeCode.UInt64])
return value.ToUInt64(provider);
if (targetType==ConvertTypes[(int)TypeCode.Single])
return value.ToSingle(provider);
if (targetType==ConvertTypes[(int)TypeCode.Double])
return value.ToDouble(provider);
if (targetType==ConvertTypes[(int)TypeCode.Decimal])
return value.ToDecimal(provider);
if (targetType==ConvertTypes[(int)TypeCode.DateTime])
return value.ToDateTime(provider);
if (targetType==ConvertTypes[(int)TypeCode.String]) {
return value.ToString(provider);
}
if (targetType==ConvertTypes[(int)TypeCode.Object])
return (Object)value;
if (targetType==ConvertTypes[(int)TypeCode.DBNull])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
if (targetType==ConvertTypes[(int)TypeCode.Empty])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), value.GetType().FullName, targetType.FullName));
}
示例3: ToUint32
internal static uint ToUint32(Object value, IConvertible ic){
switch (Convert.GetTypeCode(value, ic)){
case TypeCode.Empty: return 0;
case TypeCode.DBNull: return 0;
case TypeCode.Boolean: return ic.ToBoolean(null) ? (uint)1 : (uint)0;
case TypeCode.Char: return (uint)ic.ToChar(null);
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32: return ic.ToUInt32(null);
case TypeCode.UInt64: return (uint)ic.ToUInt64(null);
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64: return (uint)ic.ToInt64(null);
case TypeCode.Single: return (uint)ic.ToSingle(null);
case TypeCode.Double:
case TypeCode.Decimal: return (uint)ic.ToDouble(null);
case TypeCode.Object:
case TypeCode.DateTime:
Object pval = Convert.ToPrimitive(value, PreferredType.Number, ref ic);
if (pval != value)
return Convert.ToUint32(pval, ic);
else
return 0;
case TypeCode.String: return (uint)Convert.ToNumber(ic.ToString(null));
}
return 0; //should never get here
}
示例4: InternalNegObj
private static object InternalNegObj(object obj, IConvertible conv, TypeCode tc)
{
switch (tc)
{
case TypeCode.Empty:
return 0;
case TypeCode.Boolean:
if (obj is bool)
{
return -((short) -(((bool) obj) > false));
}
return -((short) -(conv.ToBoolean(null) > false));
case TypeCode.Byte:
if (obj is byte)
{
return (short) -((byte) obj);
}
return (short) -conv.ToByte(null);
case TypeCode.Int16:
int num4;
if (obj is short)
{
num4 = 0 - ((short) obj);
}
else
{
num4 = 0 - conv.ToInt16(null);
}
if ((num4 >= -32768) && (num4 <= 0x7fff))
{
return (short) num4;
}
return num4;
case TypeCode.Int32:
long num5;
if (obj is int)
{
num5 = 0L - ((int) obj);
}
else
{
num5 = 0L - conv.ToInt32(null);
}
if ((num5 >= -2147483648L) && (num5 <= 0x7fffffffL))
{
return (int) num5;
}
return num5;
case TypeCode.Int64:
try
{
if (obj is long)
{
return (0L - ((long) obj));
}
return (0L - conv.ToInt64(null));
}
catch (StackOverflowException exception)
{
throw exception;
}
catch (OutOfMemoryException exception2)
{
throw exception2;
}
catch (ThreadAbortException exception3)
{
throw exception3;
}
catch (Exception)
{
return decimal.Negate(conv.ToDecimal(null));
}
break;
case TypeCode.Single:
goto Label_01B9;
case TypeCode.Double:
if (obj is double)
{
return -((double) obj);
}
return -conv.ToDouble(null);
case TypeCode.Decimal:
break;
case TypeCode.String:
{
string str = obj as string;
if (str == null)
{
return -DoubleType.FromString(conv.ToString(null));
}
//.........这里部分代码省略.........
示例5: DefaultToType
internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
{
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
RuntimeType left = targetType as RuntimeType;
if (left != null)
{
if (value.GetType() == targetType)
{
return value;
}
if (left == Convert.ConvertTypes[3])
{
return value.ToBoolean(provider);
}
if (left == Convert.ConvertTypes[4])
{
return value.ToChar(provider);
}
if (left == Convert.ConvertTypes[5])
{
return value.ToSByte(provider);
}
if (left == Convert.ConvertTypes[6])
{
return value.ToByte(provider);
}
if (left == Convert.ConvertTypes[7])
{
return value.ToInt16(provider);
}
if (left == Convert.ConvertTypes[8])
{
return value.ToUInt16(provider);
}
if (left == Convert.ConvertTypes[9])
{
return value.ToInt32(provider);
}
if (left == Convert.ConvertTypes[10])
{
return value.ToUInt32(provider);
}
if (left == Convert.ConvertTypes[11])
{
return value.ToInt64(provider);
}
if (left == Convert.ConvertTypes[12])
{
return value.ToUInt64(provider);
}
if (left == Convert.ConvertTypes[13])
{
return value.ToSingle(provider);
}
if (left == Convert.ConvertTypes[14])
{
return value.ToDouble(provider);
}
if (left == Convert.ConvertTypes[15])
{
return value.ToDecimal(provider);
}
if (left == Convert.ConvertTypes[16])
{
return value.ToDateTime(provider);
}
if (left == Convert.ConvertTypes[18])
{
return value.ToString(provider);
}
if (left == Convert.ConvertTypes[1])
{
return value;
}
if (left == Convert.EnumType)
{
return (Enum)value;
}
if (left == Convert.ConvertTypes[2])
{
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
}
if (left == Convert.ConvertTypes[0])
{
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
}
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
{
value.GetType().FullName,
targetType.FullName
}));
}
示例6: ToBoolean
internal static bool ToBoolean(Object value, IConvertible ic){
switch (Convert.GetTypeCode(value, ic)){
case TypeCode.Empty: return false;
case TypeCode.Object:
if (value is Missing || value is System.Reflection.Missing) return false;
Type t = value.GetType();
MethodInfo meth = t.GetMethod("op_True", BindingFlags.ExactBinding|BindingFlags.Public|BindingFlags.Static, null, new Type[]{t}, null);
if (meth != null && (meth.Attributes & MethodAttributes.SpecialName) != 0 && meth.ReturnType == Typeob.Boolean){
meth = new JSMethodInfo(meth);
return (bool)meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null);
}
return true;
case TypeCode.DBNull: return false;
case TypeCode.Boolean: return ic.ToBoolean(null);
case TypeCode.Char: return ic.ToChar(null) != (Char)0;
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32: return ic.ToInt32(null) != 0;
case TypeCode.UInt32:
case TypeCode.Int64: return ic.ToInt64(null) != 0;
case TypeCode.UInt64: return ic.ToUInt64(null) != 0;
case TypeCode.Single:
case TypeCode.Double:
double d = ic.ToDouble(null);
if (d != d) return false; else return d != 0;
case TypeCode.Decimal: return ic.ToDecimal(null) != (Decimal)0;
case TypeCode.DateTime: return true;
case TypeCode.String: return ic.ToString(null).Length != 0;
}
return false; //should never get here
}
示例7: ToUint32
internal static uint ToUint32(object value, IConvertible ic)
{
switch (GetTypeCode(value, ic))
{
case TypeCode.Empty:
return 0;
case TypeCode.Object:
case TypeCode.DateTime:
{
object obj2 = ToPrimitive(value, PreferredType.Number, ref ic);
if (obj2 == value)
{
return 0;
}
return ToUint32(obj2, ic);
}
case TypeCode.DBNull:
return 0;
case TypeCode.Boolean:
if (ic.ToBoolean(null))
{
return 1;
}
return 0;
case TypeCode.Char:
return ic.ToChar(null);
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return (uint) ic.ToInt64(null);
case TypeCode.Byte:
case TypeCode.UInt16:
case TypeCode.UInt32:
return ic.ToUInt32(null);
case TypeCode.UInt64:
return (uint) ic.ToUInt64(null);
case TypeCode.Single:
case TypeCode.Double:
return (uint) Runtime.DoubleToInt64(ic.ToDouble(null));
case TypeCode.Decimal:
return (uint) Runtime.UncheckedDecimalToInt64(ic.ToDecimal(null));
case TypeCode.String:
return (uint) Runtime.DoubleToInt64(ToNumber(ic.ToString(null)));
}
return 0;
}
示例8: JScriptStrictEquals
internal static bool JScriptStrictEquals(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects)
{
long num7;
switch (t1)
{
case TypeCode.Empty:
return (t2 == TypeCode.Empty);
case TypeCode.Object:
if (v1 != v2)
{
if ((v1 is Microsoft.JScript.Missing) || (v1 is System.Reflection.Missing))
{
v1 = null;
}
if (v1 == v2)
{
return true;
}
if ((v2 is Microsoft.JScript.Missing) || (v2 is System.Reflection.Missing))
{
v2 = null;
}
if (checkForDebuggerObjects)
{
IDebuggerObject obj2 = v1 as IDebuggerObject;
if (obj2 != null)
{
IDebuggerObject o = v2 as IDebuggerObject;
if (o != null)
{
return obj2.IsEqual(o);
}
}
}
return (v1 == v2);
}
return true;
case TypeCode.DBNull:
return (t2 == TypeCode.DBNull);
case TypeCode.Boolean:
if (t2 != TypeCode.Boolean)
{
return false;
}
return (ic1.ToBoolean(null) == ic2.ToBoolean(null));
case TypeCode.Char:
{
char ch = ic1.ToChar(null);
switch (t2)
{
case TypeCode.Char:
return (ch == ic2.ToChar(null));
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
return (ch == ic2.ToInt64(null));
case TypeCode.UInt64:
return (ch == ic2.ToUInt64(null));
case TypeCode.Single:
case TypeCode.Double:
return (((double) ch) == ic2.ToDouble(null));
case TypeCode.Decimal:
return (ch == ic2.ToDecimal(null));
case TypeCode.String:
{
string str = ic2.ToString(null);
return ((str.Length == 1) && (ch == str[0]));
}
}
break;
}
case TypeCode.SByte:
{
sbyte num = ic1.ToSByte(null);
switch (t2)
{
case TypeCode.Char:
return (num == ic2.ToChar(null));
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
return (num == ic2.ToInt64(null));
//.........这里部分代码省略.........
示例9: ToNumber
internal static double ToNumber(object value, IConvertible ic)
{
switch (GetTypeCode(value, ic))
{
case TypeCode.Empty:
return double.NaN;
case TypeCode.Object:
case TypeCode.DateTime:
{
object obj2 = ToPrimitive(value, PreferredType.Number, ref ic);
if (obj2 == value)
{
return double.NaN;
}
return ToNumber(obj2, ic);
}
case TypeCode.DBNull:
return 0.0;
case TypeCode.Boolean:
return (ic.ToBoolean(null) ? ((double) 1) : ((double) 0));
case TypeCode.Char:
return (double) ic.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
return (double) ic.ToInt32(null);
case TypeCode.UInt32:
case TypeCode.Int64:
return (double) ic.ToInt64(null);
case TypeCode.UInt64:
return (double) ic.ToUInt64(null);
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return ic.ToDouble(null);
case TypeCode.String:
return ToNumber(ic.ToString(null));
}
return 0.0;
}
示例10: ToString
internal static string ToString(object value, PreferredType pref, IConvertible ic, bool explicitOK)
{
Enum enum2 = value as Enum;
if (enum2 != 0)
{
return enum2.ToString("G");
}
EnumWrapper wrapper = value as EnumWrapper;
if (wrapper != null)
{
return wrapper.ToString();
}
TypeCode typeCode = GetTypeCode(value, ic);
if (pref == PreferredType.LocaleString)
{
switch (typeCode)
{
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Single:
case TypeCode.Double:
{
double num = ic.ToDouble(null);
return num.ToString(((num <= -1E+15) || (num >= 1E+15)) ? "g" : "n", NumberFormatInfo.CurrentInfo);
}
case TypeCode.Int64:
return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
case TypeCode.UInt64:
return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
case TypeCode.Decimal:
return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo);
}
}
switch (typeCode)
{
case TypeCode.Empty:
if (explicitOK)
{
return "undefined";
}
return null;
case TypeCode.Object:
return ToString(ToPrimitive(value, pref, ref ic), ic);
case TypeCode.DBNull:
if (explicitOK)
{
return "null";
}
return null;
case TypeCode.Boolean:
if (ic.ToBoolean(null))
{
return "true";
}
return "false";
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Decimal:
case TypeCode.String:
return ic.ToString(null);
case TypeCode.Single:
case TypeCode.Double:
return ToString(ic.ToDouble(null));
case TypeCode.DateTime:
return ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
}
return null;
}
示例11: ToBoolean
internal static bool ToBoolean(object value, IConvertible ic)
{
switch (GetTypeCode(value, ic))
{
case TypeCode.Empty:
return false;
case TypeCode.Object:
{
if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing))
{
return false;
}
Type type = value.GetType();
MethodInfo method = type.GetMethod("op_True", BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Static, null, new Type[] { type }, null);
if (((method != null) && ((method.Attributes & MethodAttributes.SpecialName) != MethodAttributes.PrivateScope)) && (method.ReturnType == typeof(bool)))
{
method = new JSMethodInfo(method);
return (bool) method.Invoke(null, BindingFlags.SuppressChangeType, null, new object[] { value }, null);
}
return true;
}
case TypeCode.DBNull:
return false;
case TypeCode.Boolean:
return ic.ToBoolean(null);
case TypeCode.Char:
return (ic.ToChar(null) != '\0');
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
return (ic.ToInt32(null) != 0);
case TypeCode.UInt32:
case TypeCode.Int64:
return (ic.ToInt64(null) != 0L);
case TypeCode.UInt64:
return (ic.ToUInt64(null) != 0L);
case TypeCode.Single:
case TypeCode.Double:
{
double num = ic.ToDouble(null);
return ((num == num) && !(num == 0.0));
}
case TypeCode.Decimal:
return (ic.ToDecimal(null) != 0M);
case TypeCode.DateTime:
return true;
case TypeCode.String:
return (ic.ToString(null).Length != 0);
}
return false;
}
示例12: ToVBBoolConv
private static IConvertible ToVBBoolConv(IConvertible conv)
{
if (conv.ToBoolean(null))
{
return (IConvertible) (-1);
}
return (IConvertible) 0;
}
示例13: ToVBBool
private static int ToVBBool(IConvertible conv)
{
if (conv.ToBoolean(null))
{
return -1;
}
return 0;
}
示例14: DefaultToType
// Default implementation of the "ToType" methods in
// the primitive classes like Byte, Int32, Boolean, etc.
internal static Object DefaultToType(IConvertible obj, Type targetType,
IFormatProvider provider,
bool recursive)
{
if(targetType != null)
{
if(obj.GetType() == targetType)
{
return obj;
}
else if(targetType == ConvertTypes[(int)TypeCode.Boolean])
{
return (Object)(obj.ToBoolean(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Char])
{
return (Object)(obj.ToChar(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.SByte])
{
return (Object)(obj.ToSByte(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Byte])
{
return (Object)(obj.ToByte(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Int16])
{
return (Object)(obj.ToInt16(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.UInt16])
{
return (Object)(obj.ToUInt16(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Int32])
{
return (Object)(obj.ToInt32(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.UInt32])
{
return (Object)(obj.ToUInt32(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Int64])
{
return (Object)(obj.ToInt64(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.UInt64])
{
return (Object)(obj.ToUInt64(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Single])
{
return (Object)(obj.ToSingle(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Double])
{
return (Object)(obj.ToDouble(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Decimal])
{
return (Object)(obj.ToDecimal(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.DateTime])
{
return (Object)(obj.ToDateTime(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.String])
{
return (Object)(obj.ToString(provider));
}
else if(targetType == ConvertTypes[(int)TypeCode.Object])
{
return obj;
}
else if(targetType == ConvertTypes[(int)TypeCode.Empty])
{
throw new InvalidCastException
(_("InvalidCast_Empty"));
}
else if(targetType == ConvertTypes[(int)TypeCode.DBNull])
{
throw new InvalidCastException
(_("InvalidCast_DBNull"));
}
else if(recursive)
{
throw new InvalidCastException
(String.Format
(_("InvalidCast_FromTo"),
obj.GetType().FullName, targetType.FullName));
}
else
{
// We weren't called from a "ToType" method,
// so we can use it to handle the default case.
return obj.ToType(targetType, provider);
}
}
//.........这里部分代码省略.........
示例15: ExecuteBooleanOperator
private object ExecuteBooleanOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op)
{
bool flag = false;
switch (op)
{
case CodeBinaryOperatorType.IdentityInequality:
return (left != right);
case CodeBinaryOperatorType.IdentityEquality:
return (left == right);
case CodeBinaryOperatorType.ValueEquality:
return left.Equals(right);
case CodeBinaryOperatorType.BitwiseOr:
case CodeBinaryOperatorType.BitwiseAnd:
case CodeBinaryOperatorType.LessThan:
case CodeBinaryOperatorType.LessThanOrEqual:
case CodeBinaryOperatorType.GreaterThan:
case CodeBinaryOperatorType.GreaterThanOrEqual:
return flag;
case CodeBinaryOperatorType.BooleanOr:
return (left.ToBoolean(null) || right.ToBoolean(null));
case CodeBinaryOperatorType.BooleanAnd:
return (left.ToBoolean(null) && right.ToBoolean(null));
}
return flag;
}