本文整理汇总了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);
}
示例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);
}
}
//.........这里部分代码省略.........
示例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;
}
示例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;
}
}