简体   繁体   English

C#TcpClient扩展,基类上转换

[英]C# TcpClient extension, base class upconversion

I am currently writing a little Tcp class capable of multithreaded client handling and so on. 我目前正在编写一个能够进行多线程客户端处理的Tcp类。 Therefore, I have to extend the TcpClient in System.Net.Sockets for added functionality such as directly writing a string and so on. 因此,我必须扩展System.Net.Sockets中的TcpClient以获得更多功能,例如直接编写字符串等。 Now I am a little stuck when it comes to upconverting a TcpClient base class into the TCP_ModifiedClient (the extended class). 现在,在将TcpClient基类上转换为TCP_ModifiedClient(扩展类)时,我有些困惑。
Currently, I am trying to copy all Properties as follows, however, I am getting the error, that a socket can only be assigned when it is not connected. 目前,我正在尝试按以下方式复制所有属性,但是,出现错误,即只能在未连接时分配套接字。
The TcpClient that has to be upconverted is connected by design. 必须将上转换的TcpClient通过设计连接。

The Code for Property-Copying: 财产复制守则:
PropertyDescriptorCollection sp = TypeDescriptor.GetProperties(new TcpClient()); PropertyDescriptorCollection tp = TypeDescriptor.GetProperties(new TCP_ModifiedClient()); foreach (PropertyDescriptor pdt in tp) foreach (PropertyDescriptor pds in sp) if (pdt.Name == pds.Name) pdt.SetValue(this, pds.GetValue(client));

Is there any elegant solution to this? 有什么优雅的解决方案吗? In the worst case szenario, I would add a private variable of type TcpClient to the TCP_ModifiedClient class whilst implementing all public methods of TcpClient, however I was hoping to get the extension sorted via inheritance. 在最坏的情况下,我将在实现TcpClient的所有公共方法的同时将类型为TcpClient的私有变量添加到TCP_ModifiedClient类,但是我希望通过继承对扩展名进行排序。

I sincerely hope somebody is capable of helping. 我衷心希望有人能够提供帮助。

* EDIT * *编辑*
Okay. 好的。 I may have not expressed myself precisely. 我可能没有精确地表达自己。
As previously mentioned, I am writing a little class capable of providing a convenient way of doing server/client applications. 如前所述,我正在编写一个小类,该类能够提供一种处理服务器/客户端应用程序的便捷方法。 For this, I am implementing a TcpClient replacement class adding functionality such as in-class writing/reading to/from the TcpClient's base stream, automatic establishing of a AES secured connection and so on. 为此,我实现了一个TcpClient替换类,该类添加了一些功能,例如在类中向TcpClient的基本流写入/读取,自动建立AES安全连接等。 Currently, this replacement class TCP_ModifiedClient inherits from TcpClient. 当前,此替换类TCP_ModifiedClient继承自TcpClient。 Since the .NET network functions use TcpClient, I must implement a conversion method from TcpClient to TCP_ModifiedClient. 由于.NET网络功能使用TcpClient,因此我必须实现从TcpClient到TCP_ModifiedClient的转换方法。 Since my current implementation of TCP_ModifiedClient is inheriting from TcpClient, I originally intended to add a constructor to TCP_ModifiedClient accepting a TcpClient as parameter and then assining this as the base class of TCP_ModifiedClient. 由于我当前的TCP_ModifiedClient实现是从TcpClient继承的,因此我最初打算向TCP_ModifiedClient添加一个构造函数,并接受TcpClient作为参数,然后将其作为TCP_ModifiedClient的基类。 This obviously does not work, therefore, in my above given code, I tried to assign all properties from TcpClient to TCP_ModifiedClient. 这显然是行不通的,因此,在我上面给出的代码中,我尝试将TcpClient的所有属性分配给TCP_ModifiedClient。
Hopefully, this was a little clearer. 希望这一点更加清楚。 It's currently 1:15AM, so sorry for any repetition/mind-blockedness. 现在是上午1:15,因此对您的重复/思维障碍感到抱歉。

This is an interesting question because it is not clear what the misunderstanding is. 这是一个有趣的问题,因为不清楚误解是什么。 It seems you want to create a class that kind of looks like a TcpClient but that does things differently. 似乎您想创建一个看起来像TcpClient的类,但其功能却有所不同。 The way to do this is to write such a class yourself and implement it. 做到这一点的方法是自己编写一个此类并实现。 You might wrap a TcpClient doing that. 您可以包装一个TcpClient这样。

It is not possible in .NET to copy properties to mimic another object. 在.NET中,无法复制属性来模仿另一个对象。 (Are you a JavaScript developer?) Type members in .NET are statically defined and not changeable. (您是JavaScript开发人员吗?)。NET中的类型成员是静态定义的,并且不可更改。 Since TcpClient does not look like it is made to be inherited from you cannot achieve your goal using inheritance either. 由于TcpClient看起来不像是从您继承的,因此您也无法使用继承来实现您的目标。 You must implement a fresh class. 您必须实现一个新的类。

This is the right way to do it. 这是正确的方法。 Composition is preferred over inheritance if inheritance would just be a way to share code and type less. 如果继承只是共享代码和减少键入的一种方式,则组合优先于继承。 Don't pull in all TcpClient members. 不要拉入所有TcpClient成员。 Just expose the members that you need and that make sense to expose. 只需公开您需要的成员,然后公开即可。 "In the worst case szenario" turns out this worst case scenario is the best case from a design standpoint. 从设计的角度来看,“在最坏的情况下szenario”证明了这种最坏的情况是最好的情况。

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

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