本文整理汇总了C#中System.Type.IsNumeric方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsNumeric方法的具体用法?C# Type.IsNumeric怎么用?C# Type.IsNumeric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsNumeric方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsNumericPromotion
private static bool IsNumericPromotion(Type leftType, Type rightType) {
if (!leftType.IsNumeric() || !rightType.IsNumeric() || leftType.Equals(TypeSystem.Boolean)) {
return false;
}
return true;
}
示例2: IsNumericPromotion
private static bool IsNumericPromotion(Type type, UnaryOperatorType @operator) {
if (!type.IsNumeric() ||
type.Equals(TypeSystem.Boolean) ||
Array.IndexOf(_unaryOperators, @operator) == -1) {
return false;
}
return true;
}
示例3: BuildJsonObject
public static IJsonObject BuildJsonObject(Type type, object instance)
{
if (type.IsNumeric()) {
return new NumericJsonProperty(instance);
}
if (type == typeof (string)) {
return new StringJsonProperty(instance);
}
if (type.GetInterface("IEnumerable") != null) {
return new JsonArray((IEnumerable)instance);
}
return new JsonClass(instance);
}
示例4: GetODSType
private static string GetODSType(Type type)
{
if (type == typeof(DateTime) || type == typeof(DateTime?))
{
return OfficeValueTypes.Date;
}
if (type == typeof(bool) || type == typeof(bool?))
{
return OfficeValueTypes.Boolean;
}
if (type.IsNumeric())
{
return OfficeValueTypes.Float;
}
return OfficeValueTypes.String;
}
示例5: GetCoarseType
public static string GetCoarseType(Type type)
{
if (type.IsDateTime())
return "datetime";
if (type.IsNumeric())
return "numeric";
if (type.IsString())
return "string";
if (type.IsBool())
return "bool";
if (type.IsGuid())
return "guid";
return "complex";
}
示例6: GetCoarseType
public static string GetCoarseType(Type type)
{
if (type.IsTimeSpan())
return "timespan";
if (type.IsDateTime())
return "datetime";
if (type.IsNumeric())
return "numeric";
if (type.IsString())
return "string";
if (type.IsBool())
return "bool";
if (type.IsGuid())
return "guid";
return "object";
}
示例7: GetCoarseType
public static string GetCoarseType(Type type)
{
if (type.IsTimeSpan())
return "timespan";
if (type.IsDateTime())
return "datetime";
if (type.IsNumeric())
return "numeric";
if (type.IsString())
return "string";
if (type.IsBool())
return "bool";
if (type.IsGuid())
return "guid";
type = type.IsNullable() ? Nullable.GetUnderlyingType(type) : type;
return type.Name.ToLowerInvariant();
}
示例8: Convert
/// <summary>
/// Convert the string representation to the destination type
/// </summary>
/// <param name="input">Input string</param>
/// <param name="destinationType">Destination type</param>
/// <param name="context">Current context</param>
/// <returns>Converted object of the destination type</returns>
public object Convert(string input, Type destinationType, BindingContext context)
{
var converter = TypeDescriptor.GetConverter(destinationType);
if (!converter.CanConvertFrom(typeof(string)) || (destinationType.IsNumeric() && string.IsNullOrEmpty(input)))
{
return null;
}
try
{
return converter.ConvertFrom(input);
}
catch (FormatException)
{
if (destinationType == typeof(bool) && converter.GetType() == typeof(BooleanConverter) && "on".Equals(input, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return null;
}
}
示例9: Emit
/// <summary>
/// 写入类型转换的 IL 指令。
/// </summary>
/// <param name="generator">IL 的指令生成器。</param>
/// <param name="inputType">要转换的对象的类型。</param>
/// <param name="outputType">要将输入对象转换到的类型。</param>
/// <param name="isChecked">是否执行溢出检查。</param>
public override void Emit(ILGenerator generator, Type inputType, Type outputType, bool isChecked)
{
Contract.Assume((inputType == typeof(decimal) && outputType.IsNumeric()) ||
(outputType == typeof(decimal) && inputType.IsNumeric()));
MethodInfo method;
if (inputType == typeof(decimal))
{
if (outputType.IsEnum)
{
outputType = Enum.GetUnderlyingType(outputType);
}
method = UserConversionCache.GetConversionTo(inputType, outputType);
}
else
{
if (inputType.IsEnum)
{
inputType = Enum.GetUnderlyingType(inputType);
}
method = UserConversionCache.GetConversionFrom(outputType, inputType);
}
generator.EmitCall(method);
}
示例10: TypesArePrimitiveAndConvertible
private bool TypesArePrimitiveAndConvertible(Type source, Type dest) {
Type nonNullableSourceType = source.GetNonNullableType();
Type nonNullableDestinationType = dest.GetNonNullableType();
return (!(source.IsEnum || dest.IsEnum)) && (source.IsEquivalentTo(dest) || ((source.IsNullableType() && dest.IsEquivalentTo(nonNullableSourceType)) || ((dest.IsNullableType() && source.IsEquivalentTo(nonNullableDestinationType)) ||
((source.IsNumeric() && dest.IsNumeric()) && (nonNullableDestinationType != TypeSystem.Boolean)))));
}
示例11: CanConvertTo
/// <summary>
/// Whether the converter can convert to the destination type
/// </summary>
/// <param name="destinationType">Destination type</param>
/// <param name="context">The current binding context</param>
/// <returns>True if conversion supported, false otherwise</returns>
public bool CanConvertTo(Type destinationType, BindingContext context)
{
return destinationType.IsNumeric();
}
示例12: GetBetweenValueTypeConversion
/// <summary>
/// 返回从值类型转换为值类型的类型转换。
/// </summary>
/// <param name="inputType">要转换的对象的类型。</param>
/// <param name="outputType">要将输入对象转换到的类型。</param>
/// <returns>从值类型转换为值类型的类型转换,如果不存在则为 <c>null</c>。</returns>
private static Conversion GetBetweenValueTypeConversion(Type inputType, Type outputType)
{
Contract.Requires(inputType != null && outputType != null);
Contract.Requires(inputType.IsValueType && outputType.IsValueType);
TypeCode inputTypeCode = Type.GetTypeCode(inputType);
TypeCode outputTypeCode = Type.GetTypeCode(outputType);
// 数值或枚举转换。
if (inputTypeCode.IsNumeric() && outputTypeCode.IsNumeric())
{
return GetNumericOrEnumConversion(inputType, inputTypeCode, outputType, outputTypeCode);
}
// 可空类型转换。
Type inputUnderlyingType = Nullable.GetUnderlyingType(inputType);
Type outputUnderlyingType = Nullable.GetUnderlyingType(outputType);
if (inputUnderlyingType != null)
{
inputTypeCode = Type.GetTypeCode(inputUnderlyingType);
if (outputUnderlyingType == null)
{
// 可空类型 S? 到非可空值类型 T 的转换。
// 1. 可空类型为 null,引发异常。
// 2. 将可空类型解包
// 3. 执行从 S 到 T 的预定义类型转换。
// 这里 S 和 T 都是值类型,可能的预定义类型转换只有标识转换和数值转换。
if (inputUnderlyingType == outputType ||
(inputTypeCode.IsNumeric() && outputTypeCode.IsNumeric()))
{
return FromNullableConversion.Default;
}
return null;
}
outputTypeCode = Type.GetTypeCode(outputUnderlyingType);
// 可空类型间转换,从 S? 到 T?,与上面同理,但此时不可能有 S==T。
if (inputTypeCode.IsNumeric() && outputTypeCode.IsNumeric())
{
Conversion conversion = GetNumericOrEnumConversion(inputUnderlyingType, inputTypeCode,
outputUnderlyingType, outputTypeCode);
return conversion.ConversionType.IsImplicit()
? BetweenNullableConversion.Implicit : BetweenNullableConversion.Explicit;
}
return null;
}
if (outputUnderlyingType != null)
{
// 非可空类型到可空类型转换,与上面同理。
if (inputType == outputUnderlyingType)
{
return ToNullableConversion.Implicit;
}
outputTypeCode = Type.GetTypeCode(outputUnderlyingType);
if (inputType.IsNumeric() && outputTypeCode.IsNumeric())
{
Conversion conversion = GetNumericOrEnumConversion(inputType, inputTypeCode,
outputUnderlyingType, outputTypeCode);
return conversion.ConversionType.IsImplicit() ?
ToNullableConversion.Implicit : ToNullableConversion.Explicit;
}
}
return null;
}
示例13: CanMatchType
public override bool CanMatchType(Type requestedType)
{
return requestedType == null || requestedType.IsNumeric() || requestedType.GetTypeCode() == TypeCode.Boolean;
}
示例14: TryChangeType
internal bool TryChangeType(ref object value, Type type)
{
if (type != null && type != typeof(object))
{
// handle nullable types
if (type.IsNullableType())
{
// if value is null, we're done
if (value == null || object.Equals(value, string.Empty))
{
value = null;
return true;
}
// get actual type for parsing
type = Nullable.GetUnderlyingType(type);
}
else if (type.GetTypeInfo().IsValueType && value == null)
{
// not nullable, can't assign null value to this
return false;
}
// handle special numeric formatting
var ci = GetCultureInfo();
var str = value as string;
if (!string.IsNullOrEmpty(str) && type.IsNumeric())
{
// handle percentages (ci.NumberFormat.PercentSymbol? overkill...)
bool pct = str[0] == '%' || str[str.Length - 1] == '%';
if (pct)
{
str = str.Trim('%');
}
decimal d;
if (decimal.TryParse(str, NumberStyles.Any, ci, out d))
{
if (pct)
{
value = d / 100;
}
else
{
// <<IP>> for currencies Convert.ChangeType will always give exception if we do without parsing,
// so change value here to the parsed one
value = d;
}
}
else
{
return false;
}
}
// ready to change type
try
{
value = Convert.ChangeType(value, type, ci);
}
catch
{
return false;
}
}
return true;
}
示例15: CreateFieldFrom
/// <summary>
/// Creates a field that will contain a value of a specific type
/// <para xml:lang="es">
/// Crea un campo que contendra un valor de un tipo especifico.
/// </para>
/// </summary>
public static FormField CreateFieldFrom(Type type)
{
//validate arguments
if (type == null) throw new ArgumentNullException("type");
//field
FormField field;
//Enum
if (type.GetTypeInfo().IsEnum)
{
field = new EnumField(type);
}
//Type, ignore this since we can't know if type means Person (as in a serializable object) or typeof(Person) as a type which child types you would choose from
//else if (type.Equals(typeof(Type)))
//{
// field = new TypeField();
//}
//Bool
else if (type.Equals(typeof(bool)))
{
field = new BoolField();
}
//DateTime
else if (type.Equals(typeof(DateTime)))
{
field = new DateTimeField();
}
//Numeric
else if (type.IsNumeric())
{
if (type.IsIntegral())
{
field = new IntegerField();
}
else
{
field = new DecimalField();
}
}
//String serializable
else if (type.GetTypeInfo().ImplementedInterfaces.Contains(typeof(Data.IStringSerializable)))
{
field = new StringSerializableField(type);
}
//XML
else if (type.GetTypeInfo().ImplementedInterfaces.Contains(typeof(System.Xml.Serialization.IXmlSerializable)))
{
field = new XmlSerializableField(type);
}
//String
else if (type.Equals(typeof(string)))
{
field = new StringField();
}
//byte[]
else if (type.Equals(typeof(byte[])))
{
field = new BinaryField();
}
//otherwise just create a textbox
else
{
field = new StringField();
}
//return
return field;
}