Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:38:49+00:00 2026-06-04T10:38:49+00:00

Ok this one is puzzling me. I have an abstract base class named Testbase

  • 0

Ok this one is puzzling me. I have an abstract base class named Testbase and it has an abstract function called RunTest. In a folder I have a collection of classes that inherit this abstract class. In a test controller I call the following code to create a list of instances of these Test Classes.

        String ns = "HCTCommon.Tests";
        var query = from t in Assembly.GetExecutingAssembly().GetTypes()
                    where t.Namespace == ns
                    select t;

        foreach (object t in query)
        {
            TestBase test = (TestBase)Activator.CreateInstance(t as Type);
            testList.Add(test);
        }

in one of the classes RunTest function I was cleaning up code and decided to use lambda expressions and tried various versions.

pullservice = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == serviceName);

and

pullservice = ServiceController.GetServices().Where(s => s.DisplayName.Equals("Pull Service")).ToList()[0];

are the ones I remember but anytime i use the lambda’s rather than running through some convoluted foreach loop i get an invalidcastexception on the line of code

TestBase test = (TestBase)Activator.CreateInstance(t as Type);

Exception:

Unable to cast object of type '<>c__DisplayClass4' to type 'HCTCommon.TestBase'.

Stack Trace:

   at HCTCommon.TestController.populateTestList()
   at HCTCommon.TestController..ctor(RegistryKey Key)
   at HealthCheck.HealthCheckForm.InitializeTestPanels() in C:\Users\bkoch.ESI911\documents\visual studio 2010\Projects\HCTCommon\HealthCheck\HealthCheckForm.cs:line 55
   at HealthCheck.HealthCheckForm..ctor() in C:\Users\bkoch.ESI911\documents\visual studio 2010\Projects\HCTCommon\HealthCheck\HealthCheckForm.cs:line 26
   at HealthCheck.Program.registrycheck() in C:\Users\bkoch.ESI911\documents\visual studio 2010\Projects\HCTCommon\HealthCheck\Program.cs:line 63
   at HealthCheck.Program.Main() in C:\Users\bkoch.ESI911\documents\visual studio 2010\Projects\HCTCommon\HealthCheck\Program.cs:line 34
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:38:51+00:00Added an answer on June 4, 2026 at 10:38 am

    The lambda expression generates an anonymous closure class to hold local variables.
    Your code is incorrectly picking up this class.

    You should add

    where typeof(TestBase).IsAssignableFrom(t)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has been puzzling my for some time now. Let's imagine a class
This one's puzzling me. I have a MySQL query, being run though PDO: $stmt
Alright, this one's been puzzling me for a bit. the following function encodes a
this one has been puzzling me for a few days now and I feel
This one has been stumping me for a while. But I'm no expert. This
This one has me scratching my head, so I'm hoping a second pair of
This one has proven to be a little tricky for me so far. I
This one has me stumped. <div id=container> <div id=store></div> <div id=contact></div> </div> I'm looking
This one is quite obviously a function pointer : typedef int (* foobar) (int
This one is tough to explain inside the title. What I have going on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.