/// <summary> /// Deserializes an object using PersistableAttribute attributes. /// </summary> /// <param name="obj"> /// </param> /// <param name="info"> /// </param> /// <param name="context"> /// </param> public static void Deserialize(object obj, SerializationInfo info, StreamingContext context) { Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; foreach (PropertyInfo pi in GetProperties(type, persistType)) { DeserializeValue(obj, pi, info, context); } foreach (FieldInfo fi in GetFields(type, persistType)) { DeserializeValue(obj, fi, info, context); } if (typeof(IPostSerializationCallback).IsInstanceOfType(obj)) { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; if (persistContext != null) { persistContext.AddPostSerializationCallback((IPostSerializationCallback)obj); } } }
internal FieldMap(string member, string field, string nullValue, string parameter, PersistType persistType, Type memberType, CustomProvider provider) : base(member, field) { if (memberType == null) { throw new MappingException("Mapping: Field memberType was Missing - " + member); } this.persistType = persistType; this.memberType = memberType; if (nullValue == null) { this.nullValue = null; } else { // Jeff Lanning ([email protected]): Update to support null value expressions and make error message less ambiguous. try { this.nullValue = ConvertNullValue(nullValue, memberType); } catch (Exception ex) { throw new MappingException("Mapping: Failed to convert nullValue '" + nullValue + "' to type " + memberType.FullName + " for member '" + member + "'.", ex); } } if (parameter == null || parameter.Length == 0) { if (persistType != PersistType.Concurrent) { this.parameter = provider.GetParameterDefault(field); } else { // Concurrent Parameter Name Fix by Stephan Wagner (http://www.calac.net) this.parameter = provider.GetParameterDefault(member); } } else { this.parameter = parameter; } }
/// <summary> /// Initializes a new Credential instance. /// </summary> /// <param name="targetName">The name of the credential.</param> /// <param name="comment">A comment associated with the credential.</param> /// <param name="persist">One of the PersistType values.</param> /// <exception cref="NotSupportedException">This functionality requires Windows XP or higher.</exception> /// <exception cref="ArgumentNullException"><b>targetName</b> is a null reference.</exception> /// <exception cref="ArgumentException">One of the input parameters is invalid.</exception> /// <exception cref="CredentialException">An error occurs while trying to retrieve the credential.</exception> /// <remarks><b>targetName</b> must be at least one character and not longer than 32767 characters.<br/> /// <b>comment</b> may be a null reference, but may not be longer than 256 characters.<br/> /// <b>persist</b> may not have the value PersistType.None.</remarks> public Credential(string targetName, string comment, PersistType persist) { Platform.AssertWinXP(); if (targetName == null) { throw new ArgumentNullException("targetName", ResourceController.GetString("Error_ParamNull")); } if (targetName.Length == 0 || targetName.Length > NativeMethods.CRED_MAX_GENERIC_TARGET_NAME_LENGTH) { throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "targetName"); } if (!Enum.IsDefined(typeof(PersistType), persist) || persist == PersistType.None) { throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "persist"); } if (comment != null && comment.Length > NativeMethods.CRED_MAX_STRING_LENGTH) { throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "comment"); } m_Name = targetName.ToLower(CultureInfo.InvariantCulture); m_Comment = comment; m_Persist = persist; m_Contents = new byte[0]; Refresh(); }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"> /// </param> /// <returns> /// The System.Object. /// </returns> /// <remarks> /// Basically for serialize members without needing ISerialize interface. /// </remarks> public static object GetSerializationSurrogate(object obj) { Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; return(GetSerializationSurrogate(obj, persistType)); }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"> /// </param> /// <param name="values"> /// The values. /// </param> /// <param name="persistType"> /// The persist Type. /// </param> /// <remarks> /// Basically for serialize members without needing ISerialize interface. /// </remarks> public static void DeserializationFromSurrogate(object obj, object values, PersistType persistType) { if (obj == null) { throw new ArgumentNullException("obj"); } var dict = values as ListDictionary; if (dict == null) { throw new ArgumentException("Values should be a Dictionary", "values"); } Type type = obj.GetType(); foreach (PropertyInfo pi in GetProperties(type, persistType)) { object value = null; if (dict.Contains(pi.Name)) { value = dict[pi.Name]; } if (value == null) { SetDefaultValue(obj, pi, false); } else { pi.SetValue(obj, value, null); } } foreach (FieldInfo fi in GetFields(type, persistType)) { object value = null; if (dict.Contains(fi.Name)) { value = dict[fi.Name]; } if (value == null) { SetDefaultValue(obj, fi, false); } else { fi.SetValue(obj, value); } } }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"> /// </param> /// <param name="values"> /// The values. /// </param> /// <param name="persistType"> /// The persist Type. /// </param> /// <remarks> /// Basically for serialize members without needing ISerialize interface. /// </remarks> public static void DeserializationFromSurrogate(object obj, object values, PersistType persistType) { if (obj == null) { throw new ArgumentNullException("obj"); } var dict = values as ListDictionary; if (dict == null) { throw new ArgumentException("Values should be a Dictionary", "values"); } Type type = obj.GetType(); foreach (PropertyInfo pi in GetProperties(type, persistType)) { object value = null; if (dict.Contains(pi.Name)) { value = dict[pi.Name]; } if (value == null) { SetDefaultValue(obj, pi, false); } else { pi.SetValue(obj, value, null); } } foreach (FieldInfo fi in GetFields(type, persistType)) { object value = null; if (dict.Contains(fi.Name)) { value = dict[fi.Name]; } if (value == null) { SetDefaultValue(obj, fi, false); } else { fi.SetValue(obj, value); } } }
/// <summary> /// Initializes a new Credential instance. /// </summary> /// <param name="targetName">The name of the credential.</param> /// <param name="comment">A comment associated with the credential.</param> /// <param name="persist">One of the PersistType values.</param> /// <exception cref="NotSupportedException">This functionality requires Windows XP or higher.</exception> /// <exception cref="ArgumentNullException"><b>targetName</b> is a null reference.</exception> /// <exception cref="ArgumentException">One of the input parameters is invalid.</exception> /// <exception cref="CredentialException">An error occurs while trying to retrieve the credential.</exception> /// <remarks><b>targetName</b> must be at least one character and not longer than 32767 characters.<br/> /// <b>comment</b> may be a null reference, but may not be longer than 256 characters.<br/> /// <b>persist</b> may not have the value PersistType.None.</remarks> public Credential(string targetName, string comment, PersistType persist) { Platform.AssertWinXP(); if (targetName == null) throw new ArgumentNullException("targetName", ResourceController.GetString("Error_ParamNull")); if (targetName.Length == 0 || targetName.Length > NativeMethods.CRED_MAX_GENERIC_TARGET_NAME_LENGTH) throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "targetName"); if (!Enum.IsDefined(typeof(PersistType), persist) || persist == PersistType.None) throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "persist"); if (comment != null && comment.Length > NativeMethods.CRED_MAX_STRING_LENGTH) throw new ArgumentException(ResourceController.GetString("Error_ParamInvalid"), "comment"); m_Name = targetName.ToLower(CultureInfo.InvariantCulture); m_Comment = comment; m_Persist = persist; m_Contents = new byte[0]; Refresh(); }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"> /// </param> /// <param name="persistType"> /// </param> /// <returns> /// The System.Object. /// </returns> public static object GetSerializationSurrogate(object obj, PersistType persistType) { Type type = obj.GetType(); IDictionary dict = new ListDictionary(); foreach (PropertyInfo pi in GetProperties(type, persistType)) { dict[pi.Name] = pi.GetValue(obj, null); } foreach (FieldInfo fi in GetFields(type, persistType)) { dict[fi.Name] = fi.GetValue(obj); } return(dict); }
internal FieldMap(string member, string field, string nullValue, string parameter, PersistType persistType, Type memberType, CustomProvider provider) : base(member, field) { if (memberType == null) { throw new MappingException("Mapping: Field memberType was Missing - " + member); } this.persistType = persistType; this.memberType = memberType; if (nullValue == null) { this.nullValue = null; } else { // Jeff Lanning ([email protected]): Update to support null value expressions and make error message less ambiguous. try { this.nullValue = ConvertNullValue(nullValue, memberType); } catch (Exception ex) { throw new MappingException("Mapping: Failed to convert nullValue '" + nullValue + "' to type " + memberType.FullName + " for member '" + member + "'.", ex); } } if (parameter == null || parameter.Length == 0) { if (persistType != PersistType.Concurrent) { this.parameter = provider.GetParameterDefault(field); } else // Concurrent Parameter Name Fix by Stephan Wagner (http://www.calac.net) { this.parameter = provider.GetParameterDefault(member); } } else { this.parameter = parameter; } }
// Includes Null-Value Assistance from Tim Byng (http://www.missioninc.com) internal void AddField(string member, string field, string nullValue, string alias, string parameter, PersistType persistType, CustomProvider provider) { this.AddMember(member); FieldMap[] tempFields = new FieldMap[this.fields.Length + 1]; this.fields.CopyTo(tempFields, 0); tempFields[this.fields.Length] = new FieldMap(member, field, nullValue, parameter, persistType, EntityMap.GetType(this.Member(member)), provider); int keyIndex = -1; for (int index = 0; index < this.keyMembers.Length; index++) { if (this.keyMembers[index].Equals(member)) { keyIndex = index; } } if (keyIndex > -1) { FieldMap[] tempKeyFields = new FieldMap[this.keyFields.Length + 1]; this.keyFields.CopyTo(tempKeyFields, 0); tempKeyFields[this.keyFields.Length] = tempFields[this.fields.Length]; this.keyFields = tempKeyFields; } if (persistType == PersistType.ReadOnly) { this.readOnlyCount++; } if (persistType == PersistType.Concurrent) { this.concurrentCount++; } this.fields = tempFields; this.helper.Add(alias, this.fields.Length - 1); if (this.baseEntity != null && !this.baseEntity.subFields.ContainsKey(field)) { this.baseEntity.subFields.Add(field, this.fields[this.fields.Length - 1]); } }
/// <summary> /// Serializes an object using PersistableAttribute attributes /// </summary> /// <param name="obj"></param> /// <param name="info"></param> /// <param name="context"></param> public static void Serialize(object obj, SerializationInfo info, StreamingContext context) { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; if (persistContext != null) { if (persistContext.RetrieveCacheInfo(obj, info)) { return; } } Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; if (pattr != null && pattr.Version != 0) { info.AddValue("PersistVersion", pattr.Version); } foreach (PropertyInfo pi in GetProperties(type, persistType)) { SerializeValue(obj, pi, info, context); } foreach (FieldInfo fi in GetFields(type, persistType)) { SerializeValue(obj, fi, info, context); } if (persistContext != null) { persistContext.StoreCacheInfo(obj, info); } }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"></param> /// <param name="persistType"></param> /// <returns></returns> public static object GetSerializationSurrogate(object obj, PersistType persistType) { Type type = obj.GetType(); IDictionary dict = new ListDictionary(); foreach (PropertyInfo pi in GetProperties(type, persistType)) dict[pi.Name] = pi.GetValue(obj, null); foreach (FieldInfo fi in GetFields(type, persistType)) dict[fi.Name] = fi.GetValue(obj); return dict; }
/// <summary> /// Return Properties which match the PeristType search criteria. /// </summary> /// <param name="type"> /// </param> /// <param name="searchType"> /// </param> /// <returns> /// The System.Reflection.PropertyInfo[]. /// </returns> private static PropertyInfo[] GetProperties(Type type, PersistType searchType) { // remove flags not meaningful for comparisons PersistType modSearchType = searchType & ~(PersistType.DeclaredOnly | PersistType.Properties | PersistType.Fields); var props = new ArrayList(); var bi = BindingFlags.SetProperty; if ((searchType & PersistType.DeclaredOnly) != 0) { // Only get declared properties bi |= BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; foreach (PropertyInfo pi in type.GetProperties(bi)) { object[] attrs = pi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length == 0) { continue; } var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { continue; } if (modSearchType == PersistType.None || ((pattr.Flags & modSearchType) == modSearchType)) { props.Add(pi); } } } else { if ((searchType & PersistType.Public) != 0) { bi |= BindingFlags.Public; } if ((searchType & PersistType.NonPublic) != 0) { bi |= BindingFlags.NonPublic; } if ((searchType & PersistType.Static) != 0) { bi |= BindingFlags.Static; } if ((searchType & PersistType.Instance) != 0) { bi |= BindingFlags.Instance; } foreach (PropertyInfo pi in type.GetProperties(bi)) { object[] attrs = pi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length > 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { continue; } } props.Add(pi); } } return((PropertyInfo[])props.ToArray(typeof(PropertyInfo))); }
/// <summary> /// Return Properties which match the PeristType search criteria. /// </summary> /// <param name="type"> /// </param> /// <param name="searchType"> /// </param> /// <returns> /// The System.Reflection.FieldInfo[]. /// </returns> private static FieldInfo[] GetFields(Type type, PersistType searchType) { // remove flags not meaningful for comparisons PersistType modSearchType = searchType & ~(PersistType.DeclaredOnly | PersistType.Fields | PersistType.Fields); var fields = new ArrayList(); var bi = BindingFlags.SetField; if ((searchType & PersistType.DeclaredOnly) != 0) { // Only get declared Fields bi |= BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; while (type != typeof(object)) { foreach (FieldInfo fi in type.GetFields(bi)) { object[] attrs = fi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length == 0) { continue; } var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { continue; } if (modSearchType == PersistType.None || ((pattr.Flags & modSearchType) == modSearchType)) { bool found = false; foreach (FieldInfo fi2 in fields) { if (fi2.Name == fi.Name) { found = true; break; } } if (!found) { fields.Add(fi); } } } type = type.BaseType; } } else { if ((searchType & PersistType.Public) != 0) { bi |= BindingFlags.Public; } if ((searchType & PersistType.NonPublic) != 0) { bi |= BindingFlags.NonPublic; } if ((searchType & PersistType.Static) != 0) { bi |= BindingFlags.Static; } if ((searchType & PersistType.Instance) != 0) { bi |= BindingFlags.Instance; } while (type != typeof(object)) { foreach (FieldInfo fi in type.GetFields(bi)) { object[] attrs = fi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length > 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { continue; } } bool found = false; foreach (FieldInfo fi2 in fields) { if (fi2.Name == fi.Name) { found = true; break; } } if (!found) { fields.Add(fi); } } type = type.BaseType; } } return((FieldInfo[])fields.ToArray(typeof(FieldInfo))); }
/// <summary> /// Return Properties which match the PeristType search criteria. /// </summary> /// <param name="type"></param> /// <param name="searchType"></param> /// <returns></returns> private static PropertyInfo[] GetProperties(Type type, PersistType searchType) { // remove flags not meaningful for comparisons PersistType modSearchType = searchType & ~(PersistType.DeclaredOnly | PersistType.Properties | PersistType.Fields); var props = new ArrayList(); BindingFlags bi = BindingFlags.SetProperty; if ((searchType & PersistType.DeclaredOnly) != 0) // Only get declared properties { bi |= BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance ; foreach (PropertyInfo pi in type.GetProperties(bi)) { object[] attrs = pi.GetCustomAttributes(typeof (PersistableAttribute), false); if (attrs.Length == 0) continue; var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) continue; if (modSearchType == PersistType.None || ((pattr.Flags & modSearchType) == modSearchType)) props.Add(pi); } } else { if ((searchType & PersistType.Public) != 0) bi |= BindingFlags.Public; if ((searchType & PersistType.NonPublic) != 0) bi |= BindingFlags.NonPublic; if ((searchType & PersistType.Static) != 0) bi |= BindingFlags.Static; if ((searchType & PersistType.Instance) != 0) bi |= BindingFlags.Instance; foreach (PropertyInfo pi in type.GetProperties(bi)) { object[] attrs = pi.GetCustomAttributes(typeof (PersistableAttribute), false); if (attrs.Length > 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) continue; } props.Add(pi); } } return (PropertyInfo[]) props.ToArray(typeof (PropertyInfo)); }
/// <summary> /// Return Properties which match the PeristType search criteria. /// </summary> /// <param name="type"></param> /// <param name="searchType"></param> /// <returns></returns> private static FieldInfo[] GetFields(Type type, PersistType searchType) { // remove flags not meaningful for comparisons PersistType modSearchType = searchType & ~(PersistType.DeclaredOnly | PersistType.Fields | PersistType.Fields); var fields = new ArrayList(); BindingFlags bi = BindingFlags.SetField; if ((searchType & PersistType.DeclaredOnly) != 0) // Only get declared Fields { bi |= BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance ; while (type != typeof (object)) { foreach (FieldInfo fi in type.GetFields(bi)) { object[] attrs = fi.GetCustomAttributes(typeof (PersistableAttribute), false); if (attrs.Length == 0) continue; var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) continue; if (modSearchType == PersistType.None || ((pattr.Flags & modSearchType) == modSearchType)) { bool found = false; foreach (FieldInfo fi2 in fields) { if (fi2.Name == fi.Name) { found = true; break; } } if (!found) fields.Add(fi); } } type = type.BaseType; } } else { if ((searchType & PersistType.Public) != 0) bi |= BindingFlags.Public; if ((searchType & PersistType.NonPublic) != 0) bi |= BindingFlags.NonPublic; if ((searchType & PersistType.Static) != 0) bi |= BindingFlags.Static; if ((searchType & PersistType.Instance) != 0) bi |= BindingFlags.Instance; while (type != typeof (object)) { foreach (FieldInfo fi in type.GetFields(bi)) { object[] attrs = fi.GetCustomAttributes(typeof (PersistableAttribute), false); if (attrs.Length > 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) continue; } bool found = false; foreach (FieldInfo fi2 in fields) { if (fi2.Name == fi.Name) { found = true; break; } } if (!found) fields.Add(fi); } type = type.BaseType; } } return (FieldInfo[]) fields.ToArray(typeof (FieldInfo)); }
// Includes Null-Value Assistance from Tim Byng (http://www.missioninc.com) internal void AddField(string member, string field, string nullValue, string alias, string parameter, PersistType persistType, CustomProvider provider) { this.AddMember(member); FieldMap[] tempFields = new FieldMap[this.fields.Length + 1]; this.fields.CopyTo(tempFields, 0); tempFields[this.fields.Length] = new FieldMap(member, field, nullValue, parameter, persistType, EntityMap.GetType(this.Member(member)), provider); int keyIndex = -1; for (int index = 0; index < this.keyMembers.Length; index++) { if (this.keyMembers[index].Equals(member)) keyIndex = index; } if (keyIndex > -1) { FieldMap[] tempKeyFields = new FieldMap[this.keyFields.Length + 1]; this.keyFields.CopyTo(tempKeyFields, 0); tempKeyFields[this.keyFields.Length] = tempFields[this.fields.Length]; this.keyFields = tempKeyFields; } if (persistType == PersistType.ReadOnly) this.readOnlyCount++; if (persistType == PersistType.Concurrent) this.concurrentCount++; this.fields = tempFields; this.helper.Add(alias, this.fields.Length - 1); if (this.baseEntity != null && !this.baseEntity.subFields.ContainsKey(field)) { this.baseEntity.subFields.Add(field, this.fields[this.fields.Length - 1]); } }