如何将对象转换为Type类描述的类型? [英] How to cast object to type described by Type class?
问题描述
我有一个对象:
ExampleClass ex = new ExampleClass();
和:
类型TargetType
我想将ex转换为TargetType描述的类型,如下所示:
对象o =(TargetType)ex;
但是当我这样做时,我得到:
< blockquote>
类型或命名空间名称't'可能找不到
去做这个?
我想获得类似这样的东西:
p> public CustomClass MyClassOuter
{
get
{
return(CustomClass)otherClass;
}
}
private otherClass;
因为我会有很多这样的属性,我想这样做:
public CustomClass MyClassOuter
{
get
{
return(GetThisPropertyType())otherClass;
}
}
private SomeOtherTypeClass otherClass;
上下文:
在我的类中,我需要创建许多属性。并在每一个替换铸造的类型的属性。它似乎对我没有意义(在我的上下文),因为我知道什么返回类型是,我想写一些类型的代码,将为我的铸造。也许是泛型的情况,我还不知道。
这就像我可以确保在这个属性,我得到正确的对象和正确的类型,我100%能够将其投射到属性类型。
所有我需要这样做,所以我不需要在每个属性中指定它必须转换值到CustomClass,我想做一些事
例如:
class MYBaseClass
{
protected List< Object> MyInternalObjects;
}
class MyClass
{
public SpecialClass MyVeryOwnSpecialObject
{
get
{
return(SpecialClass) MyInteralObjects [MyVeryOwnSpecialObject];
}
}
}
很多属性像这样一个上面 - 但有2个问题:
1)我需要在MyInternalObjects指定对象的名称,但它是相同的属性名称。这个我解决了System.Reflection.MethodBase.GetCurrentMethod()。名称。
2)在每个属性,我需要将对象从MyInternalObjects转换为不同的类型。在MyVeryOwnSpecialObject中例如 - 到SpecialClass。
这就是为什么我想这样做:
class MYBaseClass
{
protected List< Object> MyInternalObjects;
}
class MyClass
{
public SpecialClass MyVeryOwnSpecialObject
{
get
{
return(GetPropertyType ))MyInteralObjects [System.Reflection.MethodBase.GetCurrentMethod()。Name];
}
}
}
做什么的?因为在我的应用程序中,我将有安全类型等所有好处(intellisense)。
第二个:但现在你会失去类型安全在这个地方吗?
现在看起来是不可能的,但是很快就会提供.NET 4.0中称为动态的新功能:
http://www.codeguru.com/vb/vbnet30/article.php/c15645__4/
I have a object:
ExampleClass ex = new ExampleClass();
And:
Type 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:
The type or namespace name 't' could not be found
So how to do this? Am I missing something obious here?
Update:
I would like to obtain something like this:
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;
Context:
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.
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.
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".
For example:
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) 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) 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.
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屋!