Tag: gettype

.NET:如何获得null对象的Type?

我有一个带out参数的方法尝试进行类型转换。 基本上: public void GetParameterValue(out object destination) { object paramVal = “I want to return this. could be any type, not just string.”; destination = null; // default out param to null destination = Convert.ChangeType(paramVal, destination.GetType()); } 问题是通常有人会这样称呼: string output; GetParameterValue(output); 这将失败,因为: destination.GetType() destination为null,因此我们不能在其上调用.GetType() 。 我们也不能打电话: typeof(destination) 因为destination是变量名而不是类型名。 那么有没有办法获得设置为null的对象的类型? 我认为必须有一种方法可以知道什么类型的存储位置没有分配任何东西。 只是为了提供更多信息,我试图创建一个实用程序方法,它将获取Oracle存储过程的输出参数。 问题是DbParameter.Value属于object类型。 对于开发人员来说,最理想的是: string val […]

为什么GetType返回System.Int32而不是Nullable ?

为什么这个片段System.Int32的输出而不是Nullable ? int? x = 5; Console.WriteLine(x.GetType());