Beispiel #1
0
            public object Validate(string propertyValue)
            {
                object    result;
                Exception innerException;

                if (!ValueConvertor.TryConvertValueFromString(propertyValue, base.Type, null, out result, out innerException))
                {
                    throw new ArgumentException("Invalid property", base.Name, innerException);
                }
                return(result);
            }
Beispiel #2
0
        private object DeserializeObject(object sourceValue, Type destinationType)
        {
            Exception ex = null;

            byte[] array;
            string text;

            if (!this.CanConvert(sourceValue, destinationType, out array, out text, out ex))
            {
                throw ex;
            }
            ExTraceGlobals.SerializationTracer.TraceFunction <object, string>((long)this.GetHashCode(), "SerializationTypeConverter.DeserializeObject(); SerializationData.Length = {0}; ToStringValue = '{1}'", (array == null) ? "<null>" : array.Length, text);
            object obj = null;

            try
            {
                using (MemoryStream memoryStream = new MemoryStream(array))
                {
                    AppDomain.CurrentDomain.AssemblyResolve += SerializationTypeConverter.AssemblyHandler;
                    try
                    {
                        int tickCount = Environment.TickCount;
                        if (SerializationTypeConverter.IsRunningInRPSServerSide.Value)
                        {
                            if (SerializationTypeBinder.Instance == null)
                            {
                                throw new InvalidCastException("SerializationTypeBinder initialization failed.");
                            }
                            obj = TypedBinaryFormatter.DeserializeObject(memoryStream, SerializationTypeBinder.Instance);
                        }
                        else
                        {
                            BinaryFormatter binaryFormatter = ExchangeBinaryFormatterFactory.CreateBinaryFormatter(null, new string[]
                            {
                                "System.Management.Automation.PSObject",
                                "System.DelegateSerializationHolder"
                            });
                            obj = binaryFormatter.Deserialize(memoryStream);
                        }
                        ExTraceGlobals.SerializationTracer.TraceDebug <string, int>((long)this.GetHashCode(), "DeserializeObject of type {0} succeeded in {1} ms.", (obj != null) ? obj.GetType().Name : "null", Environment.TickCount - tickCount);
                        IDeserializationCallback deserializationCallback = obj as IDeserializationCallback;
                        if (deserializationCallback != null)
                        {
                            deserializationCallback.OnDeserialization(sourceValue);
                        }
                    }
                    finally
                    {
                        AppDomain.CurrentDomain.AssemblyResolve -= SerializationTypeConverter.AssemblyHandler;
                    }
                }
            }
            catch (SerializationException ex2)
            {
                ExTraceGlobals.SerializationTracer.TraceDebug <SerializationException>((long)this.GetHashCode(), "Deserialization Failed. Error = {0}", ex2);
                if (ValueConvertor.TryConvertValueFromString(text, destinationType, null, out obj, out ex))
                {
                    ExTraceGlobals.SerializationTracer.TraceDebug <SerializationException>((long)this.GetHashCode(), "String Conversion Succeeded.", ex2);
                }
                else
                {
                    ex = new InvalidCastException(string.Format("Deserialization fails due to one SerializationException: {0}", ex2.ToString()), ex2);
                }
            }
            catch (DataSourceTransientException ex3)
            {
                ex = new InvalidCastException(string.Format("Deserialization fails due to an DataSourceTransientException: {0}", ex3.ToString()), ex3);
            }
            if (ex is InvalidCastException)
            {
                throw ex;
            }
            return(obj);
        }