本文整理汇总了C#中IConvertible.ToDateTime方法的典型用法代码示例。如果您正苦于以下问题:C# IConvertible.ToDateTime方法的具体用法?C# IConvertible.ToDateTime怎么用?C# IConvertible.ToDateTime使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConvertible
的用法示例。
在下文中一共展示了IConvertible.ToDateTime方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetAsIConvertible
public void SetAsIConvertible(IConvertible value) {
Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise
TypeCode tc = value.GetTypeCode();
CultureInfo ci = CultureInfo.CurrentCulture;
switch (tc) {
case TypeCode.Empty: break;
case TypeCode.Object: AsUnknown = value; break;
case TypeCode.DBNull: SetAsNull(); break;
case TypeCode.Boolean: AsBool = value.ToBoolean(ci); break;
case TypeCode.Char: AsUi2 = value.ToChar(ci); break;
case TypeCode.SByte: AsI1 = value.ToSByte(ci); break;
case TypeCode.Byte: AsUi1 = value.ToByte(ci); break;
case TypeCode.Int16: AsI2 = value.ToInt16(ci); break;
case TypeCode.UInt16: AsUi2 = value.ToUInt16(ci); break;
case TypeCode.Int32: AsI4 = value.ToInt32(ci); break;
case TypeCode.UInt32: AsUi4 = value.ToUInt32(ci); break;
case TypeCode.Int64: AsI8 = value.ToInt64(ci); break;
case TypeCode.UInt64: AsI8 = value.ToInt64(ci); break;
case TypeCode.Single: AsR4 = value.ToSingle(ci); break;
case TypeCode.Double: AsR8 = value.ToDouble(ci); break;
case TypeCode.Decimal: AsDecimal = value.ToDecimal(ci); break;
case TypeCode.DateTime: AsDate = value.ToDateTime(ci); break;
case TypeCode.String: AsBstr = value.ToString(ci); break;
default:
throw Assert.Unreachable;
}
}
示例2: ToPrimitive
private static Object ToPrimitive(Object value, PreferredType preferredType, IConvertible ic, TypeCode tcode){
switch (tcode){
case TypeCode.Object:
System.Array arr = value as System.Array;
if (arr != null && arr.Rank == 1) value = new ArrayWrapper(ArrayPrototype.ob, arr, true);
if (value is ScriptObject){
Object result = ((ScriptObject)value).GetDefaultValue(preferredType);
if (Convert.GetTypeCode(result) != TypeCode.Object)
return result;
else if (value == result && preferredType == PreferredType.String || preferredType == PreferredType.LocaleString){
if (value is JSObject){
ScriptObject proto = ((JSObject)value).GetParent();
if (proto is ClassScope)
return ((ClassScope)proto).GetFullName();
return "[object Object]";
}
return value.ToString();
}else
throw new JScriptException(JSError.TypeMismatch);
}else if (value is Missing || value is System.Reflection.Missing)
return null;
else{
IReflect ir;
if (value is IReflect && !(value is Type))
ir = (IReflect)value;
else
ir = value.GetType();
//Look for an op_Explicit conversion to String or Double (this always fails for IDispatch/Ex objects
MethodInfo meth = null;
if (preferredType == PreferredType.String || preferredType == PreferredType.LocaleString)
meth = Convert.GetToXXXXMethod(ir, Typeob.String, true);
else{
meth = Convert.GetToXXXXMethod(ir, Typeob.Double, true);
if (meth == null)
meth = Convert.GetToXXXXMethod(ir, Typeob.Int64, true);
if (meth == null)
meth = Convert.GetToXXXXMethod(ir, Typeob.UInt64, true);
}
if (meth != null){
meth = new JSMethodInfo(meth);
return meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null);
}
//Invoke the default method/property or get the value of the default field. If an exception is thrown
//because the target doesn't have a non-paramterized default member, mask it and execute the
//default handling.
try{
try{
MemberInfo member = LateBinding.SelectMember(JSBinder.GetDefaultMembers(ir));
if (member != null){
switch(member.MemberType){
case MemberTypes.Field: return ((FieldInfo)member).GetValue(value);
case MemberTypes.Method: return ((MethodInfo)member).Invoke(value, new Object[0]);
case MemberTypes.Property: return JSProperty.GetValue((PropertyInfo)member, value, null);
case MemberTypes.Event: return null;
case MemberTypes.NestedType: return member;
}
}
return ir.InvokeMember(String.Empty, BindingFlags.ExactBinding|BindingFlags.SuppressChangeType|
BindingFlags.InvokeMethod|BindingFlags.GetProperty|BindingFlags.GetField,
null, value, new Object[0], null, null, new String[0]);
}catch(TargetInvocationException e){
throw e.InnerException;
}
}catch(ArgumentException){
}catch(IndexOutOfRangeException){
}catch(MissingMemberException){
}catch(TargetParameterCountException){
}
if (preferredType != PreferredType.Number)
if (value is Char[])
return new System.String((Char[])value);
else
return value.ToString();
return value;
}
case TypeCode.DateTime:
return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
}
return value;
}
示例3: ToString
internal static String ToString(Object value, PreferredType pref, IConvertible ic, bool explicitOK){
Enum e = value as Enum;
if (e != null) return e.ToString("G");
EnumWrapper ew = value as EnumWrapper;
if (ew != null) return ew.ToString();
TypeCode code = Convert.GetTypeCode(value, ic);
if (pref == PreferredType.LocaleString){
switch (code){
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 d = ic.ToDouble(null);
return d.ToString(d <= -1e+15 || d >= 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 (code){
case TypeCode.Empty: return explicitOK ? "undefined" : null;
case TypeCode.Object: return Convert.ToString(Convert.ToPrimitive(value, pref, ref ic), ic);
case TypeCode.DBNull: return explicitOK ? "null" : null;
case TypeCode.Boolean: return ic.ToBoolean(null) ? "true" : "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.DateTime: return Convert.ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
case TypeCode.Single:
case TypeCode.Double: return Convert.ToString(ic.ToDouble(null));
}
return null; //Should never get here
}
示例4: DefaultToType
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
if (targetType==null) {
throw new ArgumentNullException("targetType");
}
Contract.EndContractBlock();
RuntimeType rtTargetType = targetType as RuntimeType;
if (rtTargetType != null)
{
if (value.GetType() == targetType)
{
return value;
}
if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean])
return value.ToBoolean(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Char])
return value.ToChar(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.SByte])
return value.ToSByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Byte])
return value.ToByte(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int16])
return value.ToInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16])
return value.ToUInt16(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int32])
return value.ToInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32])
return value.ToUInt32(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Int64])
return value.ToInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64])
return value.ToUInt64(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Single])
return value.ToSingle(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Double])
return value.ToDouble(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal])
return value.ToDecimal(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime])
return value.ToDateTime(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.String])
return value.ToString(provider);
if (rtTargetType == ConvertTypes[(int)TypeCode.Object])
return (Object)value;
// Need to special case Enum because typecode will be underlying type, e.g. Int32
if (rtTargetType == EnumType)
return (Enum)value;
if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
if (rtTargetType == ConvertTypes[(int)TypeCode.Empty])
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
}
throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName));
}
示例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: DefaultToType
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
{
Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
Contract.EndContractBlock();
if (value.GetType() == targetType)
return value;
if (targetType == CommonRuntimeTypes.Boolean)
return value.ToBoolean(provider);
if (targetType == CommonRuntimeTypes.Char)
return value.ToChar(provider);
if (targetType == CommonRuntimeTypes.SByte)
return value.ToSByte(provider);
if (targetType == CommonRuntimeTypes.Byte)
return value.ToByte(provider);
if (targetType == CommonRuntimeTypes.Int16)
return value.ToInt16(provider);
if (targetType == CommonRuntimeTypes.UInt16)
return value.ToUInt16(provider);
if (targetType == CommonRuntimeTypes.Int32)
return value.ToInt32(provider);
if (targetType == CommonRuntimeTypes.UInt32)
return value.ToUInt32(provider);
if (targetType == CommonRuntimeTypes.Int64)
return value.ToInt64(provider);
if (targetType == CommonRuntimeTypes.UInt64)
return value.ToUInt64(provider);
if (targetType == CommonRuntimeTypes.Single)
return value.ToSingle(provider);
if (targetType == CommonRuntimeTypes.Double)
return value.ToDouble(provider);
if (targetType == CommonRuntimeTypes.Decimal)
return value.ToDecimal(provider);
if (targetType == CommonRuntimeTypes.DateTime)
return value.ToDateTime(provider);
if (targetType == CommonRuntimeTypes.String)
return value.ToString(provider);
if (targetType == CommonRuntimeTypes.Object)
return (Object)value;
if (targetType == CommonRuntimeTypes.Enum)
return (Enum)value;
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, value.GetType().ToString(), targetType.Name));
}
示例7: JScriptStrictEquals
//.........这里部分代码省略.........
case TypeCode.Decimal:
return (((decimal) num9) == ic2.ToDecimal(null));
}
return false;
}
case TypeCode.Double:
{
double num10 = ic1.ToDouble(null);
switch (t2)
{
case TypeCode.Char:
return (num10 == ((double) 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 (num10 == ic2.ToInt64(null));
case TypeCode.UInt64:
return (num10 == ic2.ToUInt64(null));
case TypeCode.Single:
return (((float) num10) == ic2.ToSingle(null));
case TypeCode.Double:
return (num10 == ic2.ToDouble(null));
case TypeCode.Decimal:
return (((decimal) num10) == ic2.ToDecimal(null));
}
return false;
}
case TypeCode.Decimal:
{
decimal num11 = ic1.ToDecimal(null);
switch (t2)
{
case TypeCode.Char:
return (num11 == 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 (num11 == ic2.ToInt64(null));
case TypeCode.UInt64:
return (num11 == ic2.ToUInt64(null));
case TypeCode.Single:
return (num11 == ((decimal) ic2.ToSingle(null)));
case TypeCode.Double:
return (num11 == ((decimal) ic2.ToDouble(null)));
case TypeCode.Decimal:
return (num11 == ic2.ToDecimal(null));
}
return false;
}
case TypeCode.DateTime:
if (t2 != TypeCode.DateTime)
{
return false;
}
return (ic1.ToDateTime(null) == ic2.ToDateTime(null));
case TypeCode.String:
{
if (t2 != TypeCode.Char)
{
if (t2 != TypeCode.String)
{
return false;
}
if (v1 != v2)
{
return ic1.ToString(null).Equals(ic2.ToString(null));
}
return true;
}
string str2 = ic1.ToString(null);
if (str2.Length != 1)
{
return false;
}
return (str2[0] == ic2.ToChar(null));
}
default:
return false;
}
return false;
}
示例8: 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);
}
}
//.........这里部分代码省略.........
示例9: 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));
}
示例10: JScriptStrictEquals
//.........这里部分代码省略.........
switch (t2){
case TypeCode.Char: return l1 == 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 l1 == ic2.ToInt64(null);
case TypeCode.UInt64: return l1 >= 0 && ((UInt64)l1) == ic2.ToUInt64(null);
case TypeCode.Single: return l1 == ic2.ToSingle(null);
case TypeCode.Double: return l1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)l1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.UInt64:
UInt64 ul1 = ic1.ToUInt64(null);
switch (t2){
case TypeCode.Char: return ul1 == ic2.ToChar(null);
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
l1 = ic2.ToInt64(null);
return l1 >= 0 && ul1 == (UInt64)l1;
case TypeCode.UInt64: return ul1 == ic2.ToUInt64(null);
case TypeCode.Single: return ul1 == ic2.ToSingle(null);
case TypeCode.Double: return ul1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)ul1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.Single:
Single f1 = ic1.ToSingle(null);
switch (t2){
case TypeCode.Char: return f1 == 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 f1 == ic2.ToInt64(null);
case TypeCode.UInt64: return f1 == ic2.ToUInt64(null);
case TypeCode.Single: return f1 == ic2.ToSingle(null);
case TypeCode.Double: return f1 == ic2.ToSingle(null);
case TypeCode.Decimal: return ((Decimal)f1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.Double:
Double d1 = ic1.ToDouble(null);
switch (t2){
case TypeCode.Char: return d1 == 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 d1 == ic2.ToInt64(null);
case TypeCode.UInt64: return d1 == ic2.ToUInt64(null);
case TypeCode.Single: return ((float)d1) == ic2.ToSingle(null);
case TypeCode.Double: return d1 == ic2.ToDouble(null);
case TypeCode.Decimal: return ((Decimal)d1) == ic2.ToDecimal(null);
}
return false;
case TypeCode.Decimal:
Decimal de1 = ic1.ToDecimal(null);
switch (t2){
case TypeCode.Char: return de1 == (Decimal)(int)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 de1 == ic2.ToInt64(null);
case TypeCode.UInt64: return de1 == ic2.ToUInt64(null);
case TypeCode.Single: return de1 == (Decimal)ic2.ToSingle(null);
case TypeCode.Double: return de1 == (Decimal)ic2.ToDouble(null);
case TypeCode.Decimal: return de1 == ic2.ToDecimal(null);
}
return false;
case TypeCode.DateTime: return t2 == TypeCode.DateTime && ic1.ToDateTime(null) == ic2.ToDateTime(null);
case TypeCode.String:
if (t2 == TypeCode.Char){
String str = ic1.ToString(null);
return str.Length == 1 && str[0] == ic2.ToChar(null);
}
return t2 == TypeCode.String && (v1 == v2 || ic1.ToString(null).Equals(ic2.ToString(null)));
}
return false; //should never get here
}
示例11: 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;
}
示例12: ToPrimitive
private static object ToPrimitive(object value, PreferredType preferredType, IConvertible ic, TypeCode tcode)
{
IReflect reflect;
TypeCode code = tcode;
if (code != TypeCode.Object)
{
if (code == TypeCode.DateTime)
{
return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
}
return value;
}
Array array = value as Array;
if ((array != null) && (array.Rank == 1))
{
value = new ArrayWrapper(ArrayPrototype.ob, array, true);
}
if (value is ScriptObject)
{
object defaultValue = ((ScriptObject) value).GetDefaultValue(preferredType);
if (GetTypeCode(defaultValue) != TypeCode.Object)
{
return defaultValue;
}
if (((value != defaultValue) || (preferredType != PreferredType.String)) && (preferredType != PreferredType.LocaleString))
{
throw new JScriptException(JSError.TypeMismatch);
}
if (!(value is JSObject))
{
return value.ToString();
}
ScriptObject parent = ((JSObject) value).GetParent();
if (parent is ClassScope)
{
return ((ClassScope) parent).GetFullName();
}
return "[object Object]";
}
if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing))
{
return null;
}
if ((value is IReflect) && !(value is Type))
{
reflect = (IReflect) value;
}
else
{
reflect = value.GetType();
}
MethodInfo method = null;
if ((preferredType == PreferredType.String) || (preferredType == PreferredType.LocaleString))
{
method = GetToXXXXMethod(reflect, typeof(string), true);
}
else
{
method = GetToXXXXMethod(reflect, typeof(double), true);
if (method == null)
{
method = GetToXXXXMethod(reflect, typeof(long), true);
}
if (method == null)
{
method = GetToXXXXMethod(reflect, typeof(ulong), true);
}
}
if (method != null)
{
method = new JSMethodInfo(method);
return method.Invoke(null, BindingFlags.SuppressChangeType, null, new object[] { value }, null);
}
try
{
try
{
MemberInfo info2 = LateBinding.SelectMember(JSBinder.GetDefaultMembers(Runtime.TypeRefs, reflect));
if (info2 != null)
{
switch (info2.MemberType)
{
case MemberTypes.Property:
return JSProperty.GetValue((PropertyInfo) info2, value, null);
case MemberTypes.NestedType:
return info2;
case MemberTypes.Event:
return null;
case MemberTypes.Field:
return ((FieldInfo) info2).GetValue(value);
case MemberTypes.Method:
return ((MethodInfo) info2).Invoke(value, new object[0]);
}
}
if (value == reflect)
{
//.........这里部分代码省略.........