简体   繁体   English

Rx Observable 值类型

[英]Rx Observable on value types

I have an API receiving messages as structs in a callback method我有一个 API 在回调方法中接收消息作为结构

void OnReceive(ref T message)

. . That method receives those struct objects by reference to avoid copy.该方法通过引用接收那些结构对象以避免复制。 I am thinking to wrap that API and expose messages as observable.我正在考虑包装 API 并将消息公开为可观察的。

Given that I can't make an observable of struct references and that the structs are passed/returned by making a copy that solution鉴于我无法观察结构引用,并且通过复制该解决方案来传递/返回结构

Observable<T> GetItems() //where T is struct

seems to be pretty suboptimal and it is better to keep using callback approach all the way through.似乎不是最理想的,最好一直使用回调方法。 Is there a better way to deal with structs in Rx extension library?有没有更好的方法来处理 Rx 扩展库中的结构?

You could simply wrap the value type in a class and have the observable pass the class.您可以简单地将值类型包装在 class 中,并让 observable 传递 class。

Something like this:是这样的:

public class Ref<T> where T : struct { public T Value { get; init; } }

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

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