public static ProtoTypeColumn CreateColumn(string name, short id, short index, Type columnType, bool ignored, bool isnullable) { ProtoTypeColumn protoColumn = new ProtoTypeColumn(); protoColumn.Name = name; protoColumn.ID = id; protoColumn.Index = index; protoColumn.ColumnType = columnType; protoColumn.Ignored = ignored; protoColumn.IsNullable = isnullable; { MethodInfo mi = typeof(ProtoTypeColumn).GetMethod("Write", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo mt = mi.MakeGenericMethod(columnType); Type gt = typeof(Action <int, int>).GetGenericTypeDefinition().MakeGenericType(typeof(BinaryWriter), typeof(object)); protoColumn.write = (Action <BinaryWriter, object>)mt.CreateDelegate(gt); } { MethodInfo mi = typeof(ProtoTypeColumn).GetMethod("Read", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo mt = mi.MakeGenericMethod(columnType); Type gt = typeof(Func <int, int>).GetGenericTypeDefinition().MakeGenericType(typeof(BinaryReader), typeof(object)); protoColumn.read = (Func <BinaryReader, object>)mt.CreateDelegate(gt); } { MethodInfo mi = typeof(ProtoTypeColumn).GetMethod("ToString", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo mt = mi.MakeGenericMethod(columnType); Type gt = typeof(Func <int, int>).GetGenericTypeDefinition().MakeGenericType(typeof(object), typeof(string)); protoColumn.toString = (Func <object, string>)mt.CreateDelegate(gt); } { MethodInfo mi = typeof(ProtoTypeColumn).GetMethod("FromString", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo mt = mi.MakeGenericMethod(columnType); Type gt = typeof(Func <int, int>).GetGenericTypeDefinition().MakeGenericType(typeof(string), typeof(object)); protoColumn.fromString = (Func <string, object>)mt.CreateDelegate(gt); } return(protoColumn); }
/// <summary> /// Create a ProtoColumn for a specific type. /// </summary> /// <param name="type">Column type</param> /// <param name="name">Column name</param> /// <param name="id">Column ID</param> /// <returns>protocolumn object</returns> private static IProtoColumn CreateColumn(Type type, string name, short id = 0, short colIndex = 0, bool nullable = false, bool ignored = false) { if (type == typeof(string)) { return(new StringColumn { Name = name, ID = id, Index = colIndex, Ignored = ignored, }); } else if (type == typeof(bool)) { return(new BooleanColumn { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(byte)) { return(new ByteColumn { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(short)) { return(new Int16Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(ushort)) { return(new UInt16Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(int)) { return(new Int32Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(uint)) { return(new UInt32Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(long)) { return(new Int64Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(ulong)) { return(new UInt64Column { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(double)) { return(new DoubleColumn { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(DateTime)) { return(new DateTimeColumn { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(Guid)) { return(new GuidColumn { Name = name, ID = id, Index = colIndex, IsNullable = nullable, Ignored = ignored, }); } else if (type == typeof(byte[])) { return(new ImageColumn { Name = name, ID = id, Index = colIndex, Ignored = ignored, }); } else if (type.IsArray) { IProtoColumn element = CreateColumn(type.GetElementType(), null); return(new ArrayColumn { Name = name, ID = id, Index = colIndex, Element = element }); } else if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(int?).GetGenericTypeDefinition()) { IProtoColumn protoColumn = CreateColumn(type.GetGenericArguments()[0], name, id, colIndex, nullable: true, ignored: ignored); if (type.GetGenericTypeDefinition().MakeGenericType(protoColumn.ColumnType) != type) { throw new ArgumentException(string.Format("ColumnType of {0} do not match ProtoColumn definition", name)); } return(protoColumn); } else if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(List <int>).GetGenericTypeDefinition()) { IProtoColumn element = CreateColumn(type.GetGenericArguments()[0], null); Type listColumnType = typeof(ListColumn <int>).GetGenericTypeDefinition().MakeGenericType(element.ColumnType); IProtoColumn protoColumn = (IProtoColumn)System.Activator.CreateInstance(listColumnType); TypeUtils.SetProperty(protoColumn, "Name", name); TypeUtils.SetProperty(protoColumn, "ID", id); TypeUtils.SetProperty(protoColumn, "Index", colIndex); TypeUtils.SetProperty(protoColumn, "Ignored", ignored); TypeUtils.SetProperty(protoColumn, "Element", element); return(protoColumn); } else if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(HashSet <int>).GetGenericTypeDefinition()) { IProtoColumn element = CreateColumn(type.GetGenericArguments()[0], null); Type listColumnType = typeof(SetColumn <int>).GetGenericTypeDefinition().MakeGenericType(element.ColumnType); IProtoColumn protoColumn = (IProtoColumn)System.Activator.CreateInstance(listColumnType); TypeUtils.SetProperty(protoColumn, "Name", name); TypeUtils.SetProperty(protoColumn, "ID", id); TypeUtils.SetProperty(protoColumn, "Index", colIndex); TypeUtils.SetProperty(protoColumn, "Ignored", ignored); TypeUtils.SetProperty(protoColumn, "Element", element); return(protoColumn); } else if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Dictionary <int, int>).GetGenericTypeDefinition()) { IProtoColumn keyElement = CreateColumn(type.GetGenericArguments()[0], null); IProtoColumn valueElement = CreateColumn(type.GetGenericArguments()[1], null); Type mapColumnType = typeof(MapColumn <int, int>).GetGenericTypeDefinition().MakeGenericType(keyElement.ColumnType, valueElement.ColumnType); IProtoColumn protoColumn = (IProtoColumn)System.Activator.CreateInstance(mapColumnType); TypeUtils.SetProperty(protoColumn, "Name", name); TypeUtils.SetProperty(protoColumn, "ID", id); TypeUtils.SetProperty(protoColumn, "Index", colIndex); TypeUtils.SetProperty(protoColumn, "Ignored", ignored); TypeUtils.SetProperty(protoColumn, "KeyElement", keyElement); TypeUtils.SetProperty(protoColumn, "ValueElement", valueElement); return(protoColumn); } else if (type.GetCustomAttribute <ProtoAttribute>() != null) { return(ProtoTypeColumn.CreateColumn(name, id, colIndex, type, ignored, nullable)); } else { throw new ArgumentException(string.Format("failed to create proto column for type {0}", type)); } }