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

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

问题描述

我有一个输出参数,试图做一个类型转换的方法。基本上是:

 公共无效GetParameterValue(out对象目的地)
{
    对象paramVal =我想返回,这可能是任何类型的,不只是串。;

    目标= NULL; //默认输出参数为null
    目的地= Convert.ChangeType(paramVal,destination.GetType());
}
 

现在的问题是,通常会有人称之为象:

 字符串输出;
GetParameterValue(输出);
 

这将失败,因为:

  destination.GetType()
 

目的地是空的,所以我们不能把 .GetType()就可以了。我们也不能说:

 的typeof(目的地)
 

由于目的地是一个变量名不是一个类型的名字。

那么,有没有办法让被设置为null对象的类型?我觉得有必须是一个办法知道存储位置是什么类型的,没有它被赋予什么。


为了给多一点信息,我想使这会抢Oracle存储过程的输出参数的实用方法。问题是, DbParameter.Value 是对象类型。

什么是理想的做法是开发商做这样的事情:

 字符串VAL = GetParameterValue(参数名称);
 

值得注意的事情是,没有铸造的类型。在实践中,你不知道的平等的LPARAM,所以我就跟着:

 字符串VAL;
GetParameterValue(参数名称,出VAL);
 

和方法内想通,我会知道的输出变量的目标类型。我想这是一个坏的假设。作为替代方案,我也写了方法:

 公共牛逼GetParameterValue< T>(字符串paramName配置)
 

因此​​,开发人员可以做的:

 字符串VAL = GetParameterValue<字符串>(参数名称);
 

我找到明确的字符串声明是重复的,特别是因为在实践中,如果目的地可能是一个对象的属性和Oracle的数据类型可能会改变(认为ORM):

  MyObj.SomeProp = GetParameterValue< MyObj.SomeProp.GetType()>(参数名称);
 

但同样,如果MyObj.SomeProp为空,即 .GetType()调用失败。虚拟机必须知道 MyObj.SomeProp 的类型,即使其空,对不对?要不怎么会就赶转换异常?


要部分解决我自己的问题,我可以做的:

  MyObj.SomeProp = GetParameterValue< typeof运算(MyObj中).GetField(SomeProp)的GetType()>(参数名称);
 

整个想法是没有明确地使用Type在一个以上的地方,所以,如果数据类型的变化,它只有在目标对象被改变( MyObj.SomeProp ),并在数据库中。必须有一个更好的办法...

解决方案
  

那么,有没有办法让被设置为null对象的类型?我觉得有必须是一个办法知道存储位置是什么类型的,没有它被赋予什么。

不一定。你能说的最好的是,它是一个对象。 A 引用不指向任何存储位置,所以没有元数据,从中可以作出这样的决定。

这是你可以做的最好的是将其更改为更加通用的,如:

 公共无效GetParameterValue< T>(出牛逼目的地)
{
    反对paramVal =嗒嗒;
    目标=默认(T);
    目的地= Convert.ChangeType(paramVal的typeof(T));
}
 

T 的类型可以推断,所以你不应该需要给一个类型参数,以明确的方式。

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

This will fail because of:

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.

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.


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");

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)

So the developers can do:

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

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");

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");

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...

解决方案

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.

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

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天全站免登陆