コード例 #1
0
        internal static Type GetCimType(Type dotNetType)
        {
            if (dotNetType == typeof(SecureString))
            {
                return(typeof(string));
            }

            if (dotNetType == typeof(PSCredential))
            {
                return(typeof(string));
            }

            return(CimValueConverter.GetCimType(dotNetType));
        }
コード例 #2
0
 internal static Type GetCimType(Type dotNetType)
 {
     if (!dotNetType.IsArray)
     {
         if (!dotNetType.IsGenericType || !dotNetType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
         {
             if (!LanguagePrimitives.IsCimIntrinsicScalarType(dotNetType))
             {
                 if (!dotNetType.Equals(typeof(CimInstance)))
                 {
                     if (!dotNetType.Equals(typeof(PSReference)))
                     {
                         Type convertibleCimType = CimValueConverter.GetConvertibleCimType(dotNetType);
                         if (convertibleCimType == null)
                         {
                             if (!typeof(ObjectSecurity).IsAssignableFrom(dotNetType))
                             {
                                 if (!typeof(X509Certificate2).Equals(dotNetType))
                                 {
                                     if (!typeof(X500DistinguishedName).Equals(dotNetType))
                                     {
                                         if (!typeof(PhysicalAddress).Equals(dotNetType))
                                         {
                                             if (!typeof(IPEndPoint).Equals(dotNetType))
                                             {
                                                 if (!typeof(WildcardPattern).Equals(dotNetType))
                                                 {
                                                     if (!typeof(XmlDocument).Equals(dotNetType))
                                                     {
                                                         return(null);
                                                     }
                                                     else
                                                     {
                                                         return(typeof(string));
                                                     }
                                                 }
                                                 else
                                                 {
                                                     return(typeof(string));
                                                 }
                                             }
                                             else
                                             {
                                                 return(typeof(string));
                                             }
                                         }
                                         else
                                         {
                                             return(typeof(string));
                                         }
                                     }
                                     else
                                     {
                                         return(typeof(byte[]));
                                     }
                                 }
                                 else
                                 {
                                     return(typeof(byte[]));
                                 }
                             }
                             else
                             {
                                 return(typeof(string));
                             }
                         }
                         else
                         {
                             return(convertibleCimType);
                         }
                     }
                     else
                     {
                         return(dotNetType);
                     }
                 }
                 else
                 {
                     return(dotNetType);
                 }
             }
             else
             {
                 return(dotNetType);
             }
         }
         else
         {
             return(CimValueConverter.GetCimType(dotNetType.GetGenericArguments()[0]));
         }
     }
     else
     {
         return(CimValueConverter.GetCimType(CimValueConverter.GetElementType(dotNetType)).MakeArrayType());
     }
 }
コード例 #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);
        }