将 int 转换为可为空的 int
本文关键字:int 转换 | 更新日期: 2023-09-27 18:26:07
我需要知道如何将 int 转换为可为空的 int。但是,我不断收到错误"二进制运算符等于未为类型'System.Nullable'1[System.Int32]'和'System.Int32'定义。任何解决方案。它需要Microsoft SQL Server 的可为空的 int 类型。
somevalue = Expression.Constant(something.GetValue(some,null).To<Nullable<System.Int32>> ());
public static T To<T>(this object obj)
{
Type t = typeof(T);
Type u = Nullable.GetUnderlyingType(t);
if (u != null)
{
if (obj == null)
return default(T);
return (T)Convert.ChangeType(obj, u);
}
else
{
return (T)Convert.ChangeType(obj, t);
}
}'
通常,使用强制转换将int
转换为int?
。
int? myNullable = (int?) 15;
int myInt = (int) myNullable;
To
代码似乎是您在给定不可空类型的值时尝试构造可空类型的Constant
,但这根本不是正确的方法。您尝试执行此操作的方式表明您对盒装值类型的工作原理存在误解。
错误消息指示您正在构造一个二元运算符表达式树节点,该节点的操作数为一个可为 null int 类型的表达式节点和一个 int 类型的表达式节点。这是不合法的;它们必须是可为空的 int。您应该做的是将不可为空的 int 表达式树节点包装在Convert
表达式树节点中,该节点将其转换为可为 null 的 int,然后将其传递给二元运算符表达式树节点构造函数。
也就是说,这是错误的:
var someIntExpr = Expression.Constant(123, typeof(int));
var someNubIntExpr = Expression.Constant(null, typeof(int?));
var badEq = Expression.Equal(someIntExpr, someNubIntExpr);
这是正确的:
var goodEq = Expression.Equal(Expression.Convert(someIntExpr, typeof(int?)), someNubIntExpr);
那么,为什么你做错了什么呢?
您有一个返回T
的方法To<T>
。它正确地接受int
并返回等效的int?
。 那又如何?你把它传递给Expression.Constant
,它会将可空的 int 装箱成一个带框的 int,然后用它做一个常量。 您相信存在盒装可为空值类型之类的东西,但没有! 可为 null 的值类型框为空引用或装箱的不可为空值类型。
所以你也可以通过一开始就不做任何这些疯狂的事情来解决你的问题。 如果您手头有一个带盒的 int,并且需要一个可为 null 类型的常量表达式树节点,只需提供类型即可。
Expression.Constant(someBoxedIntValue, typeof(int?))
做。 所以:总结一下,你有两个解决方案:
- 如果您手头有一个带盒的 int,请将其和所需的可为 null 的值类型传递给
Constant
工厂,或者 - 如果您手头有一个 int 类型的表达式节点,请使用
Convert
表达式节点工厂,并将其和所需的类型传递给该节点。
两者都会给你一个正确类型的表达式节点,以便与另一个可为 null 的 int 进行比较。
int test = 0; // set int
int? num = test; // convert test to a nullable int
num = null; // set num as null
int i = 1;
int? k;
k = i as int?;
像这样,您将i
int 转换为可为空的 int ;)
int?
是Nullable<int>
的简短版本。
像这样简单的东西不起作用吗?
int i;
int? temp = int.TryParse(<your value>, out i) ? (int?)i : null;
你去吧。可为空的基元解决方案的泛型字符串。
int? n = " 99 ".ToNullable<int>();
/// <summary>
/// Developed by Taylor Love
/// </summary>
public static class ToNullableStringExtension
{
/// <summary>
/// <para>More convenient than using T.TryParse(string, out T).
/// Works with primitive types, structs, and enums.
/// Tries to parse the string to an instance of the type specified.
/// If the input cannot be parsed, null will be returned.
/// </para>
/// <para>
/// If the value of the caller is null, null will be returned.
/// So if you have "string s = null;" and then you try "s.ToNullable...",
/// null will be returned. No null exception will be thrown.
/// </para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="p_self"></param>
/// <returns></returns>
public static T? ToNullable<T>(this string p_self) where T : struct
{
if (!string.IsNullOrEmpty(p_self))
{
var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
if (converter.IsValid(p_self)) return (T)converter.ConvertFromString(p_self);
if (typeof(T).IsEnum) { T t; if (Enum.TryParse<T>(p_self, out t)) return t;}
}
return null;
}
https://github.com/Pangamma/PangammaUtilities-CSharp/tree/master/src/StringExtensions