private void ConfigureMetaType(MetaType metaType, TypeDescription typeDescription) { if (typeDescription.SurrogateType != null) { var surrogateType = typeDescription.SurrogateType.ResolvedType; if (surrogateType == null) { throw new NotImplementedException(); } metaType.SetSurrogate(surrogateType); } else { foreach (var fieldDescription in typeDescription.Fields) { var member = fieldDescription.Member; if (member != null) { var field = metaType.AddField(fieldDescription.MemberId, member.Name); field.IsRequired = true; var fieldType = fieldDescription.FieldTypeDescription.ResolvedType; if (IsPrimitiveArray(fieldType)) { field.IsPacked = true; } } } } }
private static void LoadProtoTypeMember(Type myEntity, List <PropertyInfo> Properties, MetaType metaType, List <FieldInfo> Fields) { Properties.ForEach((o) => { try { var fieldNumber = (o.GetCustomAttributes(typeof(ProtoMemberAttribute), false)[0] as ProtoMemberAttribute).Tag; if (metaType[fieldNumber] == null) { metaType.Add(fieldNumber, o.Name); } } //忽略异常 catch (Exception ex) { TraceLog.WriteError("Loading protobuf type \"{0}.{1}\" property error:{2}", myEntity.FullName, o.Name, ex); } }); Fields.ForEach((o) => { try { var fieldNumber = (o.GetCustomAttributes(typeof(ProtoMemberAttribute), false)[0] as ProtoMemberAttribute).Tag; if (metaType[fieldNumber] == null) { metaType.AddField(fieldNumber, o.Name); } } //忽略异常 catch (Exception ex) { TraceLog.WriteError("Loading protobuf type \"{0}.{1}\" field error:{2}", myEntity.FullName, o.Name, ex); } }); }
public static MetaType AddOptional(this MetaType metaType, int fieldNumber, string memberName) { var field = metaType.AddField(fieldNumber, memberName); field.IsRequired = false; return(metaType); }
public static ValueMember AddFieldCopy(this MetaType metaType, ValueMember originalValueMember) { var valueMember = metaType.AddField(originalValueMember.FieldNumber, originalValueMember.Name, originalValueMember.ItemType, originalValueMember.DefaultType); valueMember.AsReference = originalValueMember.AsReference; valueMember.DataFormat = originalValueMember.DataFormat; valueMember.DefaultValue = originalValueMember.DefaultValue; valueMember.DynamicType = originalValueMember.DynamicType; valueMember.IsPacked = originalValueMember.IsPacked; valueMember.IsRequired = originalValueMember.IsRequired; valueMember.IsStrict = originalValueMember.IsStrict; valueMember.SupportNull = originalValueMember.SupportNull; return(valueMember); }
public static void IncludeParents(this RuntimeTypeModel model, ITypeIdentifier typeIdentifier, MetaType type) { if (type.Type.BaseType == typeof(object) || type.Type.BaseType == null) { return; } var metaBase = model.Add(type.Type.BaseType, true); model.IncludeParents(typeIdentifier, metaBase); if (IsRegisteredType(typeIdentifier, type) || IsRegisteredGenericType(typeIdentifier, type)) { foreach (var field in metaBase.GetFields()) { if (!type.GetFields().Select(x => x.FieldNumber).Contains(field.FieldNumber)) { type.AddField(field.FieldNumber, field.Member.Name); } } } }
static void AddField(MetaType mt, ValueMember field) { try { var newField = mt.AddField(field.FieldNumber, field.Member?.Name ?? field.Name, field.ItemType, field.DefaultType); newField.DataFormat = field.DataFormat; newField.IsMap = field.IsMap; newField.IsPacked = field.IsPacked; newField.IsRequired = field.IsRequired; newField.IsStrict = field.IsStrict; newField.DefaultValue = field.DefaultValue; newField.MapKeyFormat = field.MapKeyFormat; newField.MapValueFormat = field.MapValueFormat; newField.Name = field.Name; newField.OverwriteList = field.OverwriteList; // newField.SupportNull = field.SupportNull; } catch (Exception ex) { throw new InvalidOperationException($"Error adding field {field.Member?.Name}: {ex.Message}", ex); } }
public static void Initialize() { typeModel = TypeModel.Create(); typeModel.UseImplicitZeroDefaults = false; MetaType metatype = null; metatype = typeModel.Add(typeof(Vector3), true); metatype.AddField(1, "x"); metatype.AddField(2, "y"); metatype.AddField(3, "z"); metatype = typeModel.Add(typeof(Vector2), true); metatype.AddField(1, "x"); metatype.AddField(2, "y"); metatype = typeModel.Add(typeof(Color), true); metatype.AddField(1, "a"); metatype.AddField(2, "b"); metatype.AddField(3, "g"); metatype.AddField(4, "r"); metatype = typeModel.Add(typeof(Resolution), true); metatype.AddField(1, "width"); metatype.AddField(2, "height"); metatype.AddField(3, "refreshRate"); }
public IMapProperty <T> MapProperty(Expression <Func <T, object> > expression) { _metaType.AddField(_index, GetPropertyName(expression)); _index++; return(this); }
public static MetaType Decorate(Type type) { lock (MetaTypeCache) { MetaType mtc = null; if (!MetaTypeCache.TryGetValue(type, out mtc) && !RuntimeTypeModel.Default.CanSerialize(type)) { var types = GetParentTypes(type).Concat(new[] { type }).Where(e => !e.IsInterface); var fieldsGroup = types.Select(t => t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)) .SelectMany(t => t).Where(t => !MetaTypeCache.ContainsKey(t.DeclaringType)).Distinct().GroupBy(e => e.DeclaringType); if (fieldsGroup.Any()) { foreach (var fields in fieldsGroup) { if (!RuntimeTypeModel.Default.CanSerialize(fields.Key)) { MetaTypeCache.Add(fields.Key, mtc = RuntimeTypeModel.Default.Add(fields.Key, false)); var vms = fields.OrderBy(e => e.Name).Select((f, i) => mtc.AddField(i + 1, f.Name)).ToList(); vms.ForEach(vm => Decorate(vm.ItemType ?? vm.DefaultType ?? vm.MemberType)); //preprocess inside vm } } } else { MetaTypeCache.Add(type, RuntimeTypeModel.Default.Add(type, true)); } if (!MetaTypeCache.TryGetValue(type, out mtc)) { return(null); } } return(mtc); } }