.NET:如何获得空对象的类型? [英] .NET : How do you get the Type of a null object?

查看:29
本文介绍了.NET:如何获得空对象的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 out 参数的方法试图进行类型转换.基本上:

I have a method with an out parameter that tries to do a type conversion. Basically:

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());
}

问题是通常有人会这样称呼它:

The problem is that usually someone would call this like:

string output;
GetParameterValue(output);

这将失败,因为:

destination.GetType()

destination 为空,因此我们不能对其调用 .GetType().我们也不能调用:

destination is null, so we can't call .GetType() on it. We also can not call:

typeof(destination)

因为目的地是变量名而不是类型名.

because destination is a variable name not a type name.

那么有没有什么办法可以得到一个设置为null的对象的类型呢?我认为必须有一种方法可以在不分配任何内容的情况下知道存储位置是什么类型.

So is there any way to get the type of an object that is set to null? I would think there would have to be a way to know what type a storage location is without it being assigned anything.

只是为了提供更多信息,我正在尝试制作一个实用方法,该方法将获取 Oracle 存储过程的输出参数.问题是 DbParameter.Value 是对象类型.

Just to give a bit more info, I am trying to make a utility method that will grab the output parameters of an Oracle stored procedure. The issue is that DbParameter.Value is of type object.

对于开发人员来说,理想的做法是:

What would be ideal would be for the developers to do something like:

string val = GetParameterValue("parameterName");

值得注意的是没有类型转换.实际上,你不知道equals"的 lparam,所以我选择了:

The notable thing is that there is no casting of types. In practice, you don't know the lparam of the "equals", so I went with:

string val;
GetParameterValue("parameterName", out val);

在方法中,我会知道输出变量的目标类型.我想这是一个糟糕的假设.作为替代,我也写了方法:

And figured within the method, I would know the destination type of the output variable. I guess that was a bad assumption. As an alternative, I also wrote the method:

public T GetParameterValue<T>(string paramName)

因此开发人员可以:

string val = GetParameterValue<string>("parameterName");

我发现显式的字符串"声明是重复的,特别是因为在实践中,目标可能是一个对象属性和 oracle 数据类型可能会改变(想想 ORM):

I find the explicit "string" declaration to be repetitive, especially since in practice, the destination if probably an object property and the oracle data type could change (think ORM):

MyObj.SomeProp = GetParameterValue<MyObj.SomeProp.GetType()>("parameterName");

但同样,如果 MyObj.SomeProp 为空,则 .GetType() 调用失败.VM 必须知道 MyObj.SomeProp 的类型,即使它为空,对吗?否则它将如何捕获强制转换异常?

But again, if MyObj.SomeProp is null, that .GetType() call fails. The VM has to know the type of MyObj.SomeProp, even when its null, right? or else how would it catch cast exceptions?

为了部分解决我自己的问题,我可以这样做:

To partially solve my own problem, I can do:

MyObj.SomeProp = GetParameterValue<typeof(MyObj).GetField("SomeProp").GetType()>("parameterName");

整个想法是不必在多个地方显式使用 Type,这样如果数据类型发生变化,只需在目标对象中更改它 (MyObj.SomeProp) 并在数据库中.必须有更好的方法...

The whole idea was to not have to explicitly use the Type in more than one place, so that if the data type changes, it only has to be changed in the destination object (MyObj.SomeProp) and in the DB. There has to be a better way...

推荐答案

那么有没有什么办法可以得到一个设置为null的对象的类型呢?我认为必须有一种方法可以在不分配任何内容的情况下知道存储位置是什么类型.

So is there any way to get the type of an object that is set to null? I would think there would have to be a way to know what type a storage location is without it being assigned anything.

不一定.最好的说法是它是一个 object.null 引用不指向任何存储位置,因此没有元数据可用于确定.

Not necessarily. The best that you can say is that it is an object. A null reference does not point to any storage location, so there is no metadata from which it can make that determination.

您能做的最好的事情就是将其更改为更通用,例如:

The best that you could do is change it to be more generic, as in:

public void GetParameterValue<T>(out T destination)
{
    object paramVal = "Blah";
    destination = default(T);
    destination = Convert.ChangeType(paramVal, typeof(T));
}

T 的类型是可以推断的,所以你不需要显式地给方法一个类型参数.

The type of T can be inferred, so you shouldn't need to give a type parameter to the method explicitly.

这篇关于.NET:如何获得空对象的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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