Is there an interface for System.IO.Stream?

Is there an interface for System.IO.Stream?

平定天下 发布于 2021-11-27 字数 673 浏览 727 回复 3 原文

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. The main problem is, is there an interface for 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.)

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


潜移默化 2022-06-07 3 楼

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? 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.

橘亓 2022-06-07 2 楼

Why do you want an interface for 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.

public class MyObject : Base Object


public class BaseObject : Stream



mroe background / context might help.

只是我以为 2022-06-07 1 楼

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.

You'd want something that read from a stream (e.g. a socket) and created objects, probably with C# iterator blocks (yield statements etc). 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.

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. Is it that you're interested in async IO?