C# 如何区分C语言中typeof(int)==typeof(int?)之间的区别#

C# 如何区分C语言中typeof(int)==typeof(int?)之间的区别#,c#,C#,为什么C#将这些设置为相等 typeof(int).GetType() == typeof(int?).GetType() 在我将 List<int?> ids = JsonConvert.DeserializeObject<List<int?>>(filter.Value?.ToString()); var filterField = filter.PropertyName; var method = ids.GetType().GetMethod("Co

为什么C#将这些设置为相等

typeof(int).GetType() == typeof(int?).GetType()
在我将

List<int?> ids = JsonConvert.DeserializeObject<List<int?>>(filter.Value?.ToString());
var filterField = filter.PropertyName;
var method = ids.GetType().GetMethod("Contains");
return Expression.Call(Expression.Constant(ids), method, member);
为什么C#将这些设置为相等

typeof(int).GetType() == typeof(int?).GetType()
因为他们是平等的

typeof(int)
由编译器生成一个
RuntimeType
实例

typeof(int?
由编译器生成不同的
RuntimeType
实例

在任何
RuntimeType
实例上调用
GetType()
将返回类型
System.RuntimeType

我想你想要

typeof(int) == typeof(int?)

证明:

Console.WriteLine(typeof(int));
Console.WriteLine(typeof(int?));
Console.WriteLine(typeof(int).GetType());
Console.WriteLine(typeof(int?).GetType());
输出:

System.Int32
System.Nullable`1[System.Int32]
System.RuntimeType
System.RuntimeType

typeof(X)
操作符始终返回表示类型
X
Type
对象。
GetType()
方法返回调用它的对象的运行时类型。因此,如果使用表达式
typeof(X).GetType()
表达式的第一部分将始终返回
Type
实例,而该表达式的第二部分将始终返回表示类型
Type
对象,无论
X
是什么。您想比较
typeof(int)
typeof(int?
,两者不同。

我认为,表达式树的错误在于
成员
变量是
类型
int
表达式
,而不是
int?
。 您发布的代码没有显示它的来源,但我认为以下内容将对您有所帮助:

return Expression.Call(Expression.Constant(ids), method, Expression.Convert(member, typeof(int?)));

在这里搜索答案并尝试一切后,找到了这个

bool isIntNull = member.Type.IsGenericType && member.Type.GetGenericTypeDefinition() == typeof(Nullable<>);
bool isIntNull=member.Type.IsGenericType&&member.Type.GetGenericTypeDefinition()==typeof(可为null);

我在将未知运行时数据类型的数据提取到已知数据类型时遇到了同样的问题-我通过这种方式解决了这个问题

public bool CompareDataType<T>(Type runtimedatatype)
{
    Type KT = typeof(T);

    return runtimedatatype.Equals(KT) || runtimedatatype.Equals(Nullable.GetUnderlyingType(KT));
}

int? output = null;
object RunTimeData = (object)((int)0);
if (CompareDataType<int?>(RunTimeData.GetType()))
    output = (int?)RunTimeData;
public bool CompareDataType(类型runtimedatatype)
{
KT型=T型;
返回runtimedatatype.Equals(KT)| runtimedatatype.Equals(null.getUnderlineType(KT));
}
智力?输出=空;
对象运行时数据=(对象)((int)0);
if(CompareDataType(RunTimeData.GetType()))
输出=(int?)运行时数据;
或者,您可以创建Object的扩展(这就是我最后提到的)

公共静态类ObjectTypeIsEqual
{
公共静态bool CompareDataType(此对象输入)
{
类型ObjectType=input.GetType();
类型CompareType=typeof(T);
返回ObjectType.Equals(CompareType)| | ObjectType.Equals(Nullable.GetUnderlyingType(CompareType));
}
}
智力?输出=空;
对象运行时数据=(对象)((int)0);
if(RunTimeData.CompareDataType())
输出=(int?)运行时数据;
public bool CompareDataType<T>(Type runtimedatatype)
{
    Type KT = typeof(T);

    return runtimedatatype.Equals(KT) || runtimedatatype.Equals(Nullable.GetUnderlyingType(KT));
}

int? output = null;
object RunTimeData = (object)((int)0);
if (CompareDataType<int?>(RunTimeData.GetType()))
    output = (int?)RunTimeData;
public static class ObjectTypeIsEqual
{
    public static bool CompareDataType<T>(this object input)
    {
        Type ObjectType = input.GetType();
        Type CompareType = typeof(T);

        return ObjectType.Equals(CompareType) || ObjectType.Equals(Nullable.GetUnderlyingType(CompareType));
    }
}

int? output = null;
object RunTimeData = (object)((int)0);
if (RunTimeData.CompareDataType<int?>())
    output = (int?)RunTimeData;