Пример #1
0
        private bool CanConvert(object sourceValue, Type destinationType, out byte[] serializationData, out string stringValue, out Exception error)
        {
            serializationData = null;
            stringValue       = null;
            error             = null;
            ExTraceGlobals.SerializationTracer.TraceFunction((long)this.GetHashCode(), "SerializationTypeConverter.CanConvert({0}, {1})", new object[]
            {
                sourceValue ?? "<null>",
                destinationType ?? "<null>"
            });
            if (null == destinationType)
            {
                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(DataStrings.ExceptionUnsupportedSourceType(sourceValue, sourceValue.GetType()));
                return(false);
            }
            if (!SerializationTypeConverter.CanSerialize(destinationType))
            {
                error = new NotSupportedException(DataStrings.ExceptionUnsupportedTypeConversion(destinationType.FullName));
                return(false);
            }
            if (psobject.Properties["SerializationData"] == null)
            {
                error = new NotSupportedException(DataStrings.ExceptionSerializationDataAbsent);
                return(false);
            }
            object value = psobject.Properties["SerializationData"].Value;

            if (!(value is byte[]))
            {
                error = new NotSupportedException(DataStrings.ExceptionUnsupportedDataFormat(value));
                return(false);
            }
            stringValue       = psobject.ToString();
            serializationData = (value as byte[]);
            return(true);
        }
Пример #2
0
 public static bool CanSerialize(object obj)
 {
     return(obj != null && SerializationTypeConverter.CanSerialize(obj.GetType()));
 }