Esempio n. 1
0
        /// <summary>
        /// Retrieve a new object of type T, whose properties and fields are
        /// populated from an instance of the named WMI class. If the CIM
        /// query results in multiple instances, only the first instance is
        /// returned.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to be created. Must be a default-constructable
        /// reference type.
        /// </typeparam>
        /// <param name="session">
        /// The CIM session to be queried.
        /// </param>
        /// <param name="nameSpace">
        /// A string containing the namespace to run the query against
        /// </param>
        /// <param name="wmiClassName">
        /// A string containing the name of the WMI class from which to populate
        /// the resultant object.
        /// </param>
        /// <returns>
        /// A new object of type T if successful, null otherwise.
        /// </returns>
        /// <remarks>
        /// This method matches property and field names of type T with identically
        /// named properties in the WMI class instance. The WMI property is converted
        /// to the type of T's property or field.
        /// </remarks>
        internal static T GetFirst <T>(CimSession session, string nameSpace, string wmiClassName) where T : class, new()
        {
            if (string.IsNullOrEmpty(wmiClassName))
            {
                throw new ArgumentException("String argument may not be null or empty", "wmiClassName");
            }

            try
            {
                var type    = typeof(T);
                var binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                T   rv      = new T();

                using (var instance = session.QueryFirstInstance(nameSpace, CIMHelper.WqlQueryAll(wmiClassName)))
                {
                    SetObjectDataMembers(rv, binding, instance);
                }

                return(rv);
            }
            catch (Exception /*ex*/)
            {
                // on any error fall through to the null return below
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieve an array of new objects of type T, whose properties and fields are
        /// populated from an instance of the specified WMI class on the specified CIM
        /// session.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the object to be created. Must be a default-constructable
        /// reference type.
        /// </typeparam>
        /// <param name="session">
        /// The CIM session to be queried.
        /// </param>
        /// <param name="nameSpace">
        /// A string containing the namespace to run the query against
        /// </param>
        /// <param name="wmiClassName">
        /// A string containing the name of the WMI class from which to populate
        /// the resultant array elements.
        /// </param>
        /// <returns>
        /// An array of new objects of type T if successful, null otherwise.
        /// </returns>
        /// <remarks>
        /// This method matches property and field names of type T with identically
        /// named properties in the WMI class instance. The WMI property is converted
        /// to the type of T's property or field.
        /// </remarks>
        internal static T[] GetAll <T>(CimSession session, string nameSpace, string wmiClassName) where T : class, new()
        {
            if (string.IsNullOrEmpty(wmiClassName))
            {
                throw new ArgumentException("String argument may not be null or empty", "wmiClassName");
            }

            var rv = new List <T>();

            try
            {
                var instances = session.QueryInstances(nameSpace, CIMHelper.WqlQueryAll(wmiClassName));

                if (instances != null)
                {
                    var type    = typeof(T);
                    var binding = BindingFlags.Public | BindingFlags.Instance;

                    foreach (var instance in instances)
                    {
                        T objT = new T();

                        using (instance)
                        {
                            SetObjectDataMembers(objT, binding, instance);
                        }

                        rv.Add(objT);
                    }
                }
            }
            catch (Exception /*ex*/)
            {
                // on any error we'll just fall through to the return below
            }

            return(rv.ToArray());
        }