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)); } }
public FieldWrapper(Type ownerType, FieldInfo info, NanoLocation location, NanoState state, int constructorArg, string name) : base(ownerType, info, info.FieldType, location, state, constructorArg, name) { Info = info; readFunc = InvocationHelper.CreateGetFieldDelegate(ownerType, info); writeFunc = InvocationHelper.CreateSetFieldDelegate(ownerType, info); }
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 serialization settings for the 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="state">Serialization state of the member</param> /// <param name="constructorArg">Constructor argument index. -1 if this member shouldn't be used in constructor</param> /// <param name="location">Location of the member in serialized output</param> /// <param name="serializationName">Name of the member in serialized output</param> /// <remarks>This settings will override the attribtues declared for member. 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 RegisterCustomSettings( Type type, string memberName, NanoState state = NanoState.Serialize, int constructorArg = -1, NanoLocation location = NanoLocation.Auto, string serializationName = null ) { RegisterCustomAttribute( type, memberName, new NanoSerializationAttribute { ConstructorArg = constructorArg, State = state, Name = serializationName, Location = location } ); }
protected MemberWrapper(Type ownerType, MemberInfo memberInfo, Type memberType, NanoLocation location, NanoState state, int constructorArg, string name) { OwnerType = ownerType; MemberInfo = memberInfo; MemberType = memberType; State = state; ConstructorArg = constructorArg; Name = name ?? memberInfo.Name; TypeCategory = SerializationBase.GetTypeCategory(memberType); if (location == NanoLocation.Auto) { Location = TypeCategory == TypeCategory.Primitive || TypeCategory == TypeCategory.Enum ? NanoLocation.Attribute : NanoLocation.SubNode; } else { Location = location; } GenericArguments = TypeCache.GetTypeGenericArgs(memberType); }
public PropertyWrapper(Type ownerType, PropertyInfo info, NanoLocation location, NanoState state, int constructorArg, string name) : base(ownerType, info, info.PropertyType, location, state, constructorArg, name) { Info = info; if (info.CanWrite) { writeAction = InvocationHelper.CreateSetDelegate(ownerType, info.PropertyType, info.SetMethod); } readFunc = InvocationHelper.CreateGetDelegate(ownerType, info.PropertyType, info.GetMethod); CanWrite = info.CanWrite; CanRead = info.CanRead; IsPrivate = (!info.CanRead || info.GetMethod.IsPrivate) && (!info.CanWrite || info.SetMethod.IsPrivate); }
public CustomConstructorTestClass(string b, NanoState c, int a) { A = a; B = b; C = c; }