private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info) { info = new ElementInfo(); // Are we ignoring this property? if (ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute)) != null) return false; var prop = member as PropertyInfo; var field = member as FieldInfo; // If we can write or read from it we can skip it. if (prop != null && (!prop.CanWrite || !prop.CanRead)) return false; // Default the to member name as the element name. info.Name = member.Name; var attrib = ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerAttribute)) as ContentSerializerAttribute; if (attrib != null) { if (!string.IsNullOrEmpty(attrib.ElementName)) info.Name = attrib.ElementName; } else if (prop != null) { if (!ReflectionHelpers.PropertyIsPublic(prop)) return false; } else if (field != null) { if (!field.IsPublic) return false; } if (prop != null) { info.Serializer = serializer.GetTypeSerializer(prop.PropertyType); info.Setter = (o, v) => prop.SetValue(o, v, null); info.Getter = (o) => prop.GetValue(o, null); } else if (field != null) { info.Serializer = serializer.GetTypeSerializer(field.FieldType); info.Setter = field.SetValue; info.Getter = field.GetValue; } return true; }
protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, NamedValueDictionary <T> value) { foreach (var kvp in value) { callback(serializer.GetTypeSerializer(typeof(T)), kvp.Value); } }
protected internal override void Initialize(IntermediateSerializer serializer) { _keySerializer = serializer.GetTypeSerializer(typeof(string)); _keyFormat = new ContentSerializerAttribute { ElementName = "Key", AllowNull = false }; _valueFormat = new ContentSerializerAttribute { ElementName = "Value", AllowNull = typeof(T).IsValueType }; }
internal CollectionHelper(IntermediateSerializer serializer, Type type) { this.targetType = type; Type type2 = CollectionUtils.CollectionElementType(type, false); if (type2 == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.NotACollectionType, new object[] { type })); } this.contentSerializer = serializer.GetTypeSerializer(type2); Type type3 = typeof(ICollection<>).MakeGenericType(new Type[] { type2 }); this.countPropertyGetter = ReflectionEmitUtils.GenerateGetter(type3.GetProperty("Count")); this.addToCollection = ReflectionEmitUtils.GenerateAddToCollection(type3, type2); }
private bool GetElementInfo(IntermediateSerializer serializer, MemberInfo member, out ElementInfo info) { info = new ElementInfo(); // Are we ignoring this property? if (ReflectionHelpers.GetCustomAttribute<ContentSerializerIgnoreAttribute>(member) != null) return false; var prop = member as PropertyInfo; var field = member as FieldInfo; var attrib = ReflectionHelpers.GetCustomAttribute<ContentSerializerAttribute>(member); if (attrib != null) { // Store the attribute for later use. info.Attribute = attrib.Clone(); // Default the to member name as the element name. if (string.IsNullOrEmpty(attrib.ElementName)) info.Attribute.ElementName = member.Name; } else { // We don't have a serializer attribute, so we can // only access this member thru a public field/property. if (prop != null) { // If we don't have at least a public getter then this // property can't be serialized or deserialized in any way. if (prop.GetGetMethod() == null) return false; // If there is a setter, but it's private, then don't include this element // (although technically we could, as long as we have a serializer with // CanDeserializeIntoExistingObject=true for this property type) var setter = prop.GetSetMethod(true); if (setter != null && !setter.IsPublic) return false; // If there is no setter, and we don't have a type serializer // that can deserialize into an existing object, then we have no way // for it to be deserialized. if (setter == null && !serializer.GetTypeSerializer(prop.PropertyType).CanDeserializeIntoExistingObject) return false; // Don't serialize or deserialize indexers. if (prop.GetIndexParameters().Any()) return false; } else if (field != null) { if (!field.IsPublic) return false; } info.Attribute = new ContentSerializerAttribute(); info.Attribute.ElementName = member.Name; } if (prop != null) { info.Serializer = serializer.GetTypeSerializer(prop.PropertyType); if (prop.CanWrite) info.Setter = (o, v) => prop.SetValue(o, v, null); info.Getter = o => prop.GetValue(o, null); } else if (field != null) { info.Serializer = serializer.GetTypeSerializer(field.FieldType); info.Setter = field.SetValue; info.Getter = field.GetValue; } return true; }
protected internal override void ScanChildren(IntermediateSerializer serializer, ChildCallback callback, object value) { if (serializer.AlreadyScanned(value)) return; // First scan the base type. if (_baseSerializer != null) _baseSerializer.ScanChildren(serializer, callback, value); // Now scan our own elements. foreach (var info in _elements) { var elementValue = info.Getter(value); callback(info.Serializer, elementValue); var elementSerializer = info.Serializer; if (elementValue != null) elementSerializer = serializer.GetTypeSerializer(elementValue.GetType()); elementSerializer.ScanChildren(serializer, callback, elementValue); } }
protected internal override void Initialize(IntermediateSerializer serializer) { // If we have a base type then we need to deserialize it first. if (TargetType.BaseType != null) _baseSerializer = serializer.GetTypeSerializer(TargetType.BaseType); // Cache all our serializable properties. var properties = TargetType.GetProperties(_bindingFlags); foreach (var prop in properties) { ElementInfo info; if (GetElementInfo(serializer, prop, out info)) _elements.Add(info); } // Cache all our serializable fields. var fields = TargetType.GetFields(_bindingFlags); foreach (var field in fields) { ElementInfo info; if (GetElementInfo(serializer, field, out info)) _elements.Add(info); } }