/// <summary> /// Creates a new ValueMember instance /// </summary> public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue) : this(model, fieldNumber, memberType, itemType, defaultType, dataFormat) { if (member == null) { throw new ArgumentNullException("member"); } if (parentType == null) { throw new ArgumentNullException("parentType"); } if (fieldNumber < 1 && !parentType.IsEnum) { throw new ArgumentOutOfRangeException("fieldNumber"); } this.member = member; this.parentType = parentType; if (fieldNumber < 1 && !parentType.IsEnum) { throw new ArgumentOutOfRangeException("fieldNumber"); } if (defaultValue != null && !memberType.IsInstanceOfType(defaultValue)) { defaultValue = ParseDefaultValue(memberType, defaultValue); } this.defaultValue = defaultValue; MetaType type = model.FindWithoutAdd(memberType); if (type != null) { this.asReference = type.AsReferenceDefault; } }
// Token: 0x060003F5 RID: 1013 RVA: 0x00014D68 File Offset: 0x00012F68 public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue) : this(model, fieldNumber, memberType, itemType, defaultType, dataFormat) { if (member == null) { throw new ArgumentNullException("member"); } if (parentType == null) { throw new ArgumentNullException("parentType"); } if (fieldNumber < 1 && !Helpers.IsEnum(parentType)) { throw new ArgumentOutOfRangeException("fieldNumber"); } this.member = member; this.parentType = parentType; if (fieldNumber < 1 && !Helpers.IsEnum(parentType)) { throw new ArgumentOutOfRangeException("fieldNumber"); } if (defaultValue != null && model.MapType(defaultValue.GetType()) != memberType) { defaultValue = ValueMember.ParseDefaultValue(memberType, defaultValue); } this.defaultValue = defaultValue; MetaType metaType = model.FindWithoutAdd(memberType); if (metaType != null) { this.asReference = metaType.AsReferenceDefault; return; } this.asReference = MetaType.GetAsReferenceDefault(model, memberType); }
/// <summary> /// Creates a new ValueMember instance /// </summary> public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue) : this(model, fieldNumber, memberType, itemType, defaultType, dataFormat) { if (member == null) { throw new ArgumentNullException("member"); } if (parentType == null) { throw new ArgumentNullException("parentType"); } if (fieldNumber < 1 && !Helpers.IsEnum(parentType)) { throw new ArgumentOutOfRangeException("fieldNumber"); } this.member = member; this.parentType = parentType; if (fieldNumber < 1 && !Helpers.IsEnum(parentType)) { throw new ArgumentOutOfRangeException("fieldNumber"); } //#if WINRT if (defaultValue != null && model.MapType(defaultValue.GetType()) != memberType) //#else // if (defaultValue != null && !memberType.IsInstanceOfType(defaultValue)) //#endif { defaultValue = ParseDefaultValue(memberType, defaultValue); } this.defaultValue = defaultValue; MetaType type = model.FindWithoutAdd(memberType); if (type != null) { this.asReference = type.AsReferenceDefault; } else { // we need to scan the hard way; can't risk recursion by fully walking it this.asReference = MetaType.GetAsReferenceDefault(model, memberType); } }
/// <summary> /// Creates a new ValueMember instance /// </summary> public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue) : this(model, fieldNumber,memberType, itemType, defaultType, dataFormat) { if (member == null) throw new ArgumentNullException("member"); if (parentType == null) throw new ArgumentNullException("parentType"); if (fieldNumber < 1 && !parentType.IsEnum) throw new ArgumentOutOfRangeException("fieldNumber"); this.member = member; this.parentType = parentType; if (fieldNumber < 1 && !parentType.IsEnum) throw new ArgumentOutOfRangeException("fieldNumber"); if (defaultValue != null && !memberType.IsInstanceOfType(defaultValue)) { defaultValue = ParseDefaultValue(memberType, defaultValue); } this.defaultValue = defaultValue; MetaType type = model.FindWithoutAdd(memberType); if (type != null) this.asReference = type.AsReferenceDefault; }
/// <summary> /// Creates a new ValueMember instance /// </summary> public ValueMember(RuntimeTypeModel model, Type parentType, int fieldNumber, MemberInfo member, Type memberType, Type itemType, Type defaultType, DataFormat dataFormat, object defaultValue) : this(model, fieldNumber, memberType, itemType, defaultType, dataFormat) { if (parentType == null) { throw new ArgumentNullException(nameof(parentType)); } if (fieldNumber < 1 && !parentType.IsEnum) { throw new ArgumentOutOfRangeException(nameof(fieldNumber)); } Member = member ?? throw new ArgumentNullException(nameof(member)); ParentType = parentType; if (fieldNumber < 1 && !parentType.IsEnum) { throw new ArgumentOutOfRangeException(nameof(fieldNumber)); } if (defaultValue != null && (defaultValue.GetType() != memberType)) { defaultValue = ParseDefaultValue(memberType, defaultValue); } _defaultValue = defaultValue; MetaType type = model.FindWithoutAdd(memberType); #if FEAT_DYNAMIC_REF if (type != null) { AsReference = type.AsReferenceDefault; } else { // we need to scan the hard way; can't risk recursion by fully walking it AsReference = MetaType.GetAsReferenceDefault(memberType); } #endif }
internal void ApplyDefaultBehaviour() { if (model.FindWithoutAdd(type.BaseType) == null && GetContractFamily(type.BaseType, null) != MetaType.AttributeFamily.None) { model.FindOrAddAuto(type.BaseType, true, false, false); } object[] typeAttribs = type.GetCustomAttributes(true); AttributeFamily family = GetContractFamily(type, typeAttribs); bool isEnum = type.IsEnum; if (family == AttributeFamily.None && !isEnum) { return; // and you'd like me to do what, exactly? } BasicList partialIgnores = null, partialMembers = null; for (int i = 0; i < typeAttribs.Length; i++) { if (!isEnum && typeAttribs[i] is ProtoIncludeAttribute) { ProtoIncludeAttribute pia = (ProtoIncludeAttribute)typeAttribs[i]; AddSubType(pia.Tag, pia.KnownType); } if (typeAttribs[i] is ProtoPartialIgnoreAttribute) { if (partialIgnores == null) { partialIgnores = new BasicList(); } partialIgnores.Add(((ProtoPartialIgnoreAttribute)typeAttribs[i]).MemberName); } if (!isEnum && typeAttribs[i] is ProtoPartialMemberAttribute) { if (partialMembers == null) { partialMembers = new BasicList(); } partialMembers.Add(typeAttribs[i]); } } MethodInfo[] callbacks = null; foreach (MemberInfo member in type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { if (member.DeclaringType != type) { continue; } if (member.IsDefined(typeof(ProtoIgnoreAttribute), true)) { continue; } if (partialIgnores != null && partialIgnores.Contains(member.Name)) { continue; } switch (member.MemberType) { case MemberTypes.Property: case MemberTypes.Field: ValueMember vm = ApplyDefaultBehaviour(isEnum, family, member, partialMembers); if (vm != null) { Add(vm); } break; case MemberTypes.Method: if (isEnum) { continue; } MethodInfo method = (MethodInfo)member; object[] memberAttribs = Attribute.GetCustomAttributes(method); if (memberAttribs != null && memberAttribs.Length > 0) { CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7); } break; } } if (callbacks != null) { SetCallbacks(callbacks[0] ?? callbacks[4], callbacks[1] ?? callbacks[5], callbacks[2] ?? callbacks[6], callbacks[3] ?? callbacks[7]); } }
internal void ApplyDefaultBehaviour() { if (type.BaseType != null && model.FindWithoutAdd(type.BaseType) == null && GetContractFamily(type.BaseType, null) != MetaType.AttributeFamily.None) { model.FindOrAddAuto(type.BaseType, true, false, false); } object[] typeAttribs = type.GetCustomAttributes(true); AttributeFamily family = GetContractFamily(type, typeAttribs); bool isEnum = type.IsEnum; if (family == AttributeFamily.None && !isEnum) { return; // and you'd like me to do what, exactly? } BasicList partialIgnores = null, partialMembers = null; int dataMemberOffset = 0, implicitFirstTag = 1; bool inferTagByName = model.InferTagFromNameDefault; ImplicitFields implicitMode = ImplicitFields.None; for (int i = 0; i < typeAttribs.Length; i++) { Attribute item = (Attribute)typeAttribs[i]; if (!isEnum && item is ProtoIncludeAttribute) { ProtoIncludeAttribute pia = (ProtoIncludeAttribute)item; AddSubType(pia.Tag, pia.KnownType); } if (item is ProtoPartialIgnoreAttribute) { if (partialIgnores == null) { partialIgnores = new BasicList(); } partialIgnores.Add(((ProtoPartialIgnoreAttribute)item).MemberName); } if (!isEnum && item is ProtoPartialMemberAttribute) { if (partialMembers == null) { partialMembers = new BasicList(); } partialMembers.Add(item); } if (!isEnum && item is ProtoContractAttribute) { ProtoContractAttribute pca = (ProtoContractAttribute)item; dataMemberOffset = pca.DataMemberOffset; if (pca.InferTagFromNameHasValue) { inferTagByName = pca.InferTagFromName; } implicitMode = pca.ImplicitFields; UseConstructor = !pca.SkipConstructor; if (pca.ImplicitFirstTag > 0) { implicitFirstTag = pca.ImplicitFirstTag; } } } if (implicitMode != ImplicitFields.None) { family &= AttributeFamily.ProtoBuf; // with implicit fields, **only** proto attributes are important } MethodInfo[] callbacks = null; BasicList members = new BasicList(); foreach (MemberInfo member in type.GetMembers(isEnum ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { if (member.DeclaringType != type) { continue; } if (member.IsDefined(typeof(ProtoIgnoreAttribute), true)) { continue; } if (partialIgnores != null && partialIgnores.Contains(member.Name)) { continue; } bool forced = false, isPublic, isField; Type effectiveType; switch (member.MemberType) { case MemberTypes.Property: PropertyInfo property = (PropertyInfo)member; effectiveType = property.PropertyType; isPublic = property.GetGetMethod(false) != null; isField = false; goto ProcessMember; case MemberTypes.Field: FieldInfo field = (FieldInfo)member; effectiveType = field.FieldType; isPublic = field.IsPublic; isField = true; ProcessMember: switch (implicitMode) { case ImplicitFields.AllFields: if (isField) { forced = true; } break; case ImplicitFields.AllPublic: if (isPublic) { forced = true; } break; } if (effectiveType.IsSubclassOf(typeof(Delegate))) { continue; // we just don't like delegate types ;p } ProtoMemberAttribute normalizedAttribute = NormalizeProtoMember(member, family, forced, isEnum, partialMembers, dataMemberOffset, inferTagByName); if (normalizedAttribute != null) { members.Add(normalizedAttribute); } break; case MemberTypes.Method: if (isEnum) { continue; } MethodInfo method = (MethodInfo)member; object[] memberAttribs = Attribute.GetCustomAttributes(method); if (memberAttribs != null && memberAttribs.Length > 0) { CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeSerializationAttribute", ref callbacks, 0); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterSerializationAttribute", ref callbacks, 1); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoBeforeDeserializationAttribute", ref callbacks, 2); CheckForCallback(method, memberAttribs, "ProtoBuf.ProtoAfterDeserializationAttribute", ref callbacks, 3); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializingAttribute", ref callbacks, 4); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnSerializedAttribute", ref callbacks, 5); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializingAttribute", ref callbacks, 6); CheckForCallback(method, memberAttribs, "System.Runtime.Serialization.OnDeserializedAttribute", ref callbacks, 7); } break; } } ProtoMemberAttribute[] arr = new ProtoMemberAttribute[members.Count]; members.CopyTo(arr, 0); if (inferTagByName || implicitMode != ImplicitFields.None) { Array.Sort(arr); int nextTag = implicitFirstTag; foreach (ProtoMemberAttribute normalizedAttribute in arr) { if (!normalizedAttribute.TagIsPinned) // if ProtoMember etc sets a tag, we'll trust it { normalizedAttribute.Rebase(nextTag++); } } } foreach (ProtoMemberAttribute normalizedAttribute in arr) { ValueMember vm = ApplyDefaultBehaviour(isEnum, normalizedAttribute); if (vm != null) { Add(vm); } } if (callbacks != null) { SetCallbacks(Coalesce(callbacks, 0, 4), Coalesce(callbacks, 1, 5), Coalesce(callbacks, 2, 6), Coalesce(callbacks, 3, 7)); } }