Example #1
0
        /// <summary>
        /// Convert a list of ManagementBaseObjects into our types.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="myType"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        internal static bool Convert(List <ManagementBaseObject> data, clsMyType myType, System.Collections.IList result)
        {
            bool flagPerfectConversion = true;

            if (data == null)
            {
                throw new ArgumentNullException(string.Format("Argument cant be null. Arg='{0}', Type='{1}'", nameof(data), typeof(ManagementObjectCollection).Name));
            }

            if (data.Count == 0)
            {
                return(true);
            }

            foreach (var item in data)
            {
                // Create the instance.
                var instance = myType.CreateObject();

                foreach (var p in item.Properties)
                {
                    //if (!p.IsLocal) // are there any info in this propname??
                    //    continue; // no, then continue.

                    clsMyProperty myprop = null;

                    // try to match wmi prop against our props.
                    if (myType.WmiProperties.TryGetValue(p.Name, out myprop))
                    {
                        if (myprop.DetailInfo == MyTypeInfoEnum.Invalid) // ignore types we dont know how to handle.
                        {
                            flagPerfectConversion = false;               // set not perfect conversion flag.
                            continue;
                        }

                        object oset = null;

                        if (myprop.IsArray)
                        {
                            if (myprop.CimType == p.Type)
                            {
                                oset = p.Value;
                            }
                            else
                            {
                                oset = CreateArray(p.Value, myprop);
                            }
                        }
                        else if (myprop.IsList)
                        {
                            oset = CreateGenericList(p.Value, myprop);
                        }
                        else
                        {
                            oset = ConvertObject(p.Value, myprop);
                        }
                        //oset = ConvertObject(p.Value, myprop.DetailInfo, myprop.IsNullable);

                        //if (myprop.DetailInfo != MyTypeInfoEnum.Invalid)
                        if (myprop.IsNullable || oset != null)
                        {
                            instance = myprop.GenericSetter(instance, oset);
                        }
                    }
                }

                result.Add(instance);
            }

            return(flagPerfectConversion);
        }