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>
 /// Execute a CIM query and return only the first instance in the result.
 /// </summary>
 /// <param name="session">The CimSession to be queried</param>
 /// <param name="query">A string containing the query to be run</param>
 /// <returns>
 /// A <see cref="Microsoft.Management.Infrastructure.CimInstance"/> object
 /// representing the first instance in a query result if successful, null
 /// otherwise.
 /// </returns>
 internal static CimInstance QueryFirstInstance(this CimSession session, string query)
 {
     return(session.QueryFirstInstance(CIMHelper.DefaultNamespace, query));
 }