如何将对象转换为 Type 类描述的类型? [英] How to cast object to type described by Type class?

查看:30
本文介绍了如何将对象转换为 Type 类描述的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

ExampleClass ex = new ExampleClass();

还有:

Type TargetType

我想将 ex 转换为 TargetType 描述的类型,如下所示:

I would like to cast ex to type described by TargetType like this:

Object o = (TargetType) ex;

但是当我这样做时,我得到:

But when I do this I get:

类型或命名空间名称 't' 可以找不到

The type or namespace name 't' could not be found

那么怎么做呢?我在这里错过了什么令人讨厌的东西吗?

So how to do this? Am I missing something obious here?

更新:

我想获得这样的东西:

public CustomClass MyClassOuter
{
   get
   {
        return (CustomClass) otherClass;
   }
}

private otherClass;

因为我会有很多这样的属性,所以我想这样做:

And because I will have many properties like this I would like do this:

public CustomClass MyClassOuter
{
   get
   {
        return (GetThisPropertyType()) otherClass;
   }
}

private SomeOtherTypeClass otherClass;

上下文:

通常在我的班级上下文中,我需要创建许多属性.并且在每一个中都将转换替换为属性类型.这对我来说似乎没有意义(在我的上下文中),因为我知道返回类型是什么,并且我想编写某种代码来为我进行转换.也许这是泛型的情况,我还不知道.

Normally in my context in my class I need to create many properties. And in every one replace casting to the type of property. It does not seem to have sense to me (in my context) because I know what return type is and I would like to write some kind of code that will do the casting for me. Maybe it's case of generics, I don't know yet.

就好像我可以在这个属性中保证我得到正确的对象和正确的类型,并且我 100% 能够将它转换为属性类型.

It's like I can assure in this property that I get the right object and in right type and am 100% able to cast it to the property type.

我需要这样做,这样我就不需要在每个属性中都指定它必须将值转换为 CustomClass",我想做一些类似将值转换为与此属性相同的类"的操作".

All I need to do this so that I do not need to specify in every one property that it has to "cast value to CustomClass", I would like to do something like "cast value to the same class as this property is".

例如:

class MYBaseClass
{
   protected List<Object> MyInternalObjects;
}

class MyClass
{
   public SpecialClass MyVeryOwnSpecialObject
   {
      get
      {
           return (SpecialClass) MyInteralObjects["MyVeryOwnSpecialObject"];
      }
   }
}

好吧 - 我可以创建很多像上面这样的属性 - 但有两个问题:

And ok - I can make many properties like this one above - but there is 2 problems:

1) 我需要在 MyInternalObjects 上指定对象的名称,但它与属性名称相同.我用 System.Reflection.MethodBase.GetCurrentMethod().Name 解决了这个问题.

1) I need to specify name of object on MyInternalObjects but it's the same like property name. This I solved with System.Reflection.MethodBase.GetCurrentMethod().Name.

2) 在每个属性中,我需要将对象从 MyInternalObjects 转换为不同的类型.例如在 MyVeryOwnSpecialObject 中 - 到 SpecialClass.它总是与属性相同的类.

2) In every property I need to cast object from MyInternalObjects to different types. In MyVeryOwnSpecialObject for example - to SpecialClass. It's always the same class as the property.

这就是为什么我想做这样的事情:

That's why I would like to do something like this:

class MYBaseClass
{
   protected List<Object> MyInternalObjects;
}

class MyClass
{
   public SpecialClass MyVeryOwnSpecialObject
   {
      get
      {
           return (GetPropertyType()) MyInteralObjects[System.Reflection.MethodBase.GetCurrentMethod().Name];
      }
   }
}

现在担心:好的,有什么用?因为在我的应用程序中,我将拥有安全类型等(智能感知)的所有好处.

And now concerns: Ok, what for? Because further in my application I will have all benefits of safe types and so on (intellisense).

第二个:但是现在你会在这个地方失去类型安全吗?不.因为我非常确定我的列表中有我的类型的对象.

Second one: but now you will lost type safety in this place? No. Because I'm very sure that I have object of my type on a list.

推荐答案

现在这似乎是不可能的,但很快就会通过 .NET 4.0 中称为动态"的新功能提供:

Now it seems to be impossible, but soon will be available with new feature in .NET 4.0 called "dynamic":

http://www.codeguru.com/vb/vbnet30/article.php/c15645__4/

这篇关于如何将对象转换为 Type 类描述的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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