Пример #1
0
        static BaseFormatter()
        {
            if (typeof(T).ImplementsOrInherits(typeof(UnityEngine.Object)))
            {
                DefaultLoggers.DefaultLogger.LogWarning("A formatter has been created for the UnityEngine.Object type " + typeof(T).Name + " - this is *strongly* discouraged. Unity should be allowed to handle serialization and deserialization of its own weird objects. Remember to serialize with a UnityReferenceResolver as the external index reference resolver in the serialization context.");
            }

            var methods = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            Func <MethodInfo, Action <T, StreamingContext> > selector = (info) =>
            {
                var parameters = info.GetParameters();
                if (parameters.Length == 0)
                {
                    var action = EmitUtilities.CreateInstanceMethodCaller <T>(info);
                    return((value, context) => action(value));
                }
                else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(StreamingContext) && parameters[0].ParameterType.IsByRef == false)
                {
                    return(EmitUtilities.CreateInstanceMethodCaller <T, StreamingContext>(info));
                }
                else
                {
                    DefaultLoggers.DefaultLogger.LogWarning("The method " + info.GetNiceName() + " has an invalid signature and will be ignored by the serialization system.");
                    return(null);
                }
            };

            OnSerializingCallbacks = methods.Where(n => n.IsDefined(typeof(OnSerializingAttribute), true))
                                     .Select(selector)
                                     .Where(n => n != null)
                                     .ToArray();

            OnSerializedCallbacks = methods.Where(n => n.IsDefined(typeof(OnSerializedAttribute), true))
                                    .Select(selector)
                                    .Where(n => n != null)
                                    .ToArray();

            OnDeserializingCallbacks = methods.Where(n => n.IsDefined(typeof(OnDeserializingAttribute), true))
                                       .Select(selector)
                                       .Where(n => n != null)
                                       .ToArray();

            OnDeserializedCallbacks = methods.Where(n => n.IsDefined(typeof(OnDeserializedAttribute), true))
                                      .Select(selector)
                                      .Where(n => n != null)
                                      .ToArray();
        }