Exemplo n.º 1
0
 private static T SafelyGetBaseObject <T>(PSObject psObject)
 {
     if (((psObject == null) || (psObject.BaseObject == null)) || !(psObject.BaseObject is T))
     {
         throw RemoteHostExceptions.NewDecodingFailedException();
     }
     return((T)psObject.BaseObject);
 }
Exemplo n.º 2
0
 private static T SafelyCastObject <T>(object obj)
 {
     if (!(obj is T))
     {
         throw RemoteHostExceptions.NewDecodingFailedException();
     }
     return((T)obj);
 }
Exemplo n.º 3
0
        private static T SafelyGetPropertyValue <T>(PSObject psObject, string key)
        {
            PSPropertyInfo info = psObject.Properties[key];

            if (((info == null) || (info.Value == null)) || !(info.Value is T))
            {
                throw RemoteHostExceptions.NewDecodingFailedException();
            }
            return((T)info.Value);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Safely get property value.
        /// </summary>
        private static T SafelyGetPropertyValue <T>(PSObject psObject, string key)
        {
            PSPropertyInfo propertyInfo = psObject.Properties[key];

            if (propertyInfo == null || propertyInfo.Value == null || propertyInfo.Value is not T)
            {
                throw RemoteHostExceptions.NewDecodingFailedException();
            }

            return((T)propertyInfo.Value);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Decode exception.
        /// </summary>
        private static Exception DecodeException(PSObject psObject)
        {
            object result = RemoteHostEncoder.DecodePropertyValue(psObject, RemoteDataNameStrings.MethodException, typeof(Exception));

            if (result == null)
            {
                return(null);
            }
            if (result is Exception)
            {
                return((Exception)result);
            }
            throw RemoteHostExceptions.NewDecodingFailedException();
        }
Exemplo n.º 6
0
        private static Exception DecodeException(PSObject psObject)
        {
            object obj2 = RemoteHostEncoder.DecodePropertyValue(psObject, "me", typeof(Exception));

            if (obj2 == null)
            {
                return(null);
            }
            if (!(obj2 is Exception))
            {
                throw RemoteHostExceptions.NewDecodingFailedException();
            }
            return((Exception)obj2);
        }
Exemplo n.º 7
0
        private static object DecodeClassOrStruct(PSObject psObject, Type type)
        {
            object uninitializedObject = FormatterServices.GetUninitializedObject(type);

            foreach (PSPropertyInfo property in psObject.Properties)
            {
                FieldInfo field = type.GetField(property.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (property.Value == null)
                {
                    throw RemoteHostExceptions.NewDecodingFailedException();
                }
                field.SetValue(uninitializedObject, RemoteHostEncoder.DecodeObject(property.Value, field.FieldType) ?? throw RemoteHostExceptions.NewDecodingFailedException());
            }
            return(uninitializedObject);
        }
        /// <summary>
        /// Decode class or struct.
        /// </summary>
        private static object DecodeClassOrStruct(PSObject psObject, Type type)
        {
            object obj = ClrFacade.GetUninitializedObject(type);

            // Field values cannot be null - because for null fields we simply don't transport them.
            foreach (PSPropertyInfo propertyInfo in psObject.Properties)
            {
                FieldInfo fieldInfo = type.GetField(propertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (propertyInfo.Value == null)
                {
                    throw RemoteHostExceptions.NewDecodingFailedException();
                }
                object fieldValue = DecodeObject(propertyInfo.Value, fieldInfo.FieldType);
                if (fieldValue == null)
                {
                    throw RemoteHostExceptions.NewDecodingFailedException();
                }

                fieldInfo.SetValue(obj, fieldValue);
            }

            return(obj);
        }