Beispiel #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;
        }
Beispiel #2
0
        internal clsMyType TryGetMyType(string fullname, Type objType)
        {
            clsMyType myType = null;

            if (sTypeCache.TryGetValue(fullname, out myType))
            {
                return(myType);
            }
            else
            {
                myType = new clsMyType(objType);

                lock (sTypeCacheLock)
                {
                    sTypeCache.Add(myType.FullName, myType);
                }

                return(myType);
            }
        }
Beispiel #3
0
        internal clsMyType TryGetMyType(string fullname, Type objType)
        {
            clsMyType myType = null;

            if (sTypeCache.TryGetValue(fullname, out myType))
            {
                return myType;
            }
            else
            {
                myType = new clsMyType(objType);

                lock (sTypeCacheLock)
                {
                    sTypeCache.Add(myType.FullName, myType);
                }

                return myType;
            }
        }
Beispiel #4
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);
        }