标签:
public bool Compile(BGProject project, out String errorMessage) { TextCompiler textCompiler = new TextCompiler(project); ImageCompiler imageCompiler = new ImageCompiler(project); XxxCompiler xxxCompiler = new XxxCompiler(project); foreach (CompilerBase compiler in new CompilerBase[] {textCompiler, imageCompiler, XxxCompiler}) { compiler.Compile(); if (!compiler.Validate(out errorMessage)) { return false; } } // ... }
protected static void TravelElements<T>(BGProject project, BGElementType elementType, Action<T> action) where T : BGElement { foreach (BGDiagram diagram in project.Diagrams) { foreach (T element in diagram.Elements.Where(e => e.ElementType == elementType)) { action(element); } } }
TravelElements<BGTextElement>(Project, BGElementType.Text, element => { //.... 针对目标元素做相应处理 });
处理该特定问题,这里使用委托的方式,泛型化使其易于使用,你也可以使用TemplateMethod模式
序列化工具类
项目中涉及数据序列化到本地文件,直接使用如下工具类型
public static class SerializeUtility { public static void BinarySave<T>(String filePath, T obj) { using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); } } public static T BinaryLoad<T>(String filePath) { return BinaryLoad<T>(filePath, null); } public static T BinaryLoad<T>(String filePath, SerializationBinder serializationBinder) { if (!File.Exists(filePath)) { return default(T); } using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { IFormatter formatter = new BinaryFormatter(); if (serializationBinder != null) { formatter.Binder = serializationBinder; } return (T)formatter.Deserialize(stream); } } }
int与byte数组转换工具类
涉及到传输数据至下位机,考虑数据存储格式,编写如下工具类,支持littleEnding与bigEnding
// Created by Ant 2014-4-30 public static class BytesConverterUtility { public static byte[] GetBytes(int value, int length, bool isLittleEndian = true) { if (value < 0) { throw new ArgumentException("value不能为负数"); } if (length > 4) { throw new ArgumentException("length不能>4"); } var rawBytes = BitConverter.GetBytes(value); if (rawBytes.Length < length) { throw new ApplicationException( String.Format("BitConverter.GetBytes返回的字符数{0}小于目标字符数{1}", rawBytes.Length, length)); } var bytes = new byte[length]; if (BitConverter.IsLittleEndian != isLittleEndian) { Array.Reverse(rawBytes); Array.Copy(rawBytes, rawBytes.Length - length, bytes, 0, length); } else { Array.Copy(rawBytes, bytes, length); } return bytes; } public static int ToInt(byte[] bytes, int offset, int length, bool isLittleEndian = true) { if (length == 1) { return bytes[offset]; } var tempBytes = new byte[length]; Array.Copy(bytes, offset, tempBytes, 0, length); if (!isLittleEndian) { Array.Reverse(tempBytes); } switch (length) { case 2: // 特殊处理,转换为无符号int类型,返回时自动转换为Int32 return BitConverter.ToUInt16(tempBytes, 0); case 4: // 注意,这里将数据转换为有符号int类型 return BitConverter.ToInt32(tempBytes, 0); default: throw new ArgumentException("length 长度非标准值"); } } }
工具类型的方便之处在于其独立性,几乎无外部依赖,不需要考虑对其进行初始化,拿来就可以直接使用
标签:
原文地址:http://www.cnblogs.com/gods/p/4201530.html