本文整理匯總了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;
}