private void UpdateFields(ref int maxArgIndex) { foreach (FieldInfo info in Type.GetFields(BindingFlags.Instance | BindingFlags.Public)) { NanoSerializationAttribute attr = CustomStore.FindAttribute(Type, info.Name); if (attr == null) { attr = info.GetCustomAttribute <NanoSerializationAttribute>(); } if (attr != null && attr.State == NanoState.Ignore) { continue; } NanoLocation location = NanoLocation.Auto; NanoState state = NanoState.Serialize; string name = null; int constructorArg = -1; if (attr != null) { location = attr.Location; state = attr.State; constructorArg = attr.ConstructorArg; if (constructorArg > maxArgIndex) { maxArgIndex = constructorArg; } name = attr.Name; } Fields.Add(new FieldWrapper(Type, info, location, state, constructorArg, name)); } }
private void UpdateProperties(ref int maxArgIndex) { foreach (PropertyInfo info in Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (!info.CanRead) { continue; } ParameterInfo[] indexParams = info.GetIndexParameters(); // exclude properties, that returns the same struct to void infinite recursion if (!Type.IsClass && info.PropertyType == Type) { continue; } // indexers are not supported if (indexParams.Length > 0) { continue; } NanoSerializationAttribute attr = CustomStore.FindAttribute(Type, info.Name); if (attr == null) { attr = info.GetCustomAttribute <NanoSerializationAttribute>(); } NanoLocation location = NanoLocation.Auto; NanoState state = NanoState.Serialize; int constructorArg = -1; string name = null; if (attr != null) { if (attr.State == NanoState.Ignore) { continue; } location = attr.Location; constructorArg = attr.ConstructorArg; if (constructorArg > maxArgIndex) { maxArgIndex = constructorArg; } state = attr.State; name = attr.Name; } Properties.Add(new PropertyWrapper(Type, info, location, state, constructorArg, name)); } }
/// <summary> /// Register custom attribute for type member. /// </summary> /// <param name="type">Type for what member to register</param> /// <param name="memberName">Name of memeber for which to register attribute. Using nameof is recommended</param> /// <param name="attribute">Attribute to register</param> /// <remarks>This attributes will override the attributes declared for members. Use these methods when you need to declare /// custom serialization settings for type while can't declare it in usual way.</remarks> public static void RegisterCustomAttribute(Type type, string memberName, NanoSerializationAttribute attribute) { Dictionary <string, NanoSerializationAttribute> dict; if (!customAttributes.TryGetValue(type, out dict)) { dict = new Dictionary <string, NanoSerializationAttribute>(); customAttributes.Add(type, dict); } dict[memberName] = attribute; }