示例#1
0
 /// <summary>
 /// The validation check on whether a type is serializable
 /// </summary>
 /// <param name="type">The type to test</param>
 /// <returns>Returns whether that type can be serialized</returns>
 public static bool TypeIsSerializable(Type type)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     if (!type.IsSerializable)
     {
         return(false);
     }
     if (!type.IsGenericType)
     {
         return(true);
     }
     Type[] genericArguments = type.GetGenericArguments();
     for (int i = 0; i < genericArguments.Length; i++)
     {
         Type type2 = genericArguments[i];
         if (!SerializationTypeConverter.TypeIsSerializable(type2))
         {
             return(false);
         }
     }
     return(true);
 }
示例#2
0
        private bool CanConvert(object sourceValue, Type destinationType, out byte[] serializationData, out Exception error)
        {
            serializationData = null;
            error             = null;
            if (destinationType == null)
            {
                error = new ArgumentNullException("destinationType");
                return(false);
            }
            if (sourceValue == null)
            {
                error = new ArgumentNullException("sourceValue");
                return(false);
            }
            PSObject pSObject = sourceValue as PSObject;

            if (pSObject == null)
            {
                error = new NotSupportedException(string.Format("Unsupported Source Type: {0}", sourceValue.GetType().FullName));
                return(false);
            }
            if (!SerializationTypeConverter.CanSerialize(destinationType))
            {
                error = new NotSupportedException(string.Format("Unsupported Type Conversion: {0}", destinationType.FullName));
                return(false);
            }
            if (typeof(Exception).IsAssignableFrom(destinationType) && pSObject.TypeNames != null && pSObject.TypeNames.Count > 0 && pSObject.TypeNames[0].StartsWith("Deserialized.System.Management.Automation"))
            {
                foreach (string current in pSObject.TypeNames)
                {
                    if (current.Equals("Deserialized.System.Management.Automation.ParameterBindingException", StringComparison.OrdinalIgnoreCase))
                    {
                        return(false);
                    }
                }
            }
            if (pSObject.Properties["SerializationData"] == null)
            {
                error = new NotSupportedException("Serialization Data is Absent");
                return(false);
            }
            object value = pSObject.Properties["SerializationData"].Value;

            if (!(value is byte[]))
            {
                error = new NotSupportedException("Unsupported Data Format");
                return(false);
            }
            serializationData = (value as byte[]);
            return(true);
        }
示例#3
0
 /// <summary>
 /// Whether a type can be serialized
 /// </summary>
 /// <param name="type">The type to test</param>
 /// <returns>Whether the specified type can be serialized</returns>
 public static bool CanSerialize(Type type)
 {
     return(SerializationTypeConverter.TypeIsSerializable(type) && !type.IsEnum || (type.Equals(typeof(Exception)) || type.IsSubclassOf(typeof(Exception))));
 }
示例#4
0
 /// <summary>
 /// Whether an object can be serialized
 /// </summary>
 /// <param name="obj">The object to test</param>
 /// <returns>Whether the object can be serialized</returns>
 public static bool CanSerialize(object obj)
 {
     return(obj != null && SerializationTypeConverter.CanSerialize(obj.GetType()));
 }