简体   繁体   English

是否有System.IO.Stream的接口?

[英]Is there an interface for System.IO.Stream?

I'm writing a class that needs to take in an object, record it's current state, update the object, and pass it to another class. 我正在编写一个需要接收一个对象,记录其当前状态,更新该对象并将其传递给另一个类的类。 The only problem is that you can only inherit from one class and I need that to inherit a class in my project. 唯一的问题是您只能从一个类继承,而我需要在我的项目中继承一个类。 I'm interested in System.IO.Stream because I can if needed, move this class to it's own thread without most if not all of sockets and other methods of IPC. 我对System.IO.Stream感兴趣,因为如果需要,我可以将此类移动到它自己的线程中,而无需大多数(如果不是全部)套接字和IPC的其他方法。 The main problem is, is there an interface for System.IO.Stream? 主要问题是System.IO.Stream是否有接口? If not, is there a way to get the functionality I want without sacrificing the project-critical data I depend on? 如果没有,有没有办法在不牺牲我依赖的项目关键数据的情况下获得我想要的功能? (I'm sorry if I sound a little discoherent, I'm not really sure how to word this question.) (对不起,如果我听起来有点矛盾,我不确定该如何措辞。)

It sounds like IEnumerable<T> is a better fit - streams are good for passing bytes around, but iterators are good for representing more general streams of objects. 听起来IEnumerable<T>更合适-流适合于传递字节 ,但是迭代器适合于表示更通用的对象流。

You'd want something that read from a stream (eg a socket) and created objects, probably with C# iterator blocks (yield statements etc). 您需要从流(例如套接字)读取并创建对象的对象,可能使用C#迭代器块(yield语句等)。 Then you can use processing functions, filters etc on the IEnumerable<T> , and blow it back into a socket (or other byte stream) later on. 然后,您可以在IEnumerable<T>上使用处理函数,过滤器等,然后稍后将其吹回到套接字(或其他字节流)中。

To answer the question - no, Stream doesn't have an interface, but it's not clear how relevant that is to the threading part of your question. 要回答这个问题-不, Stream没有接口,但是尚不清楚与您的问题的线程部分有多相关。 Is it that you're interested in async IO? 您是否对异步IO感兴趣?

Why do you want an interface for Stream? 为什么要Stream的接口? implementing the interface won't give you any functionality from the class, it'll simply let other classes know that you provide that functionality. 实现该接口不会为您提供该类的任何功能,只会让其他类知道您提供了该功能。 if your REALL REALLY need to have multiple inheritance (two base classes so that you can get the functionality from both), you could have your base class, the one you definately need, be a subclass of Stream itself. 如果您的REALL确实需要具有多个继承(两个基类,以便可以从这两个基类中获得功能),则可以让您的基类(您肯定需要的基类)成为Stream本身的子类。

public class MyObject : Base Object
{

}

public class BaseObject : Stream
{

}

etc..? 等等..?

mroe background / context might help. mroe背景/上下文可能会有所帮助。

It's really hard to figure out what you're trying to do here. 真的很难弄清楚您要在这里做什么。

This sounds like a candidate for aggregation over inheritance. 这听起来像是通过继承进行聚合的候选对象。 Instead of extending System.IO.Stream, Why don't you pass one into the object to use? 而不是扩展System.IO.Stream,为什么不将其中一个传递给要使用的对象? This could be as a property or a get/set method, and if you pass the object off to another thread, the stream goes with it. 这可以作为属性或get / set方法,如果将对象传递给另一个线程,则流随之而来。

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

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