3

I have

public class ABSInfo 
    {
        public decimal CodeCoveragePercentage { get; set; }
        public TestInfo TestInformation { get; set; }      

    }

And I have an Object array say "SourceType"

public object[] SourceType { get; set; }

I want to convert the Object Array (SoiurceType) to ABSInfo[].

I am trying as

ABSInfo[] absInfo = Array.ConvertAll(SourceType, x => (ABSInfo)x);

but error

Unable to cast object of type 'WindowsFormsApplication1.TestInfo' to type 'WindowsFormsApplication1.ABSInfo'.

how to do the conversion?

Edited:

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; }
    }
3
  • 2
    It seems there is a TestInfo among your ABSInfo array? Commented Apr 9, 2013 at 12:37
  • It is another class..see update Commented Apr 9, 2013 at 12:37
  • There has got to be a better, more type safe way to do this then the big ugly hammer of object[] Commented Apr 9, 2013 at 12:37

4 Answers 4

6

You could use LINQ;

ABSInfo[] absInfo = SourceType.Cast<ABSInfo>().ToArray();

Or

ABSInfo[] absInfo = SourceType.OfType<ABSInfo>().ToArray();

The first one will try casting every source array element to ABSInfo and will return InvalidCastException when it's not possible for at least one element.

The second one will put in return array only these elements, that can be cast into ABSInfo object.

Sign up to request clarification or add additional context in comments.

Comments

0

Correct your ABSInfo class by making it inherit from TestInfo:

public class ABSInfo : TestInfo
{
    public decimal CodeCoveragePercentage { get; set; }
}

This will solve conversion problems and lets you access the TestInfo properties directly on an ABSInfo class instance.

Comments

0

You have an array of objects. You can't cast all object to ABSInfo, unless all objects are instances of ABSInfo (or more derived class).

So either put null in your array

ABSInfo[] absInfo = Array.ConvertAll(SourceType, x => x as ABSInfo);

or do not add something else than ABSInfo to SourceType.

Comments

0

It seems like your array contains objects of type TestInfo.
So you need to build an object ABSInfo for each TestInfo in your array.

In LINQ :

var absInfo = SourceType.Select(s => new ABSInfo()
                            { 
                              TestInformation = (TestInfo)s
                            , CodeCoveragePercentage = whatever
                            }).ToArray()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.