比较两种类型 [英] Compare two Types

查看:55
本文介绍了比较两种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课,看起来像这样:

I've got a class looks quite like this:

object value;
Type type;

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

When I create the object I set the type to the type of the Object.

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

How can I compare this type with another type?

例如,如果类型为 String :

type.Equals(String)

type == String 

不起作用.

推荐答案

在这种情况下,您将 Type 实例与 typeof(T)的结果进行比较,其中 T 是您要比较的类型.

In this context, you compare your Type instance with the result of typeof(T), where T is the type you want to compare.

bool objectIsString = myType == typeof(string);

这篇关于比较两种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆