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 8980371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:04:23+00:00 2026-06-15T20:04:23+00:00

i need to get a list to store all fields – values in a

  • 0

i need to get a list to store all fields – values in a class

the class is just a few of public const string variables as i pasted below.

public class HTDB_Cols
{
    public class TblCustomers
    {
        public const string CustID  = "custID",
               Name  = "name",
         CustType  = "custType",
         AddDate  = "addDate",
         Address  = "address",
         City  = "city",
         Phone  = "phone",
         Cell  = "cell";
    }
}

this is a method that returns a list of strings that enables me to have a list of strings representing all my tables columns names , though somthing is not working with this code as i get an error

” Non-static field requires a target“.

public class GetClassFields
{

        public static List<string> AsList(string TableName)
        {


                    return typeof(HTDB_Cols).GetNestedTypes()
                    .First(t => String.Compare(t.Name, TableName, true) == 0)
                    .GetFields()
                    .Select(f => f.GetValue(null) as string)
                    .ToList();

        }
}

trying to use it as follows :

foreach (string tblCol in RobCS_212a.Utils.Reflct.GetClassFields.AsList      (DBSchema.HTDB_Tables.TblCustomers))
{
    Response.Write(string.Concat(tblCol, "<br />"));
}

Field ‘tbName’ defined on type ‘DBSchema.HTDB_Cols+TblTimeCPAReport’ is not a field on the target object which is of type ‘DBSchema.HTDB_Cols’.

  • 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-15T20:04:24+00:00Added an answer on June 15, 2026 at 8:04 pm

    Your code was close. There were two issues, both located in the arguments to your linq select method call:

    • Your class HTDB_Cols is a non-static class and the string values you are trying to retrieve are instance members. Thus when you are trying to pull instance members out of a class, you have to pass an instance of the class to the FieldInof.GetValue method. In my code below I create an instance of your class in the variable “instanceOfClass”. You can see this in the documentation for the FieldInfo class

    • The value returned from FieldInfo.GetValue is an object. You have to explicitly cast it to a string using the ToString method or the (string) cast.

    With these two changes your method works. A listing is as follows:

    public class GetClassFields
    {
        public static List<string> AsList(string tbl)
        {
            var instanceOfClass = new HTDB_Cols();
            return typeof(HTDB_Cols).GetNestedTypes()
                                    .First(t => String.Compare(t.Name, tbl, true) == 0)
                                    .GetFields()
                                    .Select(f => f.GetValue(instanceOfClass).ToString())
                                    .ToList<String>();
        }
    }
    

    You can call this function as follows:

    var fields = GetClassFields.AsList("TblCustomers");
    

    which returns the desired information:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<List<String>> I need to get a List of all possible concatenation
I need a get a list of all databases in a Couchdb server inside
I need to get a list of all tables that are published for replication
i have a problem with sql query. I need to get list of users.
I need to get a list of tags in a text, make their contents
i need to get the list of domain names on my network... but i
I need to get the list of dates between two dates - for e.g.
I have an app where I need to get the list of countries,languages etc
I am developing an application in which I need to get the list of
I have a little doubt. I need to get a list of the files

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.