using System.Runtime.Serialization.Formatters.Binary; namespace HydroUI { [Serializable] public static class HelperC { public static Type GetObjType(this MapObjectType type) { switch (type) { case MapObjectType.全部: return null; case MapObjectType.节点: return typeof(JunctionViewModel); case MapObjectType.水表: return typeof(MeterViewModel); case MapObjectType.水库: return typeof(ReservoirViewModel); case MapObjectType.水池: return typeof(TankViewModel); case MapObjectType.喷头: return typeof(NozzleViewModel); case MapObjectType.管线: return typeof(PipeViewModel); case MapObjectType.阀门: return typeof(ValveViewModel); case MapObjectType.重复器: return typeof(RepeaterViewModel); case MapObjectType.水泵: return typeof(PumpViewModel); case MapObjectType.阀门点: return typeof(ValveNodeViewModel); case MapObjectType.水泵点: return typeof(PumpNodeViewModel); default: return null; } } public static T DeepCopy(this T obj) { dynamic retval; using (MemoryStream ms = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); //序列化成流 bf.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); //反序列化成对象 retval = bf.Deserialize(ms); ms.Close(); } return (T)retval; } public static void ClearFileReadOnly(string filePath) { if (File.Exists(filePath)) { // 获取当前文件属性 FileAttributes attributes = File.GetAttributes(filePath); // 如果文件被设置为只读,则移除只读属性 if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes &= ~FileAttributes.ReadOnly; // 移除只读标志 // 设置新的文件属性 File.SetAttributes(filePath, attributes); } } } public static void Copy(string source, string destination, bool isOverWrite = true) { //判断源文件是否存在 if (!System.IO.File.Exists(source)) { return; } //判断源文件是否为只读,如果是则更改为非只读 System.IO.FileInfo file = new System.IO.FileInfo(source); if (file.IsReadOnly) { file.IsReadOnly = false; } //判断目标文件夹是否存在,不存在则创建 string dir = System.IO.Path.GetDirectoryName(destination); try { System.IO.File.Copy(source, destination, isOverWrite); } catch (Exception) { } } } }