Created
February 16, 2025 14:50
-
-
Save sunmeat/20dd6058dbbdf345da83eaf8f62a8919 to your computer and use it in GitHub Desktop.
DLL side
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Xml.Serialization; | |
| using System.Reflection; | |
| public interface IFileHandler | |
| { | |
| void WriteToFile<T>(string filePath, T data); | |
| T ReadFromFile<T>(string filePath); | |
| void UpdateFile<T>(string filePath, T data); | |
| } | |
| public class TextFileHandler : IFileHandler | |
| { | |
| public void WriteToFile<T>(string filePath, T data) | |
| { | |
| File.WriteAllText(filePath, data.ToString()); | |
| } | |
| public T ReadFromFile<T>(string filePath) | |
| { | |
| string content = File.ReadAllText(filePath); | |
| if (typeof(T) == typeof(string)) | |
| { | |
| return (T)(object)content; | |
| } | |
| throw new InvalidCastException($"Cannot cast content to type {typeof(T).Name} from a text file."); | |
| } | |
| public void UpdateFile<T>(string filePath, T data) | |
| { | |
| WriteToFile(filePath, data); | |
| } | |
| } | |
| public class CsvFileHandler : IFileHandler | |
| { | |
| public void WriteToFile<T>(string filePath, T data) | |
| { | |
| var lines = new List<string>(); | |
| var properties = data?.GetType().GetProperties(); | |
| var header = string.Join(",", properties.Select(p => p.Name)); | |
| lines.Add(header); | |
| var values = string.Join(",", properties.Select(p => p.GetValue(data)?.ToString() ?? string.Empty)); | |
| lines.Add(values); | |
| File.WriteAllLines(filePath, lines); | |
| } | |
| public T ReadFromFile<T>(string filePath) | |
| { | |
| var lines = File.ReadAllLines(filePath); | |
| var properties = typeof(T).GetProperties(); | |
| var values = lines[1].Split(','); | |
| var obj = Activator.CreateInstance<T>(); | |
| for (int i = 0; i < properties.Length; i++) | |
| { | |
| properties[i].SetValue(obj, Convert.ChangeType(values[i], properties[i].PropertyType)); | |
| } | |
| return obj; | |
| } | |
| public void UpdateFile<T>(string filePath, T data) | |
| { | |
| WriteToFile(filePath, data); | |
| } | |
| } | |
| public class XmlFileHandler : IFileHandler | |
| { | |
| public void WriteToFile<T>(string filePath, T data) | |
| { | |
| var serializer = new XmlSerializer(typeof(T)); | |
| using (var writer = new StreamWriter(filePath)) | |
| { | |
| serializer.Serialize(writer, data); | |
| } | |
| } | |
| public T ReadFromFile<T>(string filePath) | |
| { | |
| var serializer = new XmlSerializer(typeof(T)); | |
| using (var reader = new StreamReader(filePath)) | |
| { | |
| return (T)serializer.Deserialize(reader); | |
| } | |
| } | |
| public void UpdateFile<T>(string filePath, T data) | |
| { | |
| WriteToFile(filePath, data); | |
| } | |
| } | |
| public class JsonFileHandler : IFileHandler | |
| { | |
| public void WriteToFile<T>(string filePath, T data) | |
| { | |
| var json = System.Text.Json.JsonSerializer.Serialize(data); | |
| File.WriteAllText(filePath, json); | |
| } | |
| public T ReadFromFile<T>(string filePath) | |
| { | |
| var json = File.ReadAllText(filePath); | |
| return System.Text.Json.JsonSerializer.Deserialize<T>(json); | |
| } | |
| public void UpdateFile<T>(string filePath, T data) | |
| { | |
| WriteToFile(filePath, data); | |
| } | |
| } | |
| public class FileFacade | |
| { | |
| private readonly Dictionary<string, IFileHandler> _handlers; | |
| public FileFacade() | |
| { | |
| _handlers = Assembly.GetExecutingAssembly().GetTypes() | |
| .Where(t => typeof(IFileHandler).IsAssignableFrom(t) && !t.IsInterface) | |
| .ToDictionary(t => t.Name.Replace("FileHandler", "").ToLower(), t => (IFileHandler)Activator.CreateInstance(t)); | |
| } | |
| public void WriteToFile<T>(string filePath, T data, string fileType) | |
| { | |
| var handler = GetHandler(fileType); | |
| handler.WriteToFile(filePath, data); | |
| } | |
| public T ReadFromFile<T>(string filePath, string fileType) | |
| { | |
| var handler = GetHandler(fileType); | |
| return handler.ReadFromFile<T>(filePath); | |
| } | |
| public void UpdateFile<T>(string filePath, T data, string fileType) | |
| { | |
| var handler = GetHandler(fileType); | |
| handler.UpdateFile(filePath, data); | |
| } | |
| private IFileHandler GetHandler(string fileType) | |
| { | |
| var fileTypeLower = fileType.ToLower(); | |
| if (_handlers.ContainsKey(fileTypeLower)) | |
| { | |
| return _handlers[fileTypeLower]; | |
| } | |
| throw new ArgumentException($"Handler for file type '{fileType}' not found."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment