Exemple #1
0
        /// <summary>
        /// Executes WMI instance method with  parameters
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="classObj"></param>
        /// <param name="parameters">function paramaters</param>
        public static void ExecuteMethod(object classObj, params object[] parameters)
        {
            var inParams = new List <object>();

            foreach (var param in parameters)
            {
                inParams.Add(param);
            }


            var           propsNames = HelperFuncs.getSearchPropsNames(classObj.GetType());
            var           propValues = HelperFuncs.getSearchPropValues(classObj);
            StringBuilder builder    = new StringBuilder();

            var propNamesAndValues = propsNames.Zip(propValues, (pn, pv) => new { propName = pn, propValue = pv }).ToList();


            for (int i = 0; i < propNamesAndValues.Count; i++)
            {
                builder.Append(propNamesAndValues[i].propName + " = '" + propNamesAndValues[i].propValue + "'");
                if (i != propNamesAndValues.Count - 1)
                {
                    builder.Append(" AND ");
                }
            }

            string quertyWhere = "WHERE " + builder.ToString();

            quertyWhere = quertyWhere.Replace(@"\", @"\\");


            var    methodName    = String.IsNullOrEmpty(_methodName) ? new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name : _methodName;
            string searchParams  = "*";
            string rootNamespace = HelperFuncs.getNamespace(classObj.GetType());
            string className     = HelperFuncs.getClassName(classObj.GetType());

            string searchQuery = String.Format("SELECT {0} FROM {1} {2}", searchParams, className, quertyWhere);

            using (var searcher = new ManagementObjectSearcher(rootNamespace, searchQuery))
            {
                using (var searcherData = searcher.Get())
                {
                    foreach (ManagementObject manageObject in searcherData)
                    {
                        manageObject.InvokeMethod(methodName, inParams.ToArray());
                    }
                }
            }
        }
Exemple #2
0
        public static string toString(object obj)
        {
            var sb = new StringBuilder();

            var propsNames = HelperFuncs.getSearchPropsNames(obj.GetType());
            var propValues = HelperFuncs.getSearchPropValues(obj);

            var propNamesAndValues = propsNames.Zip(propValues, (pName, pValue) => new { propName = pName, propValue = pValue });

            foreach (var item in propNamesAndValues)
            {
                sb.AppendLine(item.propName + ": " + item.propValue);
            }

            return(sb.ToString());
        }