public byte[] Serialize(object obj) { ThrowIf.IsNull(nameof(obj), obj); var formatter = new BinaryFormatter(); var stream = new MemoryStream(); formatter.Serialize(stream, obj); return(stream.ToArray()); }
public T Deserialize <T>(byte[] bytes) where T : class, new() { ThrowIf.IsNull(nameof(bytes), bytes); var stream = new MemoryStream(); var formatter = new BinaryFormatter(); stream.Write(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); return((T)formatter.Deserialize(stream)); }
/// <summary> /// Parses a string into an Enum /// </summary> /// <typeparam name="T">The type of the Enum</typeparam> /// <param name="value">String value to parse</param> /// <param name="ignorecase">Ignore the case of the string being parsed</param> /// <returns>The Enum corresponding to the stringExtensions</returns> public static T ToEnum <T>(this string value, bool ignorecase) where T : struct { ThrowIf.IsNull(value, "value"); var t = typeof(T); if (!t.IsEnum) { throw new ArgumentException("Type provided must be an Enum.", nameof(value)); } return((T)Enum.Parse(t, value, ignorecase)); }
public async Task SendResponseAsync(IEnumerable <KeyValuePair <string, string> > headerFields, byte[] bytesToWrite) { ThrowIf.IsNullOrEmpty(nameof(bytesToWrite), bytesToWrite); ThrowIf.IsNull(nameof(headerFields), headerFields); var responseHeaders = new List <HeaderField>(); foreach (var header in headerFields) { responseHeaders.Add( new HeaderField { Name = header.Key, Value = header.Value }); } await Stream.WriteHeadersAsync(responseHeaders, false); await Stream.WriteAsync(new ArraySegment <byte>( bytesToWrite), true); }
public void Bind <T>(T instance) { ThrowIf.IsNull(nameof(instance), instance); if (!typeof(T).IsInterface) { throw new ArgumentException($"{nameof(T)} must be an interface", nameof(T)); } var interfaceName = typeof(T).GetName(); if (Map.TryGetValue(interfaceName, out var value)) { if (instance.Equals(value.Instance)) { // Skipping due to binding same thing return; } throw new ArgumentException($"{interfaceName} already has existing binding of {value.Instance}. Cannot bind {instance}.", nameof(instance)); } AddMethods <T>(instance); }
public T Deserialize <T>(byte[] bytes) where T : class, new() { ThrowIf.IsNull(nameof(bytes), bytes); return(LZ4MessagePackSerializer.Deserialize <T>(bytes)); }
public byte[] Serialize(object obj) { ThrowIf.IsNull(nameof(obj), obj); return(LZ4MessagePackSerializer.Serialize(obj)); }