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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:31:10+00:00 2026-05-26T04:31:10+00:00

So I have an imported file. Basically, I want to use the headers of

  • 0

So I have an imported file. Basically, I want to use the headers of this file to know which columns should be placed into which variable value on my class. I’d like to do this comparison via variable attributes in C#, but I’m not really sure how to approach this and set this up.

For example, say one variable in my class is public string Name; and in the imported file, one of the column headers is Name. I’d rather not use reflection to match the variable directly. How can I set an attribute on my class variable, and then use it to match with these local string header variables, and fill the correct one?

  • 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-26T04:31:11+00:00Added an answer on May 26, 2026 at 4:31 am

    Here is an example program that should give you what you need. The SetOption method is what provides the reflection logic to find the field with the specified option name and set its value.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
       // This is the attribute that we will apply to the fields
       // for which we want to specify an option name.
       [AttributeUsage(AttributeTargets.Field)]
       public class OptionNameAttribute : Attribute
       {
          public OptionNameAttribute(string optionName)
          {
             OptionName = optionName;
          }
    
          public string OptionName { get; private set; }
       }
    
       // This is the class which will contain the option values that 
       // we read from the file.
       public class OptionContainer
       {
          [OptionName("Name")]
          public string MyNameField;
    
          [OptionName("Value")]
          public string MyValueField;
       }
    
       class Program
       {
          // SetOption is the method that assigns the value provided to the 
          // field of the specified instance with an OptionName attribute containing
          // the specified optionName.
          static void SetOption(object instance, string optionName, string optionValue)
          {
             // Get all the fields that has the OptionNameAttribute defined
             IEnumerable<FieldInfo> optionFields = instance.GetType()
                .GetFields()
                .Where(field => field.IsDefined(typeof(OptionNameAttribute), true));
    
             // Find the single field where the OptionNameAttribute.OptionName property
             // matches the provided optionName argument.
             FieldInfo optionField = optionFields.SingleOrDefault(field =>
                field.GetCustomAttributes(typeof(OptionNameAttribute), true)
                .Cast<OptionNameAttribute>().Single().OptionName.Equals(optionName));
    
             // If the found field is null there is no such option.
             if (optionField == null)
                throw new ArgumentException(String.Format("Unknown option {0}", optionName), "optionname");
    
             // Finally set the value.
             optionField.SetValue(instance, optionValue);
          }
    
          static void Main(string[] args)
          {
             OptionContainer instance = new OptionContainer();
             SetOption(instance, "Name", "This is the value of Name");
             SetOption(instance, "Value", "This is my value");
    
             Console.WriteLine(instance.MyNameField);
             Console.WriteLine(instance.MyValueField);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this XML file that I have imported into Cocoa and want to
I have my CSV file imported as such: records = FasterCSV.read(path, :headers => true,
I want to have a number of files imported in a general python file
I have imported a certificate into a private ~/.keystore file: keytool -list Enter keystore
Im my Python file, I have imported the win32gui module like this: import win32gui
I want to make android home page background. I have imported a video file
I have imported a file x.ani into Resource file Resources.resx. Now trying to load
I have imported my .h file into a 2nd one, but in the 2nd
I am attempting to us a png file that I have imported into the
I have a PDF file that I have imported in as a resource into

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.