将对象强制转换为类 [英] cast object to a class

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

问题描述

如何将对象强制转换为类?这不起作用吗?



How do you cast an object to a class? This does not work?

object[] objs1 = (object[])(e.AddedItems);
           //send event with data arg to other module
           Class[] objs = (class[])(objs1);

推荐答案

此代码完全是胡言乱语。除非你从一开始就学习,否则什么都无法帮助你。



你需要知道关键字 object System.Object 别名,这是类型,但是关键字 class 不表示任何特定类型。



整个问题如何将对象强制转换为类是没有意义的,因为你的转换意味着将一种类型的对象表示为它是另一种类型的对象。强制转换的结果是同一个对象,而不是类,但结果可以分配给目标类型的变量(成员)。看起来你知道语法,但''()''中的表达式应该是某种特定类型的名称。案例可能成功与否,因此您可能希望使用动态强制转换,在运算符上读取为



抱歉,但我从一开始就无法帮助你学习编程,我无法回答你没有意义的问题。我只能提一下所涉及的内容。



-SA
This code is total gibberish. Nothing can help you unless you start learning from the very beginning.

You need to know that the keyword "object" is the alias of System.Object, which is the type, but keyword class does not denote any certain type.

The whole question "how to cast object to class" makes no sense, because you casting means representing some object of one type as it was the object of another type. The result of the cast is the same object, not class, but the result can be assigned to a variable (member) of the target type. It looks like you know the syntax, but the expression in ''()'' should be a name of some certain type. The case can be successful or not, so you may want to use dynamic cast instead, read on operator as.

Sorry, but I cannot help you to learn programming from the very beginning, and I cannot answer your question which does not make sense. I only can mention what''s involved.

—SA


myClassA X = new myClassA();

myClassB Y =(myClassB)X;
myClassA X = new myClassA();
myClassB Y = (myClassB) X;


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

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