gpt4 book ai didi

c# - 比较两种类型

转载 作者:太空狗 更新时间:2023-10-29 17:37:20 24 4
gpt4 key购买 nike

我有一个类看起来很像这样:

object value;
Type type;

当我创建对象时,我将类型设置为对象的类型。

我如何将这种类型与另一种类型进行比较?

例如,如果类型是 String:

type.Equals(String)

type == String 

不起作用。

最佳答案

在此上下文中,您将 Type 实例与 typeof(T) 的结果进行比较,其中 T 是您想要的类型相比。

bool objectIsString = myType == typeof(string);

关于c# - 比较两种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com