public void GetStreamTest() { var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName); // Memory stream can be obtained from both compiled and resx, compiled is an unmanaged memory stream var resName = "TestSound"; manager.Source = ResourceManagerSources.CompiledOnly; var compiled = manager.GetStream(resName, inv); manager.Source = ResourceManagerSources.ResXOnly; var resx = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(compiled); Assert.IsInstanceOf <MemoryStream>(resx); Assert.AreNotEqual(compiled.GetType(), resx.GetType()); // Works also for byte[], now MemoryStream is returned for both resName = "TestBinFile"; manager.Source = ResourceManagerSources.CompiledOnly; compiled = manager.GetStream(resName, inv); manager.Source = ResourceManagerSources.ResXOnly; resx = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(compiled); Assert.IsInstanceOf <MemoryStream>(resx); Assert.AreEqual(compiled.GetType(), resx.GetType()); // For a string exception is thrown when SafeMode = false resName = "TestString"; Assert.IsFalse(manager.SafeMode); manager.Source = ResourceManagerSources.CompiledOnly; Assert.Throws <InvalidOperationException>(() => manager.GetStream(resName, inv)); manager.Source = ResourceManagerSources.ResXOnly; Throws <InvalidOperationException>(() => manager.GetStream(resName, inv), Res.ResourcesNonStreamResourceWithType(resName, Reflector.StringType)); // but when SafeMode is true, a string stream is returned manager.SafeMode = true; manager.Source = ResourceManagerSources.CompiledOnly; compiled = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(compiled); Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(compiled, Encoding.Unicode).ReadToEnd()); manager.Source = ResourceManagerSources.ResXOnly; resx = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(resx); Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(resx, Encoding.Unicode).ReadToEnd()); #if !NETCOREAPP2_0 // System.NotSupportedException : Cannot read resources that depend on serialization. // even for non-string resources resName = "TestImage"; manager.Source = ResourceManagerSources.CompiledOnly; compiled = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(compiled); Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(compiled, Encoding.Unicode).ReadToEnd()); manager.Source = ResourceManagerSources.ResXOnly; resx = manager.GetStream(resName, inv); Assert.IsInstanceOf <MemoryStream>(resx); Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(resx, Encoding.Unicode).ReadToEnd()); #endif }