简体   繁体   English

如何将套接字转换为处理程序类?

[英]How do I cast a socket to my handler class?

I created a handler class which extends the System.Net.Sockets.Socket class. 我创建了一个处理程序类,该类扩展了System.Net.Sockets.Socket类。 I am not familiar with casting types, inheritance or C# for that matter, so how do I cast a regular Socket to my Handler class? 我对此不熟悉转换类型,继承或C#,因此如何将常规Socket转换为Handler类?

I've tried this: 我已经试过了:

Handler handler = (Handler)socket; 

But I get an InvalidCastException. 但是我得到一个InvalidCastException。 Also, I do not want to extend the class by using a container class. 另外,我不想通过使用容器类来扩展该类。

Thanks. 谢谢。

You just can't, that's not how inheritance works. 您只是不能,那不是继承的原理。

You can't treat some Socket as your Handler . 您不能将某些Socket视为Handler It works the other way around: you can treat your Handler as a Socket . 它以另一种方式起作用:您可以将Handler视为Socket

If you want to use your Handler , you need to create an instance of it, by using something like new Handler() . 如果要使用Handler ,则需要使用诸如new Handler()类的东西来创建它的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM