投稿

ラベル(C#)が付いた投稿を表示しています

LINQでBufferしたい

Observable.BufferをLINQで実現する var arr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /* GroupByを使う */ IEnumerable<IEnumerable<int>> arr2 = arr.Select((value, index) => (value, index)).GroupBy(a => a.index / 3).Select(a => a.Select(b => b.value)); // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] /* Aggregateを使う */ List<List<int>> arr3 = arr.Aggregate(new List<List<byte>>(), (list, next) => { if (!list.Any() || list.Last().Count() >= 3) list.Add(new List<byte>()); list.Last().Add(next); return list; }); // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] /* Observable.Bufferを使う */ IList<IList<int>> arr4 = await arr.ToObservable().Buffer(3).ToList().ToTask(); // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

C#標準ライブラリでWebSocketクライアントを作る

C#でWebSocketクライアントを作りたい場合、websocket-sharpというライブラリがよく使われているが、標準ライブラリだけで賄いたい。 標準でSystem.Net.WebSockets名前空間にClientWebSocketクラスが用意されており、MSDNに使い方が載っている。 Windows 8 のネットワーク接続 - Windows 8 と WebSocket プロトコル 一応通信はできるのだが、あまりにも原始的なのでラッパークラスを作る。 using System; using System.IO; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; public class WebSocketClient { private const int SendBufferSize = 1024; private const int ReceiveBufferSize = 1024; private ClientWebSocket Socket; public event EventHandler OnOpenHandler; public event EventHandler OnCloseHandler; public event EventHandler<string> OnTextHandler; public event EventHandler<byte[]> OnBinaryHandler; public Uri Uri { get; set; } public async Task SendTextAsync(string text) { using var memoryStream = new MemoryStream(); using var streamWriter = new StreamWriter(memoryStream); await streamWriter.WriteAsync(text); await st...

C#でコールバックをTaskにしてawaitしたい

C#でコールバックの値を同期的に取得したい時がある。 Rxを使ってJavascriptのPromise的なことをする。 // コールバックで値を返すメソッド private void Foo(Action<string> arg) { arg("qawsedrf"); } // コールバックの値を取得してTaskにして返すメソッド private Task<string> Bar() { return Observable.Create<string>(observer => { Foo(a => { observer.OnNext(a); observer.OnCompleted(); }); return null; }).ToTask(); } string value = await Bar(); Console.WriteLine(value); // qawsedrf Observable.Createでコールバックからの値を流すObservableを作り、 ToTaskでTaskに変換できる。 これを活用して以下のようなことができる。 string dataPath = await Observable.Create<string>(observer => { return Observable.Return(Unit.Default).ObserveOnMainThread().Subscribe(_ => { observer.OnNext(Application.persistentDataPath); observer.OnCompleted(); }); }).ToTask(); Unityの場合、メインスレッド上でなければUnityAPIを呼び出すことができないが、 こうすることでUnityAPIである Application.persistentDataPath の呼び出しだけメインスレッ...

指定範囲内のVector2Intの配列を作る

public class TestClass { public void TestMethod() { var countX = 3; var countY = 4; var aaa = Enumerable.Range(0, countY) .SelectMany(y => Enumerable.Range(0, countX) .Select(x => new Vector2Int(x, y))); // (x:0, y:0), (x:1, y:0), (x:2, y:0), // (x:0, y:1), (x:1, y:1), (x:2, y:1), // (x:0, y:2), (x:1, y:2), (x:2, y:2), // (x:0, y:3), (x:1, y:2), (x:2, y:3) } }

プロパティ以外からのバッキングフィールドへのアクセスに警告を出す

プロパティ以外からバッキングフィールドにアクセスさせたくない時が結構あります。 本来の用途ではありませんが、Obsoleteを使ってコンパイラ警告を出します。 public class TestClass { [Obsolete] private string _foo; // Obsolete属性を付ける public string Foo { // コンパイラ警告を無効化 #pragma warning disable 612 get { return _foo; } set { _foo = value; } // コンパイラ警告を有効化 #pragma warning restore 612 } public void TestMethod() { _foo = "aaaaaa"; // CS0612: Use of obsolete symbol (without message) } }