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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:37:46+00:00 2026-05-26T03:37:46+00:00

I have a bunch of *.h files, containing only c-style definitions like #define ALPHA_REACTOR_CODE

  • 0

I have a bunch of *.h files, containing only c-style definitions like

#define ALPHA_REACTOR_CODE 99641
#define BETA_REACTOR_CODE  99642
#define GAMMA_REACTOR_CODE 99643
#define DELTA_REACTOR_CODE 99644

How can I use this files in my С# code without changes and work with this constant in my code?

  • 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-26T03:37:47+00:00Added an answer on May 26, 2026 at 3:37 am

    apologies for the late answer when I promised one earlier.

    The idea I have involves using the runtime code compiler as well as reflection to dynamically generate a class that contains all these constants.

    So the steps (or pseudocode), as I see it are:

    1. For each .h file read all the lines that begin with #define and store these.
    2. Using Microsoft.CSharp.CSharpCodeProvider() and an empty template file, compile a DLL in memory that contains a class that exposes public const int ALPHA_REACTOR_CODE = 99641; style constants.
    3. Use Assembly.GetType(string) to get the type of your generated class
    4. Use Reflection and a method that takes the constant name to access the value.

    So if that’s our plan of attack, here is some snippets of code that may help you.

    1. Loading the .h files
    This is pretty basic file reading, the key trick is to filter your lines by #define and then parse to a Dictionary<string, int>.

    lines.Where(line => line.StartsWith("#define", true, CultureInfo.CurrentCulture))
                    .Select(line => line.Split(' ').Skip(1))
                    .ToDictionary(lineParts => lineParts.First(), lineParts => int.Parse(lineParts.Last()));
    

    This will generate a dictionary of all your definitions. You may want to do a .Distict() call before the .ToDictionary() just in case there is duplicate definitions.

    2. Generating the Assembly
    This is where you use the Microsoft.CSharp.CSharpCodeProvider() to generate a in-memory assembly. The trick here is to have a “GenerateCode.cs” file that contains a basic structure for a class. This must be set as a EmbeddedResource, otherwise it will try and be compiled (which won’t work).

    using System;
    
    namespace GeneratedCode
    {{
        public class GeneratedConstants
        {{
    
    {0}
    
        }}
    }}
    

    You’ll note the {0}, we use that with a string.Format to insert the constants into the file.

    Use assembly.GetManifestResourceStream() to get a stream of this template file and a normal StreamReader to read this into one big string in memory.

    Generate the constants by using a StringBuilder and basic string.Format() formatting coupled with the Dictionary of values.

        StringBuilder builder = new StringBuilder();
        foreach (var constant in dict)
        {
            builder.AppendFormat(CultureInfo.CurrentCulture, "public const int {0} = {1};", constant.Key, constant.Value);
        }
    

    Then just use string.Format() to insert the builder string into the generated class string (using the {0})

    Then, you just need to use the Microsoft.CSharp.CSharpCodeProvider() to generate an assembly in memory.

     using (CSharpCodeProvider provider = new CSharpCodeProvider())
                {
                    parameters.ReferencedAssemblies.Add("System.dll");
                    parameters.ReferencedAssemblies.Add("System.Core.dll");
    
                    CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode);
    
                }
    

    3 & 4. Using Reflection
    now you have an assembly that contains a type with your constants defined in it, you can use assembly.GetType("GeneratedCode.GeneratedConstants"); to get the type, and use Reflection to get the constants out, either one at a time or all together.

    Have a look at this blog post which covers how to get a const using reflection: http://weblogs.asp.net/whaggard/archive/2003/02/20/2708.aspx

    Well I hope that helps you out. It’s not an overly complex solution, but has a few moving parts.

    Good luck!

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

Sidebar

Related Questions

I have a bunch of files containing the exact same log message. One of
So, I have an existing hdfs directory, containing a bunch of files. These files
I have a bunch of files named like so: output_1.png output_2.png ... output_10.png ...
I have a bunch of files in a single directory that I would like
I have a bunch of files on my server that look like below. I
I have a bunch of files that I need to be able to transport
I have a bunch of files with coordinates in UTM form. For each coordinate
I have a bunch of files that were named in a somewhat standard format.
Say you have a bunch of files. Say you can store meta data to
I'd sooner not just permanently have a bunch of files checked out to me,

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.