C# 为什么 Assert.IsInstanceOfType(0.GetType(), typeof(int)) 失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/686571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:28:21  来源:igfitidea点击:

Why does Assert.IsInstanceOfType(0.GetType(), typeof(int)) fail?

c#unit-testingmstest

提问by Dave

I'm kind of new to unit testing, using Microsoft.VisualStudio.TestTools.UnitTesting;

我对单元测试有点陌生,使用Microsoft.VisualStudio.TestTools.UnitTesting;

The 0.GetType()is actually System.RuntimeType, so what kind of test do I need to write to pass Assert.IsInstanceOfType(0.GetType(), typeof(int))?

0.GetType()其实是System.RuntimeType,那种测试,以便我需要什么写传递Assert.IsInstanceOfType(0.GetType(), typeof(int))

--- following up, this is my own user error... Assert.IsInstanceOfType(0, typeof(int))

--- 跟进,这是我自己的用户错误... Assert.IsInstanceOfType(0, typeof(int))

采纳答案by JaredPar

Change the call to the following

将调用更改为以下

Assert.IsInstanceOfType(0, typeof(int));

The first parameter is the object being tested, not the type of the object being tested. by passing 0.GetType(), you were saying is "RunTimeType" an instance of System.int which is false. Under the covers thes call just resolves to

第一个参数是被测试的对象,而不是被测试对象的类型。通过传递 0.GetType(),你说的是“RunTimeType”System.int 的一个实例,它是错误的。在幕后,这些电话只是解决了

if (typeof(int).IsInstanceOfType(0))

回答by Lee

Looks like it should be

看起来应该是

Assert.IsInstanceOfType(0, typeof(int))

Your expression is currently evaluating to see if RunTimeType is an instance of RunTimeType, which it isn't.

您的表达式当前正在评估 RunTimeType 是否是 RunTimeType 的实例,但它不是。