当前位置: 首页>>代码示例>>C#>>正文


C# IConvertible.ToType方法代码示例

本文整理汇总了C#中IConvertible.ToType方法的典型用法代码示例。如果您正苦于以下问题:C# IConvertible.ToType方法的具体用法?C# IConvertible.ToType怎么用?C# IConvertible.ToType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConvertible的用法示例。


在下文中一共展示了IConvertible.ToType方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetProperty

        public void SetProperty(string property, IConvertible prop_val)
        {
            PropertyInfo prop_info = item.GetType ().GetProperty (property);

            if (prop_info == null)
                throw new ApplicationException (String.Format (
                    "Unable to set property {0}.{1} to \"{2}\"",
                    item.GetType (), property, prop_val));

            Type prop_type = prop_info.PropertyType;
            object param;

            try {
                param = prop_val.ToType (prop_type, NumberFormatInfo.InvariantInfo);
            } catch (FormatException e) {
                throw new ApplicationException (String.Format (
                    "prop_val cannot be converted to a {0}", prop_type), e);
            }

            prop_info.SetValue (item, param, null);
        }
开发者ID:manicolosi,项目名称:questar,代码行数:21,代码来源:ItemBuilder.cs

示例2: 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);
					}
				}
//.........这里部分代码省略.........
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:101,代码来源:Convert.cs

示例3: Case

			public void Case(IConvertible value)
			{
				bool duplicate;

				// make sure the value is of the governing type
				IComparable val = value == null ? null : (IComparable)value.ToType(govType, System.Globalization.CultureInfo.InvariantCulture);

				if (value == null)
					duplicate = defaultExists;
				else
					duplicate = cases.ContainsKey(val);

				if (duplicate)
					throw new InvalidOperationException(Properties.Messages.ErrDuplicateCase);

				if (g.reachable)
					g.il.Emit(OpCodes.Br, lbEnd);

				EndScope();
				Label lb = g.il.DefineLabel();
				g.il.MarkLabel(lb);
				if (value == null)
				{
					defaultExists = true;
					lbDefault = lb;
				}
				else
				{
					cases[val] = lb;
				}
				g.reachable = true;
			}
开发者ID:amithasan,项目名称:Framework-Class-Library-Extension,代码行数:32,代码来源:CodeGen.Statements.cs

示例4: SetValue

 protected virtual bool SetValue(object instance, PropertyInfo property, IConvertible oValue)
 {
     try {
         object convertedValue = oValue.ToType(property.PropertyType, CultureInfo.CurrentCulture);
         return SetValue(instance, property, convertedValue);
     }
     catch {
         return false;
     }
 }
开发者ID:JeremySkinner,项目名称:FluentLinqToSql,代码行数:10,代码来源:NameValueDeserializer.cs


注:本文中的IConvertible.ToType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。