public void RemoveObject(string name) { ResXResourceSet resx = resxResourceSet; if (resx == null) { Throw.ObjectDisposedException(); } resx.RemoveObject(name); }
private void RemoveUnsupportedItems(ResXResourceSet rs) { string[] unsupported = #if NETCOREAPP2_0 // .NET Core 2.0 Drawing and WinForms types are not supported { "System.Drawing", "System.Windows.Forms" }; #else // .NET Core 3.0 and above: Drawing and WinForms types are supported, only binary serialized types are removed new string[0]; #endif bool origMode = rs.SafeMode; rs.SafeMode = true; foreach (var item in rs.GetEnumerator().ToEnumerable <string, ResXDataNode>().ToList()) { if (item.Value.AssemblyQualifiedName?.ContainsAny(unsupported) == true || item.Value.FileRef?.TypeName.ContainsAny(unsupported) == true || item.Value.MimeType == ResXCommon.BinSerializedObjectMimeType) { rs.RemoveObject(item.Key); } } rs.SafeMode = origMode; }
public void SetRemoveObject() { var path = Combine(Files.GetExecutingPath(), "Resources", "TestResourceResX.resx"); var rs = new ResXResourceSet(path, null); // replace Assert.IsTrue(rs.GetObject("TestString") is string); rs.SetObject("TestString", 1); Assert.AreEqual(1, rs.GetObject("TestString")); // add new Assert.IsNull(rs.GetObject("NotExist")); rs.SetObject("NotExist", 2); Assert.IsNotNull(rs.GetObject("NotExist")); // delete rs.RemoveObject("TestString"); Assert.IsNull(rs.GetObject("TestString")); Assert.IsFalse(rs.GetEnumerator().GetKeysEnumerator().Any(e => e == "TestString")); // nullifying rs.SetObject("NotExist", null); Assert.IsNull(rs.GetObject("TestString")); Assert.IsTrue(rs.GetEnumerator().GetKeysEnumerator().Any(e => e == "NotExist")); // save and reload StringBuilder sb = new StringBuilder(); #if NETCOREAPP2_0 || NETCOREAPP3_0 RemoveUnsupportedItems(rs); #endif rs.Save(new StringWriter(sb)); var rsReloaded = new ResXResourceSet(new StringReader(sb.ToString()), Path.GetDirectoryName(path)); AssertItemsEqual(rs, rsReloaded); }