Newtonsoft.Json.Serialization.DefaultContractResolver.IsValidCallback C# (CSharp) Method

IsValidCallback() private static method

private static IsValidCallback ( MethodInfo method, ParameterInfo parameters, Type attributeType, MethodInfo currentCallback, Type &prevAttributeType ) : bool
method System.Reflection.MethodInfo
parameters System.Reflection.ParameterInfo
attributeType System.Type
currentCallback System.Reflection.MethodInfo
prevAttributeType System.Type
return bool
    private static bool IsValidCallback(MethodInfo method, ParameterInfo[] parameters, Type attributeType, MethodInfo currentCallback, ref Type prevAttributeType)
    {
      if (!method.IsDefined(attributeType, false))
        return false;

      if (currentCallback != null)
        throw new Exception("Invalid attribute. Both '{0}' and '{1}' in type '{2}' have '{3}'.".FormatWith(CultureInfo.InvariantCulture, method, currentCallback, GetClrTypeFullName(method.DeclaringType), attributeType));

      if (prevAttributeType != null)
        throw new Exception("Invalid Callback. Method '{3}' in type '{2}' has both '{0}' and '{1}'.".FormatWith(CultureInfo.InvariantCulture, prevAttributeType, attributeType, GetClrTypeFullName(method.DeclaringType), method));

      if (method.IsVirtual)
        throw new Exception("Virtual Method '{0}' of type '{1}' cannot be marked with '{2}' attribute.".FormatWith(CultureInfo.InvariantCulture, method, GetClrTypeFullName(method.DeclaringType), attributeType));

      if (method.ReturnType != typeof(void))
        throw new Exception("Serialization Callback '{1}' in type '{0}' must return void.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method));

      if (attributeType == typeof(OnErrorAttribute))
      {
        if (parameters == null || parameters.Length != 2 || parameters[0].ParameterType != typeof(StreamingContext) || parameters[1].ParameterType != typeof(ErrorContext))
          throw new Exception("Serialization Error Callback '{1}' in type '{0}' must have two parameters of type '{2}' and '{3}'.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method, typeof (StreamingContext), typeof(ErrorContext)));
      }
      else
      {
        if (parameters == null || parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
          throw new Exception("Serialization Callback '{1}' in type '{0}' must have a single parameter of type '{2}'.".FormatWith(CultureInfo.InvariantCulture, GetClrTypeFullName(method.DeclaringType), method, typeof(StreamingContext)));
      }

      prevAttributeType = attributeType;

      return true;
    }

Usage Example

Example #1
0
 private void GetCallbackMethodsForType(Type type, out List <SerializationCallback> onSerializing, out List <SerializationCallback> onSerialized, out List <SerializationCallback> onDeserializing, out List <SerializationCallback> onDeserialized, out List <SerializationErrorCallback> onError)
 {
     onSerializing   = null;
     onSerialized    = null;
     onDeserializing = null;
     onDeserialized  = null;
     onError         = null;
     foreach (Type current in this.GetClassHierarchyForType(type))
     {
         MethodInfo   currentCallback  = null;
         MethodInfo   currentCallback2 = null;
         MethodInfo   currentCallback3 = null;
         MethodInfo   currentCallback4 = null;
         MethodInfo   currentCallback5 = null;
         MethodInfo[] methods          = current.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
         for (int i = 0; i < methods.Length; i++)
         {
             MethodInfo methodInfo = methods[i];
             if (!methodInfo.ContainsGenericParameters)
             {
                 Type            type2      = null;
                 ParameterInfo[] parameters = methodInfo.GetParameters();
                 if (DefaultContractResolver.IsValidCallback(methodInfo, parameters, typeof(OnSerializingAttribute), currentCallback, ref type2))
                 {
                     onSerializing = (onSerializing ?? new List <SerializationCallback>());
                     onSerializing.Add(JsonContract.CreateSerializationCallback(methodInfo));
                     currentCallback = methodInfo;
                 }
                 if (DefaultContractResolver.IsValidCallback(methodInfo, parameters, typeof(OnSerializedAttribute), currentCallback2, ref type2))
                 {
                     onSerialized = (onSerialized ?? new List <SerializationCallback>());
                     onSerialized.Add(JsonContract.CreateSerializationCallback(methodInfo));
                     currentCallback2 = methodInfo;
                 }
                 if (DefaultContractResolver.IsValidCallback(methodInfo, parameters, typeof(OnDeserializingAttribute), currentCallback3, ref type2))
                 {
                     onDeserializing = (onDeserializing ?? new List <SerializationCallback>());
                     onDeserializing.Add(JsonContract.CreateSerializationCallback(methodInfo));
                     currentCallback3 = methodInfo;
                 }
                 if (DefaultContractResolver.IsValidCallback(methodInfo, parameters, typeof(OnDeserializedAttribute), currentCallback4, ref type2))
                 {
                     onDeserialized = (onDeserialized ?? new List <SerializationCallback>());
                     onDeserialized.Add(JsonContract.CreateSerializationCallback(methodInfo));
                     currentCallback4 = methodInfo;
                 }
                 if (DefaultContractResolver.IsValidCallback(methodInfo, parameters, typeof(OnErrorAttribute), currentCallback5, ref type2))
                 {
                     onError = (onError ?? new List <SerializationErrorCallback>());
                     onError.Add(JsonContract.CreateSerializationErrorCallback(methodInfo));
                     currentCallback5 = methodInfo;
                 }
             }
         }
     }
 }
All Usage Examples Of Newtonsoft.Json.Serialization.DefaultContractResolver::IsValidCallback