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); }
/// <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())); }