예제 #1
0
        /// <summary>
        /// Decode object.
        /// </summary>
        internal static object DecodeObject(object obj, Type type)
        {
            if (obj == null)
            {
                return(null);
            }

            Dbg.Assert(type != null, "Expected type != null");
            if (type == typeof(PSObject))
            {
                return(DecodePSObject(obj));
            }
            else if (type == typeof(ProgressRecord))
            {
                return(ProgressRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
            }
            else if (IsKnownType(type))
            {
                return(obj);
            }
            else if (obj is SecureString)
            {
                return(obj);
            }
            else if (obj is PSCredential)
            {
                return(obj);
            }
            else if (obj is PSObject && type == typeof(PSCredential))
            {
                // BUGBUG: The following piece of code is a workaround
                // because custom serialization is busted. If rehydration
                // works correctly then PSCredential should be available
                PSObject     objAsPSObject = (PSObject)obj;
                PSCredential cred          = null;
                try
                {
                    cred = new PSCredential((string)objAsPSObject.Properties["UserName"].Value,
                                            (SecureString)objAsPSObject.Properties["Password"].Value);
                }
                catch (GetValueException)
                {
                    cred = null;
                }

                return(cred);
            }
            else if (obj is int && type.IsEnum)
            {
                return(Enum.ToObject(type, (int)obj));
            }
            else if (obj is string && type == typeof(CultureInfo))
            {
                return(new CultureInfo((string)obj));
            }
            else if (obj is PSObject && type == typeof(Exception))
            {
                return(DecodeException((PSObject)obj));
            }
            else if (obj is PSObject && type == typeof(object[]))
            {
                return(DecodeObjectArray((PSObject)obj));
            }
            else if (obj is PSObject && type.IsArray)
            {
                return(DecodeArray((PSObject)obj, type));
            }
            else if (obj is PSObject && IsCollection(type))
            {
                return(DecodeCollection((PSObject)obj, type));
            }
            else if (obj is PSObject && IsDictionary(type))
            {
                return(DecodeDictionary((PSObject)obj, type));
            }
            else if (obj is PSObject && IsEncodingAllowedForClassOrStruct(type))
            {
                return(DecodeClassOrStruct((PSObject)obj, type));
            }
            else if (obj is PSObject && IsGenericIEnumerableOfInt(type))
            {
                // we cannot create an instance of interface type like IEnumerable
                // Since a Collection implements IEnumerable, falling back to use
                // that.
                return(DecodeCollection((PSObject)obj, typeof(Collection <int>)));
            }
            else if (obj is PSObject && type == typeof(RemoteHostCall))
            {
                return(RemoteHostCall.Decode((PSObject)obj));
            }
            else if (obj is PSObject && type == typeof(RemoteHostResponse))
            {
                return(RemoteHostResponse.Decode((PSObject)obj));
            }
            else
            {
                throw RemoteHostExceptions.NewRemoteHostDataDecodingNotSupportedException(type);
            }
        }
예제 #2
0
 internal static object DecodeObject(object obj, Type type)
 {
     if (obj == null)
     {
         return(obj);
     }
     if (type == typeof(PSObject))
     {
         return(DecodePSObject(obj));
     }
     if (type == typeof(System.Management.Automation.Runspaces.Runspace))
     {
         return(RemoteRunspace.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
     }
     if (type == typeof(ProgressRecord))
     {
         return(ProgressRecord.FromPSObjectForRemoting(PSObject.AsPSObject(obj)));
     }
     if (IsKnownType(type))
     {
         return(obj);
     }
     if (obj is SecureString)
     {
         return(obj);
     }
     if (obj is PSCredential)
     {
         return(obj);
     }
     if ((obj is PSObject) && (type == typeof(PSCredential)))
     {
         PSObject obj2 = (PSObject)obj;
         try
         {
             return(new PSCredential((string)obj2.Properties["UserName"].Value, (SecureString)obj2.Properties["Password"].Value));
         }
         catch (GetValueException)
         {
             return(null);
         }
     }
     if ((obj is int) && type.IsEnum)
     {
         return(Enum.ToObject(type, (int)obj));
     }
     if ((obj is string) && (type == typeof(CultureInfo)))
     {
         return(new CultureInfo((string)obj));
     }
     if ((obj is PSObject) && (type == typeof(Exception)))
     {
         return(DecodeException((PSObject)obj));
     }
     if ((obj is PSObject) && (type == typeof(object[])))
     {
         return(DecodeObjectArray((PSObject)obj));
     }
     if ((obj is PSObject) && type.IsArray)
     {
         return(DecodeArray((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsCollection(type))
     {
         return(DecodeCollection((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsDictionary(type))
     {
         return(DecodeDictionary((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsEncodingAllowedForClassOrStruct(type))
     {
         return(DecodeClassOrStruct((PSObject)obj, type));
     }
     if ((obj is PSObject) && IsGenericIEnumerableOfInt(type))
     {
         return(DecodeCollection((PSObject)obj, typeof(Collection <int>)));
     }
     if ((obj is PSObject) && (type == typeof(RemoteHostCall)))
     {
         return(RemoteHostCall.Decode((PSObject)obj));
     }
     if (!(obj is PSObject) || (type != typeof(RemoteHostResponse)))
     {
         throw RemoteHostExceptions.NewRemoteHostDataDecodingNotSupportedException(type);
     }
     return(RemoteHostResponse.Decode((PSObject)obj));
 }