/// <summary>
        /// Gets the type serialization information about a type
        /// </summary>
        /// <param name="type">Type for which information is retrieved</param>
        /// <returns>TypeSerializationInfo for the type, null if it doesn't exist</returns>
        internal static MITypeSerializationInfo GetTypeSerializationInfo(Type type)
        {
            MITypeSerializationInfo temp = null;

            s_knownTableKeyType.TryGetValue(type.FullName, out temp);
            return(temp);
        }
        /// <summary>
        /// Writes an item or property in Monad namespace
        /// </summary>
        /// <param name="serializer">The serializer to which the object is serialized.</param>
        /// <param name="property">name of property. Pass null for item</param>
        /// <param name="source">object to be written</param>
        /// <param name="entry">serialization information about source</param>
        /// <param name="result"></param>
        private static void CreateCimInstanceForOnePrimitiveKnownType
        (
            InternalMISerializer serializer,
            string property,
            object source,
            MITypeSerializationInfo entry,
            out CimInstance result
        )
        {
            Dbg.Assert(serializer != null, "caller should have validated the information");
            Dbg.Assert(source != null, "caller should have validated the information");
            Dbg.Assert(entry != null, "caller should have validated the information");

            if (entry != null && entry.Serializer == null)
            {
                // we are not using GetToString, because we assume that
                // ToString() for primitive types never throws
                string value = Convert.ToString(source, CultureInfo.InvariantCulture);
                Dbg.Assert(value != null, "ToString shouldn't return null for primitive types");
                result = CreateRawStringCimInstance(property, value, entry);
            }
            else
            {
                result = entry.Serializer(property, source, entry);
            }
        }
        /// <summary>
        /// Serializes PSObject whose base objects are of primitive known type
        /// </summary>
        /// <param name="source"></param>
        /// <param name="property"></param>
        /// <param name="depth"></param>
        /// <param name="result"></param>
        /// <returns>
        /// true if source is handled, else false.
        /// </returns>
        private bool HandlePrimitiveKnownTypePSObject
        (
            object source,
            string property,
            int depth,
            out CimInstance result
        )
        {
            // To avoid compiler error
            result = CreateNullCimInstance();

            Dbg.Assert(source != null, "caller should validate the parameter");

            bool     sourceHandled = false;
            PSObject moSource      = source as PSObject;

            if (moSource != null && !moSource.immediateBaseObjectIsEmpty)
            {
                //Check if baseObject is primitive known type
                object baseObject = moSource.ImmediateBaseObject;
                MITypeSerializationInfo pktInfo = KnownMITypes.GetTypeSerializationInfo(baseObject.GetType());
                if (pktInfo != null)
                {
                    CreateCimInstanceForPrimitiveTypePSObject(moSource, baseObject, pktInfo, property, depth, out result);
                    sourceHandled = true;
                }
            }
            return(sourceHandled);
        }
        /// <summary>
        /// Serializes an PSObject whose baseobject is of primitive type.
        /// </summary>
        /// <param name="source">
        /// source from which notes are written
        /// </param>
        /// <param name="primitive">
        /// primitive object which is written as base object. In most cases it
        /// is same source.ImmediateBaseObject. When PSObject is serialized as string,
        /// </param>
        /// <param name="pktInfo">
        /// TypeSerializationInfo for the primitive.
        /// </param>
        /// <param name="property"></param>
        /// <param name="depth"></param>
        /// <param name="result"></param>
        private void CreateCimInstanceForPrimitiveTypePSObject
        (
            PSObject source,
            object primitive,
            MITypeSerializationInfo pktInfo,
            string property,
            int depth,
            out CimInstance result
        )
        {
            // To avoid compiler error
            result = CreateNullCimInstance();

            Dbg.Assert(source != null, "Caller should validate source != null");

            string toStringValue = SerializationUtilities.GetToStringForPrimitiveObject(source);
            bool   hasModifiedTypesCollection = false;
            //hasModifiedTypesCollection = PSObjectHasModifiedTypesCollection(source);

            bool hasNotes = false;
            //hasNotes = PSObjectHasNotes(source);

            bool hasModifiedToString = (toStringValue != null);

            if (hasNotes || hasModifiedTypesCollection || hasModifiedToString)
            {
                //TODO, insivara : TO BE IMPLEMENTED
                //WritePrimitiveTypePSObjectWithNotes(
                //    source,
                //    primitive,
                //    hasModifiedTypesCollection,
                //    toStringValue,
                //    pktInfo,
                //    streamName,
                //    property,
                //    depth);
                //return;
            }
            else
            {
                if (primitive != null)
                {
                    CreateCimInstanceForOnePrimitiveKnownType(this, property, primitive, pktInfo, out result);
                    return;
                }
                else
                {
                    //TODO, insivara : Return Null CimInstance
                    return;
                }
            }
        }
        /// <summary>
        /// Handles primitive known type by first converting it to a PSObject.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="property"></param>
        /// <param name="depth"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private bool HandlePrimitiveKnownTypeByConvertingToPSObject(
            object source,
            string property,
            int depth,
            out CimInstance result
            )
        {
            // To avoid compiler error
            result = CreateNullCimInstance();

            Dbg.Assert(source != null, "caller should validate the parameter");
            //Check if source is of primitive known type
            MITypeSerializationInfo pktInfo = KnownMITypes.GetTypeSerializationInfo(source.GetType());

            if (pktInfo != null)
            {
                PSObject pktInfoPSObject = PSObject.AsPSObject(source);
                return(HandlePrimitiveKnownTypePSObject(pktInfoPSObject, property, depth, out result));
            }
            return(false);
        }
        private static CimInstance CreateCimInstanceWhenPropertyNameExists(string property, object source, MITypeSerializationInfo entry)
        {
            CimInstance innerInstance = CreateCimInstance(entry.CimClassName);
            CimProperty valueProperty = CimProperty.Create("Value", source, entry.CimType, CimFlags.Property);
            innerInstance.CimInstanceProperties.Add(valueProperty);
            CimInstance c = CreateCimInstance("PS_ObjectProperty");
            CimProperty name = CimProperty.Create("Name", property,
                                                  Microsoft.Management.Infrastructure.CimType.String,
                                                  CimFlags.Property);
            c.CimInstanceProperties.Add(name);
            CimProperty outerInstanceValueProperty = CimProperty.Create("Value", innerInstance,
                                                                        Microsoft.Management.Infrastructure.CimType.Reference,
                                                                        CimFlags.Property);
            c.CimInstanceProperties.Add(outerInstanceValueProperty);

            return c;
        }
 private static CimInstance CreateRawStringCimInstance(string property, string value, MITypeSerializationInfo entry)
 {
     CimInstance c;
     if (property != null)
     {
         c = CreateCimInstanceWhenPropertyNameExists(property, value, entry);
     }
     else
     {
         c = CreateCimInstance(entry.CimClassName);
         CimProperty p1 = CimProperty.Create("Value", value, entry.CimType, CimFlags.Property);
         c.CimInstanceProperties.Add(p1);
     }
     return c;
 }
 /// <summary>
 /// Creates CimInstance for a string 
 /// </summary>
 /// <param name="property">name of property. pass null for item</param>
 /// <param name="source">string value to write</param>
 /// <param name="entry">serialization information about source</param>
 internal static CimInstance CreateCimInstanceForString(string property, object source, MITypeSerializationInfo entry)
 {
     CimInstance c;
     String value = InternalSerializer.EncodeString((String)source);
     if (property != null)
     {
         c = CreateCimInstanceWhenPropertyNameExists(property, value, entry);
     }
     else
     {
         c = CreateCimInstance(entry.CimClassName);
         CimProperty valueProperty = CimProperty.Create("Value", value, entry.CimType, CimFlags.Property);
         c.CimInstanceProperties.Add(valueProperty);
     }
     return c;
 }
 /// <summary>
 /// Creates CimInstance for a primitive type 
 /// </summary>
 /// <param name="property">name of property. pass null for item</param>
 /// <param name="source">value</param>
 /// <param name="entry">serialization information about source</param>
 internal static CimInstance CreateCimInstanceForPrimitiveType(string property, object source, MITypeSerializationInfo entry)
 {
     CimInstance c;
     if (property != null)
     {
         c = CreateCimInstanceWhenPropertyNameExists(property, source, entry);
     }
     else
     {
         c = CreateCimInstance(entry.CimClassName);
         CimProperty valueProperty = CimProperty.Create("Value", source, entry.CimType, CimFlags.Property);
         c.CimInstanceProperties.Add(valueProperty);
     }
     return c;
 }
        /// <summary>
        /// Writes an item or property in Monad namespace
        /// </summary>
        /// <param name="serializer">The serializer to which the object is serialized.</param>
        /// <param name="property">name of property. Pass null for item</param>
        /// <param name="source">object to be written</param>
        /// <param name="entry">serialization information about source</param>
        /// <param name="result"></param>
        private static void CreateCimInstanceForOnePrimitiveKnownType
        (
            InternalMISerializer serializer,
            string property,
            object source,
            MITypeSerializationInfo entry,
            out CimInstance result
        )
        {
            Dbg.Assert(serializer != null, "caller should have validated the information");
            Dbg.Assert(source != null, "caller should have validated the information");
            Dbg.Assert(entry != null, "caller should have validated the information");

            if (entry != null && entry.Serializer == null)
            {
                // we are not using GetToString, because we assume that
                // ToString() for primitive types never throws
                string value = Convert.ToString(source, CultureInfo.InvariantCulture);
                Dbg.Assert(value != null, "ToString shouldn't return null for primitive types");
                result = CreateRawStringCimInstance(property, value, entry);
            }
            else
            {
                result = entry.Serializer(property, source, entry);
            }
        }
        /// <summary>
        /// Serializes an PSObject whose baseobject is of primitive type.
        /// </summary>
        /// <param name="source">
        /// source from which notes are written
        /// </param>
        /// <param name="primitive">
        /// primitive object which is written as base object. In most cases it
        /// is same source.ImmediateBaseObject. When PSObject is serialized as string,
        /// </param>
        /// <param name="pktInfo">
        /// TypeSerializationInfo for the primitive. 
        /// </param>
        /// <param name="property"></param>
        /// <param name="depth"></param>
        /// <param name="result"></param>
        private void CreateCimInstanceForPrimitiveTypePSObject
        (
            PSObject source,
            object primitive,
            MITypeSerializationInfo pktInfo,
            string property,
            int depth,
            out CimInstance result
        )
        {
            // To avoid compiler error
            result = CreateNullCimInstance();

            Dbg.Assert(source != null, "Caller should validate source != null");

            string toStringValue = SerializationUtilities.GetToStringForPrimitiveObject(source);
            bool hasModifiedTypesCollection = false;
            //hasModifiedTypesCollection = PSObjectHasModifiedTypesCollection(source);

            bool hasNotes = false;
            //hasNotes = PSObjectHasNotes(source);

            bool hasModifiedToString = (toStringValue != null);

            if (hasNotes || hasModifiedTypesCollection || hasModifiedToString)
            {
                //TODO, insivara : TO BE IMPLEMENTED
                //WritePrimitiveTypePSObjectWithNotes(
                //    source,
                //    primitive,
                //    hasModifiedTypesCollection,
                //    toStringValue,
                //    pktInfo,
                //    streamName,
                //    property,
                //    depth);
                //return;
            }
            else
            {
                if (primitive != null)
                {
                    CreateCimInstanceForOnePrimitiveKnownType(this, property, primitive, pktInfo, out result);
                    return;
                }
                else
                {
                    //TODO, insivara : Return Null CimInstance
                    return;
                }
            }
        }