简体   繁体   中英

C#: Iterate through members of a list of objects of different types

I have a list I'm filling with objects of different types, which inherit from a base class QueryExecutor. However I get "QueryExecutor does not contain a definition for queryprompt and not accessible extension method queryprompt accepting a first argument of type QueryExecutor could be found" when I try to read queryprompt, because queryprompt is defined in the subclasses and not the base class (QueryExecutor).

public static List<Object> queries = new List<Object>();

#if DEBUG
    QueryExecutor.queries.Add(new TestString(url, program, key));
#endif
    QueryExecutor.queries.Add(new UATBlank(url, program, key));
    QueryExecutor.queries.Add(new AcceptanceCriteriaBlank(url, program, key));
    QueryExecutor.queries.Add(new BDDFormatInvalid(url, program, key));
    QueryExecutor.queries.Add(new TargetElevationDateStale(url, program, key));
    QueryExecutor.queries.Add(new TargetElevationDateMissing(url, program, key));
    QueryExecutor.queries.Add(new MissedSLA(url, program, key));
    QueryExecutor.queries.Add(new CapabilityIDBlank(url, program, key));

    foreach (QueryExecutor qe in queries)
    {
        Console.WriteLine(qe.queryprompt);
        if (Console.ReadLine().ToUpper() == "Y")
        {
            Console.WriteLine(qe.queryprogress);
            var workItems = await qe.QueryOpenBugs("eDellPrograms", rt).ConfigureAwait(false);
            workItems = await qe.filterResults(workItems);
            await qe.PrintOpenBugsAsync(workItems, key, qe.querycomment, nameof(qe));
        }
    }

C# - List of different types of objects This thread suggests casting, but how would I do that in a loop? Can I get an object's subclass from it somehow? Also, casting qe gives me "Cannot assign to qe because it is a foreach iteration variable".

I did this by creating an interface:

    public interface QueryInterface
    {
        string queryprompt
        {
            get;
            set;
        }
        string querycomment
        {
            get;
            set;
        }
        string queryprogress
        {
            get;
            set;
        }

public class QueryExecutor : QueryInterface
{
        public Task<List<WorkItem>> QueryOpenBugs(string project, string rt);
        public Task PrintOpenBugsAsync(List<WorkItem> workItems, string key, string comment, string queryname);
        public Task<List<WorkItem>> filterResults(List<WorkItem> workItems);
    }

public static List<Object> queries = new List<Object>();

#if DEBUG
    QueryExecutor.queries.Add(new TestString(url, program, key));
#endif
    QueryExecutor.queries.Add(new UATBlank(url, program, key));
    QueryExecutor.queries.Add(new AcceptanceCriteriaBlank(url, program, key));
    QueryExecutor.queries.Add(new BDDFormatInvalid(url, program, key));
    QueryExecutor.queries.Add(new TargetElevationDateStale(url, program, key));
    QueryExecutor.queries.Add(new TargetElevationDateMissing(url, program, key));
    QueryExecutor.queries.Add(new MissedSLA(url, program, key));
    QueryExecutor.queries.Add(new CapabilityIDBlank(url, program, key));

    foreach (QueryExecutor qe in queries)
    {
        Console.WriteLine(qe.queryprompt);
        if (Console.ReadLine().ToUpper() == "Y")
        {
            Console.WriteLine(qe.queryprogress);
            var workItems = await qe.QueryOpenBugs("eDellPrograms", rt).ConfigureAwait(false);
            workItems = await qe.filterResults(workItems);
            await qe.PrintOpenBugsAsync(workItems, key, qe.querycomment, nameof(qe));
        }
    }

        public virtual string queryprompt
        {
            get;
            set;
        }
        public virtual string querycomment
        {
            get;
            set;
        }
        public virtual string queryprogress
        {
            get;
            set;
        }
    }
}

    public class TestString : QueryExecutor, QueryInterface
    {

        public TestString(string url, string orgName, string personalAccessToken) : base(url, orgName, personalAccessToken) { }
        public override string queryprompt { get { return "Enter 'y' to search for test workitem"; } }
        public override string querycomment { get { return "This is a test comment, please ignore."; } }
        public override string queryprogress { get { return "Querying test workitem..."; } }
        public override string queryfunc(string project, string rt)
        {
              ...
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM