示例#1
0
        /// <summary>
        /// Encode object.
        /// </summary>
        internal static object EncodeObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            Type type = obj.GetType();

            if (obj is PSObject)
            {
                // The "is" keyword takes care of PSObject and subclasses.
                return(EncodePSObject((PSObject)obj));
            }
            else if (obj is ProgressRecord)
            {
                return(((ProgressRecord)obj).ToPSObjectForRemoting());
            }
            else if (IsKnownType(type))
            {
                return(obj);
            }
            else if (type.IsEnum)
            {
                return((int)obj);
            }
            else if (obj is CultureInfo)
            {
                // The "is" keyword takes care of CultureInfo and subclasses.
                return(obj.ToString());
            }
            else if (obj is Exception)
            {
                return(EncodeException((Exception)obj));
            }
            else if (type == typeof(object[]))
            {
                return(EncodeObjectArray((object[])obj));
            }
            else if (type.IsArray)
            {
                return(EncodeArray((Array)obj));
            }
            else if (obj is IList && IsCollection(type))
            {
                return(EncodeCollection((IList)obj));
            }
            else if (obj is IDictionary && IsDictionary(type))
            {
                return(EncodeDictionary((IDictionary)obj));
            }
            else if (type.IsSubclassOf(typeof(FieldDescription)) || type == typeof(FieldDescription))
            {
                // The upcasting removes the Attributes, so we want to do this both when it
                // is a subclass and when it is a FieldDescription object.
                return(EncodeClassOrStruct(UpcastFieldDescriptionSubclassAndDropAttributes((FieldDescription)obj)));
            }
            else if (IsEncodingAllowedForClassOrStruct(type))
            {
                return(EncodeClassOrStruct(obj));
            }
            else if (obj is RemoteHostCall)
            {
                return(((RemoteHostCall)obj).Encode());
            }
            else if (obj is RemoteHostResponse)
            {
                return(((RemoteHostResponse)obj).Encode());
            }
            else if (obj is SecureString)
            {
                return(obj);
            }
            else if (obj is PSCredential)
            {
                return(obj);
            }
            else if (IsGenericIEnumerableOfInt(type))
            {
                return(EncodeCollection((IList)obj));
            }
            else
            {
                throw RemoteHostExceptions.NewRemoteHostDataEncodingNotSupportedException(type);
            }
        }
示例#2
0
        internal static object EncodeObject(object obj)
        {
            if (obj == null)
            {
                return(null);
            }
            Type type = obj.GetType();

            if (obj is PSObject)
            {
                return(EncodePSObject((PSObject)obj));
            }
            if (obj is RemoteRunspace)
            {
                return(EncodePSObject(((RemoteRunspace)obj).ToPSObjectForRemoting()));
            }
            if (obj is ProgressRecord)
            {
                return(((ProgressRecord)obj).ToPSObjectForRemoting());
            }
            if (IsKnownType(type))
            {
                return(obj);
            }
            if (type.IsEnum)
            {
                return((int)obj);
            }
            if (obj is CultureInfo)
            {
                return(obj.ToString());
            }
            if (obj is Exception)
            {
                return(EncodeException((Exception)obj));
            }
            if (type == typeof(object[]))
            {
                return(EncodeObjectArray((object[])obj));
            }
            if (type.IsArray)
            {
                return(EncodeArray((Array)obj));
            }
            if (!(obj is IList) || !IsCollection(type))
            {
                if ((obj is IDictionary) && IsDictionary(type))
                {
                    return(EncodeDictionary((IDictionary)obj));
                }
                if (type.IsSubclassOf(typeof(FieldDescription)) || (type == typeof(FieldDescription)))
                {
                    return(EncodeClassOrStruct(UpcastFieldDescriptionSubclassAndDropAttributes((FieldDescription)obj)));
                }
                if (IsEncodingAllowedForClassOrStruct(type))
                {
                    return(EncodeClassOrStruct(obj));
                }
                if (obj is RemoteHostCall)
                {
                    return(((RemoteHostCall)obj).Encode());
                }
                if (obj is RemoteHostResponse)
                {
                    return(((RemoteHostResponse)obj).Encode());
                }
                if (obj is SecureString)
                {
                    return(obj);
                }
                if (obj is PSCredential)
                {
                    return(obj);
                }
                if (!IsGenericIEnumerableOfInt(type))
                {
                    throw RemoteHostExceptions.NewRemoteHostDataEncodingNotSupportedException(type);
                }
            }
            return(EncodeCollection((IList)obj));
        }