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

  • Home
  • SEARCH
  • 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 6117607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:20:25+00:00 2026-05-23T15:20:25+00:00

I have a project which stores values in SQL and later retrieves them for

  • 0

I have a project which stores values in SQL and later retrieves them for analysis. To keep track of what kinds of values might be stored, I created a class roughly like this:

private class DataField
{
    public string FieldName;
    public string FieldType;
}

When values are being read for analysis, a switch statement is used as follows (simplified):

switch (DataField.FieldType)
{
    case "int":
        List<int> InputData = new List<int>();
        // Populate list from DB
    break;
    case "bool":
        List<bool> InputData = new List<bool>();
        // Populate list from DB
    break;
}

Rather than maintain code in multiple places, I am looking for a way to get rid of the switch statement, but that means I need to dynamically create collections based on the type. Current that type is (naively?) a string, but I think I could improve this by changing the class:

private class ImprovedDataField
{
    public string FieldName;
    public Type FieldType;
}

And then dynamically create collections somehow:

Type DataType = typeof(DataField.FieldType);
List<DataType> InputData = new List<DataType>();
// Populate list from DB

This of course does not work, resulting in a Type or namespace name expected error.

Unfortunately I’m not very familiar with working with the Type class, nor generics nor anonymous types as I search for a solution (nothing seems to be appropriate).

How can I reduce duplication of code where the only difference in each switch statement branch is the type of variable collection being produced?

  • 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-05-23T15:20:25+00:00Added an answer on May 23, 2026 at 3:20 pm

    If you want to create statically-typed collection of objects of type known at runtime, you need to use reflection. See i.e. this blog entry – that’s the solution for creating List<T>:

    public static IList CreateGenericList(Type collectionType)
    {
        var listType = typeof(List<>).MakeGenericType(new[] { collectionType});
        return (IList) Activator.CreateInstance(listType);
    }
    

    So in your example:

    Type dataType = DataField.FieldType;
    IList inputData = CreateGenericList(dataType);
    // Populate list from DB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a smelly code in our project. A lot of values which used
I have a database table which stores a type for a project which stores
I have some code here which reads from a file, and stores them in
I have an C# dll project for which I have to store the runtime
I have a project which is source controlled using Subversion and VisualSVN. Since the
I have a project which contains different components that everyone works on. We have
I have a project which it's domain contain following classes: Courier Customer Food Order
I have a project which exposes object model to use by different types of
I have a project which builds correctly from the command line. On one of
I have a project which has a set of binary dependencies (assembly dlls for

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.