• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Read Struct and enum definitions in C#

Instead of manually writing these this info in the source file, I need to read it from a file.

Does anyone know what the syntax is?
0
Barbless
Asked:
Barbless
  • 3
  • 2
  • 2
5 Solutions
 
Tommie Nathaniel Carter, Jr., MBAFounderCommented:
The file is a class file? Or are you reading from a compiled assembly? In the either case I think what we want is to add the file/assembly to an existing project as either a reference (assembly) or an imported item to the project.
0
 
BarblessAuthor Commented:
Not sure. Here's the sample source code:

namespace Engine
{
//-----------------------------------------------------
    public enum a
    {
       ...
    }
    public enum b
    {
       ...
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct a
    {
       ...
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct b
    {
       ...
    }
    ...
//-----------------------------------------------------
}

I want cut out the portion between the lines, save it to a file as is, and in its place the syntax to read the file back.
0
 
dstanley9Commented:
You can't define enums and structs from a file becuase they need to be defined at compile-time.  
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Tommie Nathaniel Carter, Jr., MBAFounderCommented:
If you can see source code then you can compile the files and afterwards include them as references in your project.

There are classes etc. in this solution right? If so then build the files in a separate project  that you want to use and add them as refs in your current project.
0
 
dstanley9Commented:
Just to clarify, I assume you're talking about reading in the enum values and struct defs at run-time.  If so, you can't do that because they are defined at compile-time.  You could do it at compile-time by creating some sort of code-generation template using Codesmith or something, but any changes to the source files wouldn't be available until you rebuilt the project.
0
 
BarblessAuthor Commented:
Yes I'm doing it at compile time.  What got this whole thing started is that I'll have a conversion utility that takes a C header file and converts all its structs and enums into a file that will replace the portion in between the dash lines, and of course the file will have to be read in.

Given all the difficulties, I guess the easiest way is to add hard-coded lines to the conversion util to write all the necessary using statements at the top, add namespace, etc, so that the converted file is a complete C# source file.

Thanks for your help.
0
 
dstanley9Commented:
Yeah, I  think some sort of code gen is going to be your best bet.  There is no #include directive to add content from an outside file.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now