ジョブ プリンターと TimeSubmitted を含むいくつかのプロパティを受け取る C# クラスがあります。これは次の形式で DateTime として保存されます: 20130608204517.699000-300
このプロパティを持つオブジェクトを定義しました:
public class PrinterJob
{
public String Document { get; set; }
public string JobName { get; set; }
public UInt32 JobId { get; set; }
public string JobStatus { get; set; }
//===========================================================================
// This property doesn't cast adecuate
//===========================================================================
public Datetime TimeSubmitted { get; set; }
}
DateTime を (実行時に) キャストしようとすると、次のようになります。
foreach (ManagementObject prntJob in prntJobCollection)
{
System.String jobName = prntJob.Properties["Name"].Value.ToString();
//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnterName = jobName.Split(splitArr)[0];
if (String.Compare(prnterName, printerName, true) == 0)
{
PrinterJob prtobj = new PrinterJob();
prtobj.Document = prntJob.Properties["Document"].Value.ToString();
prtobj.JobName = prntJob.Properties["Name"].Value.ToString();
var id = prntJob.Properties["JobId"].Value;
prtobj.JobId = (UInt32)prntJob.Properties["JobId"].Value;
prtobj.JobStatus = prntJob.Properties["JobStatus"].Value.ToString();
//============================================================================
// Here The cast doesn't work. It comes as: 20130608204517.699000-300
// It throws an InvalidCastException
//============================================================================
prtobj.TimeSubmitted = (DateTime)prntJob.Properties["TimeSubmitted"].Value;
jobList.Add(prtobj);
}
}
この値をキャストして DateTime に変換する正しい方法は何ですか?