我有
public class ABSInfo
{
public decimal CodeCoveragePercentage { get; set; }
public TestInfo TestInformation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个Object数组说"SourceType"
public object[] SourceType { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想将Object Array(SoiurceType)转换为ABSInfo [].
我正在努力
ABSInfo[] absInfo = Array.ConvertAll(SourceType, x => (ABSInfo)x);
Run Code Online (Sandbox Code Playgroud)
但是错误
无法将"WindowsFormsApplication1.TestInfo"类型的对象强制转换为"WindowsFormsApplication1.ABSInfo".
怎么做转换?
编辑:
public class TestInfo
{
public int RunID { get; set; }
public TestRunStatus Status { get; set; }
public string Description { get; set; }
public int TotalTestCases { get; set; }
public int TotalTestCasesPassed { get; set; }
public int TotalTestCasesFailed { get; set; }
public int TotalTestCasesInconclusive { get; set; }
public string ReportTo { get; set; }
public string Modules { get; set; }
public string CodeStream { get; set; }
public int RetryCount { get; set; }
public bool IsCodeCoverageRequired { get; set; }
public FrameWorkVersion FrameworkVersion { get; set; }
public string TimeTaken { get; set; }
public int ProcessID { get; set; }
public int GroupID { get; set; }
public string GroupName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
你可以使用LINQ;
ABSInfo[] absInfo = SourceType.Cast<ABSInfo>().ToArray();
Run Code Online (Sandbox Code Playgroud)
要么
ABSInfo[] absInfo = SourceType.OfType<ABSInfo>().ToArray();
Run Code Online (Sandbox Code Playgroud)
第一个将尝试将每个源数组元素转换为,ABSInfo并且InvalidCastException当至少一个元素不可能时将返回.
第二个将只返回数组中的这些元素,可以将它们转换为ABSInfo对象.