title: How do I retrieve a CimInstance property of a certain type?
body: <p>So, I have been searching for a while now, and I can't seem to find the answer to what seems to be (at least, to me) a simple, foundational C# question.</p>
  
  <p>To set it up, I am putting together a C# GUI program that leverages CIM classes from remote machines to retrieve data about said machine, which will be used to determine it's current "Status" from an End-User Support Technician's point of view.</p>
  
  <p>I have had no problems with setting up the CIM session to the remote machine, and retrieving the data I requested by querying for instances.  The problem I have run into is that I can't seem to figure out how to retrieve a value from a property from one of these instances with the value returned being of a certain expected type.  Without actually running the query before the code is compiled and executed, the IDE (Visual Studio 2017) assumes the return value to be of the super-type "Object" (Please forgive any vernacular problems).</p>
  
  <p>Here is the Code I am using for reference:</p>
  
  <pre><code>public class DispRecord
  {
      //declare properties for record object
      public string Hostname;
      public string Status;
      public string User;
      public string NTLogin;
      public string LockTime;
      public string LockDuration;
      public string LogonTime;
      public string LogonDuration;
      public string LastRestart;
      public string PwrOnDuration;
  }
  </code></pre>
  
  <p>I have a constructed custom class object that contains the properties I am retrieving and calculating.</p>
  
  <p>This custom object is then passed to a function that is performing the query and assigning the value to the properties.  My method class has several methods, but here is the one that currently is the sticking point:</p>
  
  <pre><code>//method for gathering CIM data
  public static DispRecord QueryCIMData (DispRecord Record)
  {
      //use cimsession to remote host
      using (CimSession Session = CimSession.Create(Record.Hostname, new DComSessionOptions()))
      {
          //declare queries
          string PwrQuery = "Select LastBootUpTime from CIM_OperatingSystem";
          //string ProcQuery = "Select CreationDate,Caption from CIM_Process where Name='explorer.exe' or Name='logonui.exe'";
  
          //declare namespace
          string Namespace = @"root\cimv2";
  
          //perform PwrQuery and retrieve lastbootuptime
          IEnumerable&lt;CimInstance&gt; Results = Session.QueryInstances(Namespace, "WQL", PwrQuery);
          DateTime LastBootUpTime = DateTime.Parse(Results.First().CimInstanceProperties["LastBootUpTime"].Value.ToString());
  
          //add PwrOnDuration and LastRestart to Record
          Record.LastRestart = LastBootUpTime.ToString();
          Record.PwrOnDuration = (DateTime.Now - LastBootUpTime).ToString(@"dd\/hh\:mm\:ss");
      }
  
      //return changed record object
      return Record;
  }
  </code></pre>
  
  <p>What you see above does work, but I feel like the code gyrations necessary to achieve the proper output are a bit ridiculous and I feel that there must be another, possibly easier or cleaner, way to achieve my desired output.  Surely there is a better way to retrieve the DateTime object I am expecting than to retrieve a property value as a string constructed from a DateTime object, that is then parsed into a new Datetime object, especially considering that I am turning around and converting it back into a string to insert into the record.</p>
  
  <p>Ideally, I would like to do something like this, but I'm not sure how to achieve it:</p>
  
  <pre><code>DateTime LastBootUpTime = Results.First().CimInstanceProperties["LastBootUpTime"].Value;
  </code></pre>
  
  <p>When I attempt the above, the Compiler will throw an exception stating that it cannot implicitly convert a value of type 'Object' to type 'DateTime'</p>
  
  <p>Essentially, since the query isn't performed until runtime, the returned CIM instance properties are all just expected as Objects, instead of their expected output based on the class that the instance is built from (Which in this case, a 'DateTime' object is expected as the value of the "LastBootUpTime" property).  </p>
  
  <p>The Code doesn't know what is coming out of the egg before it hatches, even if MSDN does.</p>
  
  <p>Can anyone help me with this seeming easy problem?</p>