public void CustomProviders() { int stockProviders = MemoryDataConverter.Providers.Count; var testConv = new MemoryDataConverterProvider <TestCustomConverter>( (arr, ind) => (TestCustomConverter)arr[ind], (obj) => obj is byte ?(TestCustomConverter)obj : TestCustomConverter.Error); MemoryDataConverter.AddProvider(testConv); Assert.AreEqual(1, MemoryDataConverter.Providers.Count - stockProviders); MemoryDataConverter.AddProvider(testConv); Assert.AreEqual(1, MemoryDataConverter.Providers.Count - stockProviders); MemoryDataConverter.RemoveProvider <TestCustomConverter>(); Assert.AreEqual(0, MemoryDataConverter.Providers.Count - stockProviders); MemoryDataConverter.RemoveProvider <TestCustomConverter>(); Assert.AreEqual(0, MemoryDataConverter.Providers.Count - stockProviders); }
public void ConvertCustom() { var data = new byte[20]; var myConv = new MemoryDataConverterProvider <TestCustomConverter>( (arr, ind) => { int intermediate = BitConverter.ToInt32(arr, ind); return((TestCustomConverter)(intermediate / 2)); } , (obj) => { if (obj is int) { return((TestCustomConverter)((int)obj / 2)); } else { return(TestCustomConverter.Error); } }); // integer code *2 is our 'conversion' Array.Copy(BitConverter.GetBytes((int)(TestCustomConverter.All) * 2), 0, data, 0, 4); Array.Copy(BitConverter.GetBytes((int)(TestCustomConverter.Test1) * 2), 0, data, 4, 4); Array.Copy(BitConverter.GetBytes((int)(TestCustomConverter.Test2) * 2), 0, data, 8, 4); Array.Copy(BitConverter.GetBytes((int)(TestCustomConverter.Test3) * 2), 0, data, 12, 4); Array.Copy(BitConverter.GetBytes((int)(TestCustomConverter.Test4) * 2), 0, data, 16, 4); MemoryDataConverter.AddProvider(myConv); Assert.AreEqual(TestCustomConverter.All, MemoryDataConverter.Read <TestCustomConverter>(data, 0)); Assert.AreEqual(TestCustomConverter.Test1, MemoryDataConverter.Read <TestCustomConverter>(data, 4)); Assert.AreEqual(TestCustomConverter.Test2, MemoryDataConverter.Read <TestCustomConverter>(data, 8)); Assert.AreEqual(TestCustomConverter.Test3, MemoryDataConverter.Read <TestCustomConverter>(data, 12)); Assert.AreEqual(TestCustomConverter.Test4, MemoryDataConverter.Read <TestCustomConverter>(data, 16)); Assert.AreEqual(TestCustomConverter.All, MemoryDataConverter.Read <int, TestCustomConverter>(data, 0)); Assert.AreEqual(TestCustomConverter.Test1, MemoryDataConverter.Read <int, TestCustomConverter>(data, 4)); Assert.AreEqual(TestCustomConverter.Test2, MemoryDataConverter.Read <int, TestCustomConverter>(data, 8)); Assert.AreEqual(TestCustomConverter.Test3, MemoryDataConverter.Read <int, TestCustomConverter>(data, 12)); Assert.AreEqual(TestCustomConverter.Test4, MemoryDataConverter.Read <int, TestCustomConverter>(data, 16)); // Short != type of int, so the conversion function returns 'error' Assert.AreEqual(TestCustomConverter.Error, MemoryDataConverter.Read <short, TestCustomConverter>(data, 16)); MemoryDataConverter.RemoveProvider <TestCustomConverter>(); }