예제 #1
0
 internal static object ConvertFromCimToDotNet(object cimObject, Type expectedDotNetType)
 {
     if (expectedDotNetType != null)
     {
         if (cimObject != null)
         {
             if (expectedDotNetType.IsGenericType && expectedDotNetType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
             {
                 expectedDotNetType = expectedDotNetType.GetGenericArguments()[0];
             }
             if (!LanguagePrimitives.IsCimIntrinsicScalarType(expectedDotNetType))
             {
                 if (!expectedDotNetType.Equals(typeof(CimInstance)))
                 {
                     if (expectedDotNetType.IsArray)
                     {
                         Type elementType = CimValueConverter.GetElementType(expectedDotNetType);
                         if (elementType != null)
                         {
                             Array arrays  = (Array)LanguagePrimitives.ConvertTo(cimObject, typeof(Array), CultureInfo.InvariantCulture);
                             Array arrays1 = Array.CreateInstance(elementType, arrays.Length);
                             for (int i = 0; i < arrays1.Length; i++)
                             {
                                 object dotNet = CimValueConverter.ConvertFromCimToDotNet(arrays.GetValue(i), elementType);
                                 arrays1.SetValue(dotNet, i);
                             }
                             return(arrays1);
                         }
                     }
                     Type convertibleCimType = CimValueConverter.GetConvertibleCimType(expectedDotNetType);
                     if (convertibleCimType == null)
                     {
                         Func <Func <object>, object> func = (Func <object> innerAction) => {
                             object obj;
                             try
                             {
                                 obj = innerAction();
                             }
                             catch (Exception exception1)
                             {
                                 Exception exception = exception1;
                                 CommandProcessorBase.CheckForSevereException(exception);
                                 throw CimValueConverter.GetInvalidCastException(exception, "InvalidCimToDotNetCast", cimObject, expectedDotNetType.FullName);
                             }
                             return(obj);
                         }
                         ;
                         if (!typeof(ObjectSecurity).IsAssignableFrom(expectedDotNetType))
                         {
                             if (!typeof(X509Certificate2).Equals(expectedDotNetType))
                             {
                                 if (!typeof(X500DistinguishedName).Equals(expectedDotNetType))
                                 {
                                     if (!typeof(PhysicalAddress).Equals(expectedDotNetType))
                                     {
                                         if (!typeof(IPEndPoint).Equals(expectedDotNetType))
                                         {
                                             if (!typeof(XmlDocument).Equals(expectedDotNetType))
                                             {
                                                 throw CimValueConverter.GetInvalidCastException(null, "InvalidCimToDotNetCast", cimObject, expectedDotNetType.FullName);
                                             }
                                             else
                                             {
                                                 return(func(() => {
                                                     int?nullable = null;
                                                     XmlDocument xmlDocument = InternalDeserializer.LoadUnsafeXmlDocument((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture), true, nullable);
                                                     return xmlDocument;
                                                 }
                                                             ));
                                             }
                                         }
                                         else
                                         {
                                             return(func(() => {
                                                 int num = ((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture)).LastIndexOf(':');
                                                 int num1 = int.Parse(((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture)).Substring(num + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
                                                 IPAddress pAddress = IPAddress.Parse(((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture)).Substring(0, num));
                                                 return new IPEndPoint(pAddress, num1);
                                             }
                                                         ));
                                         }
                                     }
                                     else
                                     {
                                         return(func(() => PhysicalAddress.Parse((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture))));
                                     }
                                 }
                                 else
                                 {
                                     return(func(() => new X500DistinguishedName((byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture))));
                                 }
                             }
                             else
                             {
                                 return(func(() => new X509Certificate2((byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture))));
                             }
                         }
                         else
                         {
                             return(func(() => {
                                 ObjectSecurity objectSecurity = (ObjectSecurity)Activator.CreateInstance(expectedDotNetType);
                                 objectSecurity.SetSecurityDescriptorSddlForm((string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture));
                                 return objectSecurity;
                             }
                                         ));
                         }
                     }
                     else
                     {
                         object obj1 = LanguagePrimitives.ConvertTo(cimObject, convertibleCimType, CultureInfo.InvariantCulture);
                         object obj2 = LanguagePrimitives.ConvertTo(obj1, expectedDotNetType, CultureInfo.InvariantCulture);
                         return(obj2);
                     }
                 }
                 else
                 {
                     return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
                 }
             }
             else
             {
                 return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
             }
         }
         else
         {
             return(null);
         }
     }
     else
     {
         throw new ArgumentNullException("expectedDotNetType");
     }
 }
예제 #2
0
        /// <exception cref="PSInvalidCastException">The only kind of exception this method can throw.</exception>
        internal static object ConvertFromCimToDotNet(object cimObject, Type expectedDotNetType)
        {
            if (expectedDotNetType == null)
            {
                throw new ArgumentNullException("expectedDotNetType");
            }

            if (cimObject == null)
            {
                return(null);
            }

            if (expectedDotNetType.GetTypeInfo().IsGenericType&& expectedDotNetType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                expectedDotNetType = expectedDotNetType.GetGenericArguments()[0];
            }

            if (LanguagePrimitives.IsCimIntrinsicScalarType(expectedDotNetType))
            {
                return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
            }

            if (expectedDotNetType == typeof(CimInstance))
            {
                return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
            }

            if (expectedDotNetType.IsArray)
            {
                Type dotNetElementType = GetElementType(expectedDotNetType);
                if (dotNetElementType != null)
                {
                    var   cimArray    = (Array)LanguagePrimitives.ConvertTo(cimObject, typeof(Array), CultureInfo.InvariantCulture);
                    Array dotNetArray = Array.CreateInstance(dotNetElementType, cimArray.Length);
                    for (int i = 0; i < dotNetArray.Length; i++)
                    {
                        object dotNetElement = ConvertFromCimToDotNet(cimArray.GetValue(i), dotNetElementType);
                        dotNetArray.SetValue(dotNetElement, i);
                    }

                    return(dotNetArray);
                }
            }

            Type convertibleCimType = GetConvertibleCimType(expectedDotNetType);

            if (convertibleCimType != null)
            {
                object cimIntrinsicValue = LanguagePrimitives.ConvertTo(cimObject, convertibleCimType, CultureInfo.InvariantCulture);
                object dotNetObject      = LanguagePrimitives.ConvertTo(cimIntrinsicValue, expectedDotNetType, CultureInfo.InvariantCulture);
                return(dotNetObject);
            }

            Func <Func <object>, object> exceptionSafeReturn = delegate(Func <object> innerAction)
            {
                try
                {
                    return(innerAction());
                }
                catch (Exception e)
                {
                    throw CimValueConverter.GetInvalidCastException(
                              e,
                              "InvalidCimToDotNetCast",
                              cimObject,
                              expectedDotNetType.FullName);
                }
            };

            if (typeof(ObjectSecurity).IsAssignableFrom(expectedDotNetType))
            {
                var sddl = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    var objectSecurity = (ObjectSecurity)Activator.CreateInstance(expectedDotNetType);
                    objectSecurity.SetSecurityDescriptorSddlForm(sddl);
                    return objectSecurity;
                }));
            }

            if (typeof(X509Certificate2) == expectedDotNetType)
            {
                var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return new X509Certificate2(cimIntrinsicValue);
                }));
            }

            if (typeof(X500DistinguishedName) == expectedDotNetType)
            {
                var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return new X500DistinguishedName(cimIntrinsicValue);
                }));
            }

            if (typeof(PhysicalAddress) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return PhysicalAddress.Parse(cimIntrinsicValue);
                }));
            }

            if (typeof(IPEndPoint) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    int indexOfLastColon = cimIntrinsicValue.LastIndexOf(':');
                    int port = int.Parse(cimIntrinsicValue.Substring(indexOfLastColon + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
                    IPAddress address = IPAddress.Parse(cimIntrinsicValue.Substring(0, indexOfLastColon));
                    return new IPEndPoint(address, port);
                }));
            }

            // WildcardPattern is only supported as an "in" parameter - we do not support the reverse translation (i.e. from "a%" to "a*")

            if (typeof(XmlDocument) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument(
                        cimIntrinsicValue,
                        true,                                /* preserve non elements: whitespace, processing instructions, comments, etc. */
                        null);                               /* default maxCharactersInDocument */
                    return doc;
                }));
            }

            // unrecognized type = throw invalid cast exception
            throw CimValueConverter.GetInvalidCastException(
                      null, /* inner exception */
                      "InvalidCimToDotNetCast",
                      cimObject,
                      expectedDotNetType.FullName);
        }
예제 #3
0
 internal static object ConvertFromDotNetToCim(object dotNetObject)
 {
     if (dotNetObject != null)
     {
         PSObject pSObject = PSObject.AsPSObject(dotNetObject);
         Type     type     = pSObject.BaseObject.GetType();
         if (!LanguagePrimitives.IsCimIntrinsicScalarType(type))
         {
             if (!typeof(CimInstance).IsAssignableFrom(type))
             {
                 if (!typeof(PSReference).IsAssignableFrom(type))
                 {
                     if (type.IsArray)
                     {
                         Type elementType = CimValueConverter.GetElementType(type);
                         if (elementType != null)
                         {
                             Array baseObject = (Array)pSObject.BaseObject;
                             Type  cimType    = CimValueConverter.GetCimType(elementType);
                             Array arrays     = Array.CreateInstance(cimType, baseObject.Length);
                             for (int i = 0; i < arrays.Length; i++)
                             {
                                 object cim = CimValueConverter.ConvertFromDotNetToCim(baseObject.GetValue(i));
                                 arrays.SetValue(cim, i);
                             }
                             return(arrays);
                         }
                     }
                     Type convertibleCimType = CimValueConverter.GetConvertibleCimType(type);
                     if (convertibleCimType == null)
                     {
                         if (!typeof(ObjectSecurity).IsAssignableFrom(type))
                         {
                             if (!typeof(X509Certificate2).IsAssignableFrom(type))
                             {
                                 if (!typeof(X500DistinguishedName).IsAssignableFrom(type))
                                 {
                                     if (!typeof(PhysicalAddress).IsAssignableFrom(type))
                                     {
                                         if (!typeof(IPEndPoint).IsAssignableFrom(type))
                                         {
                                             if (!typeof(WildcardPattern).IsAssignableFrom(type))
                                             {
                                                 if (!typeof(XmlDocument).IsAssignableFrom(type))
                                                 {
                                                     throw CimValueConverter.GetInvalidCastException(null, "InvalidDotNetToCimCast", dotNetObject, CmdletizationResources.CimConversion_CimIntrinsicValue);
                                                 }
                                                 else
                                                 {
                                                     XmlDocument xmlDocument = (XmlDocument)pSObject.BaseObject;
                                                     string      outerXml    = xmlDocument.OuterXml;
                                                     return(outerXml);
                                                 }
                                             }
                                             else
                                             {
                                                 WildcardPattern wildcardPattern = (WildcardPattern)pSObject.BaseObject;
                                                 return(wildcardPattern.ToWql());
                                             }
                                         }
                                         else
                                         {
                                             object obj = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                                             return(obj);
                                         }
                                     }
                                     else
                                     {
                                         object obj1 = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                                         return(obj1);
                                     }
                                 }
                                 else
                                 {
                                     X500DistinguishedName x500DistinguishedName = (X500DistinguishedName)pSObject.BaseObject;
                                     byte[] rawData = x500DistinguishedName.RawData;
                                     return(rawData);
                                 }
                             }
                             else
                             {
                                 X509Certificate2 x509Certificate2 = (X509Certificate2)pSObject.BaseObject;
                                 byte[]           numArray         = x509Certificate2.RawData;
                                 return(numArray);
                             }
                         }
                         else
                         {
                             string sddl = SecurityDescriptorCommandsBase.GetSddl(pSObject);
                             return(sddl);
                         }
                     }
                     else
                     {
                         object obj2 = LanguagePrimitives.ConvertTo(dotNetObject, convertibleCimType, CultureInfo.InvariantCulture);
                         return(obj2);
                     }
                 }
                 else
                 {
                     PSReference pSReference = (PSReference)pSObject.BaseObject;
                     if (pSReference.Value != null)
                     {
                         PSObject pSObject1 = PSObject.AsPSObject(pSReference.Value);
                         return(CimValueConverter.ConvertFromDotNetToCim(pSObject1.BaseObject));
                     }
                     else
                     {
                         return(null);
                     }
                 }
             }
             else
             {
                 return(pSObject.BaseObject);
             }
         }
         else
         {
             return(pSObject.BaseObject);
         }
     }
     else
     {
         return(null);
     }
 }
예제 #4
0
        /// <exception cref="PSInvalidCastException">The only kind of exception this method can throw.</exception>
        internal static object ConvertFromDotNetToCim(object dotNetObject)
        {
            if (dotNetObject == null)
            {
                return(null);
            }

            PSObject psObject   = PSObject.AsPSObject(dotNetObject);
            Type     dotNetType = psObject.BaseObject.GetType();

            Dbg.Assert(
                !(dotNetType.GetTypeInfo().IsGenericType&& dotNetType.GetGenericTypeDefinition() == typeof(Nullable <>)),
                "GetType on a boxed object should never return Nullable<T>");

            if (LanguagePrimitives.IsCimIntrinsicScalarType(dotNetType))
            {
                return(psObject.BaseObject);
            }

            if (typeof(CimInstance).IsAssignableFrom(dotNetType))
            {
                return(psObject.BaseObject);
            }

            if (typeof(PSReference).IsAssignableFrom(dotNetType))
            {
                PSReference psReference = (PSReference)psObject.BaseObject;
                if (psReference.Value == null)
                {
                    return(null);
                }
                else
                {
                    PSObject innerPso = PSObject.AsPSObject(psReference.Value);
                    return(ConvertFromDotNetToCim(innerPso.BaseObject));
                }
            }

            if (dotNetType.IsArray)
            {
                Type dotNetElementType = GetElementType(dotNetType);
                if (dotNetElementType != null)
                {
                    var   dotNetArray    = (Array)psObject.BaseObject;
                    Type  cimElementType = CimValueConverter.GetCimType(dotNetElementType);
                    Array cimArray       = Array.CreateInstance(cimElementType, dotNetArray.Length);
                    for (int i = 0; i < cimArray.Length; i++)
                    {
                        object cimElement = ConvertFromDotNetToCim(dotNetArray.GetValue(i));
                        cimArray.SetValue(cimElement, i);
                    }

                    return(cimArray);
                }
            }

            Type convertibleCimType = GetConvertibleCimType(dotNetType);

            if (convertibleCimType != null)
            {
                object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, convertibleCimType, CultureInfo.InvariantCulture);
                return(cimIntrinsicValue);
            }

            if (typeof(ObjectSecurity).IsAssignableFrom(dotNetType))
            {
                string cimIntrinsicValue = Microsoft.PowerShell.Commands.SecurityDescriptorCommandsBase.GetSddl(psObject);
                return(cimIntrinsicValue);
            }

            if (typeof(X509Certificate2).IsAssignableFrom(dotNetType))
            {
                var    cert = (X509Certificate2)(psObject.BaseObject);
                byte[] cimIntrinsicValue = cert.RawData;
                return(cimIntrinsicValue);
            }

            if (typeof(X500DistinguishedName).IsAssignableFrom(dotNetType))
            {
                var    x500name          = (X500DistinguishedName)(psObject.BaseObject);
                byte[] cimIntrinsicValue = x500name.RawData;
                return(cimIntrinsicValue);
            }

            if (typeof(PhysicalAddress).IsAssignableFrom(dotNetType))
            {
                object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                return(cimIntrinsicValue);
            }

            if (typeof(IPEndPoint).IsAssignableFrom(dotNetType))
            {
                object cimIntrinsicValue = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                return(cimIntrinsicValue);
            }

            if (typeof(WildcardPattern).IsAssignableFrom(dotNetType))
            {
                var wildcardPattern = (WildcardPattern)(psObject.BaseObject);
                return(wildcardPattern.ToWql());
            }

            if (typeof(XmlDocument).IsAssignableFrom(dotNetType))
            {
                var    xmlDocument       = (XmlDocument)(psObject.BaseObject);
                string cimIntrinsicValue = xmlDocument.OuterXml;
                return(cimIntrinsicValue);
            }

            // unrecognized type = throw invalid cast exception
            throw CimValueConverter.GetInvalidCastException(
                      null, /* inner exception */
                      "InvalidDotNetToCimCast",
                      dotNetObject,
                      CmdletizationResources.CimConversion_CimIntrinsicValue);
        }