运行时转换 [英] runtime casting
问题描述
我想用某种运行时方法替换CSomeObject类
返回我可以用作强制类型的CSomeObject类型。
如何指定运行时显式转换的类型?
例如:
object object1 = new CSomeObject(22);
object object2 = new CSomeObject(2);
if((运行时方法返回类CSomeObject)object1>(运行时
方法返回CSomeObject)object2)
// ...等
//不会工作,因为编译器错误运算符>''不能应用于
的操作数输入''object''和''object'' :
if(object1> object)
// ...等
Richard
>
我想你提出两个不同的问题:
1)如何将对象转换为返回的类型运行时
2)如果在编译时我不知道它们的类型,我如何比较2个对象?
我将它们分开,因为我可以回答每一个,但不一定是你的
原始问题。
要将对象转换为在运行时确定的类型,你需要
Covert.ChangeType()。示例:
public Type RuntimeMethodReturnsSomeType(){
返回typeof(CSomeObject);
}
object object1 = Convert.ChangeType(object2,
RuntimeMethodReturnsSomeType());
现在object2已转换为CSomeObject。但是,它仍然存储在一个''对象''变量中,所以你仍然可以只执行可以在一个对象上执行的动作
(不包括比较
使用>)。
如果你想在不知道类型的情况下比较2个对象,你可以将
投射到IComparable。 br />
对象a = 3;
对象b = 4;
if(((IComparable)a)。 CompareTo(b)> 0)
{
Console.WriteLine(a更大);
}
else
{
Console.WriteLine(" b is greater);
}
>
如果你不能保证底层类型实现IComparable,
或者是同一类型,你可能想要做一些额外的检查...
对象a = 3;
对象b = 4;
IComparable aComparable = a为IComparable;
if(a!= null&& a.GetType()== b.GetType()){
if(a.CompareTo(b)> 0){
//当a大于b时做东西
}
else {
//当b大于(或等于)a
}
}
else {
//当物品无法比较时做东西
}
希望这会有所帮助
Joshua Flanagan
http://flimflan.com/blog
我明白你的意思。我希望这样一个更简单的
解决方案:
类型t = someinstance.GetType();
if( (t)对象>(t)对象)
//等
也许MS可以在下一版C#中添加此功能?
感谢您提供有关Convert.ChangeType和IComparable的提示。
Richard
< blockquote>你试图解决的潜在问题是什么?那是什么
你想要做什么?也许有更好的方法来解决问题
不需要新的语言功能。发布您的
问题的详细信息,也许这里有人可以建议采用不同的方法。
I want to replace CSomeObject class with some kind of runtime method
that returns type CSomeObject that I can use as cast.
How do I specify type of explicit cast at runtime?
eg:
object object1 = new CSomeObject(22);
object object2 = new CSomeObject(2);
if ( ( runtime method returns class CSomeObject ) object1 > ( runtime
method returns CSomeObject ) object2 )
// ... etc
// won''t work because compiler error "Operator ''>'' cannot be applied to
operands of type ''object'' and ''object'' " :
if (object1 > object)
// ...etc
Richard
I think you are asking 2 different questions:
1) How do I convert an object to a type that is returned at runtime
2) How do I compare 2 objects when I do not know their type at compile time?
I separate them, because I can answer each one, but not necessarily your
original question.
To convert an object to a type that is determined at runtime, you
Covert.ChangeType(). Example:
public Type RuntimeMethodReturnsSomeType(){
return typeof(CSomeObject);
}
object object1 = Convert.ChangeType( object2,
RuntimeMethodReturnsSomeType() );
Now object2 has been converted to a CSomeObject. However, it is still
stored in an ''object'' variable, so you can still only perform actions
that can be performed on an object (which does not include comparisons
using >).
If you want to compare 2 objects without knowing their type, you can
cast them to IComparable.
object a = 3;
object b = 4;
if ( ((IComparable)a).CompareTo( b ) > 0 )
{
Console.WriteLine("a is greater");
}
else
{
Console.WriteLine("b is greater");
}
If you can''t guarantee that the underlying type implements IComparable,
or are the same type, you may want to do some additional checking...
object a = 3;
object b = 4;
IComparable aComparable = a as IComparable;
if ( a != null && a.GetType() == b.GetType() ) {
if (a.CompareTo(b) > 0) {
// do stuff when a is greater than b
}
else {
// do stuff when b is greater (or equal) to a
}
}
else {
// do stuff when the objects cannot be compared
}
Hope this helps
Joshua Flanagan
http://flimflan.com/blog
I understand what you are getting at. I was hoping for a much simpler
solution like this:
Type t = someinstance.GetType();
if( (t)object > (t)object )
// etc
Maybe MS can add this feature in the next release of C#?
Thanks for the tips on Convert.ChangeType and IComparable.
Richard
What is the underlying problem you''re trying to solve? What is that
you''re trying to do? Maybe there''s a better way to approach the problem
that doesn''t require new language features. Post the details of your
problem, and perhaps someone here can suggest a different approach.
这篇关于运行时转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!