如何施展对象按类型类类型来描述? [英] How to cast object to type described by Type class?

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

问题描述

我有一个目标:

ExampleClass ex = new ExampleClass();

Type TargetType

我想投前要键入由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

那么,如何做到这一点?我失去了一些东西obious在这里?

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;

上下文:

通常我在我的课方面,我需要创建很多属性。并在每更换铸造属性的类型。它似乎并不具有意义,我(在我的情况下),因为我知道是什么返回类型是,我想写出某种code,会做铸件我。也许这是仿制药的情况下,我还不知道。

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"];
      }
   }
}

和行 - 我可以做许多属性像这样的上面 - 但是有2个问题:

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

1)我需要在MyInternalObjects指定对象的名称,但它同样喜欢的属性名称。这个我解决了System.Reflection.MethodBase.GetCurrentMethod()。名称。

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.$c $ cguru.com / VB / vbnet30 / article.php / c15645%5F%5F4 /

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

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