- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我刚刚从一个同事那里得到了这个让我发疯的测验。对于这段代码:
var x = new Int32?();
string text = x.ToString(); // No exception
Console.WriteLine(text);
Type type = x.GetType(); // Bang!
为什么第一部分 .ToString() 工作时没有抛出异常,然后调用 GetType() 抛出 NullReferenceException?
最佳答案
ToString
是overridden in Nullable<T>
, 因此调用时不涉及装箱。
GetType()
不是虚方法,因此不会(也不能)被重写,因此在调用之前将值装箱...并且装箱可为空值类型的空值提供空引用。
装箱的原因在 C# 4 规范的第 7.5.5 节中:
If
M
is an instance function member declared in a reference-type:
- ...
- If the type of
E
is a value-type, a boxing conversion (4.3.1) is performed to convertE
to typeobject
, andE
is considered to be of typeobject
in the following steps. In this case,M
could only be a member ofSystem.Object
请注意,如果您有:
var x = new Int32?(10);
你最终会得到与 typeof(int)
相同的类型,又是由于拳击。无法创建值 foo
这样 foo.GetType()
使用普通的 GetType()
返回可空值类型方法。 (您当然可以创建一个新 GetType()
方法,但这是一个附带问题:)
(使用“Bang!”表明上述测验的作者可能是我。如果是这样的话,很抱歉让你发疯。)
关于c# - 可空类型 GetType() 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12725631/
我已经阅读了“UnderlyingSystemType”的定义,即它“表示公共(public)语言运行时提供的表示此类型的类型”。 SO 上有一个相关链接 When does the Underlyi
以下代码会产生不同的结果: class X { public class Y { } } ... var t = typeof(X.Y); var n = t.ToString().Dump(
这个问题在这里已经有了答案: How costly is .NET reflection? (13 个答案) 关闭 9 年前。 我在 .NET/C# 中使用反射 API,例如 GetType() 和
private int buType=0; public int getType() { return buType; } 上述代码片段是在扩展 JDialog 类的类中编写的。它在 Java
我试图在运行时从程序集列表中定位一个类型;我的代码是这样的: foreach (Assembly assembly in assembliesToSearch) { Type t = asse
System.Object.GetType() 和 System.Type.GetType() 有区别吗 最佳答案 System.Type 派生自 System.Object。因此,它从 System
当我在 Powershell(已安装 v3)中观察到一些奇怪的行为时,我正在整理一个简单的演示脚本。 $file = ls 'C:\temp' | Where-Object {$_.Extension
我正在使用 Rome 和 SyndEntry(com.sun.syndicate.feed.synd.SyndEntry) 来检索 URL 的“getType”字段。 这是示例 xml feed。
我有一个 C# 程序集,但我没有它的依赖项程序集。 有没有办法在没有其依赖项程序集的情况下获取 .Net 程序集的 GetTypes()。 Assembly SampleAssembly; Sam
在 foreach 语句的帮助下,我在我的 WinForms 表单中的一些 controls 中搜索。我正在比较通过“is”-reference(a is DataGridView)找到的对象。 “a
我正在制作 XNA 游戏,每次更新我调用以下代码 2 到 20 次。我尝试使用谷歌搜索,这似乎有点慢,所以我只是想问问是否有更快的方法来比较类型? 代码: public Modifier th
我在 NewStarterTool 命名空间中有以下 Globals 类: static class Globals { static internal class FieldNames
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: .NET: Determine the type of “this” class in its static
为什么会这样: Object o = "my string"; Console.WriteLine(o.GetType()); 输出: System.String 如果函数调用被分派(dispatch
因此,我对自定义类型方面的数据如何存储在 .NET Framework 中没有深入的了解,但我一直在寻找关于转换系统如何工作的解释。 例如,如果要从像 Char 这样的 ValueType 结构显式转
我遇到了这段代码: int myInt = 0; textBox1.Text = myInt.GetType().Name; 根据.NET documentation , GetType() 是一个方
在处理嵌套类并将类型名称输出到控制台窗口时,我注意到了一些奇怪的事情。我想知道是否有人可以为我解释一下。在主类上调用 GetType() 时,它会返回我所期望的,即相关命名空间之后的类名。即 Name
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: .NET: Determine the type of “this” class in its static
我有一个可能不包含任何项目的初始化数组。 我们称它为a, 在a 上调用GetType() 显然会返回一种数组类型。是否可以获取数组包含的项的类型? 显然 a[0].GetType() 可以工作,但数组
基于几天前在 SO 中提出的以下问题:GetType() and polymorphism和阅读 Eric Lippert's回答,我开始考虑如果让 GetType() 不是虚拟的真的确保一个对象不能
我是一名优秀的程序员,十分优秀!