开发者

C# Getting functions, enums and values in enums from a file

i'm trying to get values from a set of files which contains enums and functions but i got some problems getting them. Since the users should be able to add their own files there to read from it sh开发者_StackOverflowouldn't be defined from other sources an example of how the files look

/**  
info info info  
*/  
enum MyEnum  
{  
    Myvalue = 1,  
    Mysecondvalue,  
}

/**  
more info  
*/  
public MyFunction();

I need to get MyEnum's name and the values without " = 1," and "," etc i also need the info and MyFunction without ";" any ideas how to do it?

edit: This is not for compiled files, its from files that can be treated as txt


Considering these aren't compiled and are script files this question will point you in the right direction.

My solution to simplify the task is to use one of the many language parsers out there. The one I prefer with .NET is Gold Parser - download it, grab the C# language file, open the grammar file in the tool and just click the bottom right arrow button 4 times until you get a prompt to save a CGT file.

After that, follow these instructions for using the CGT file to interpret the C#s script file using the Calitha engine. The example uses search query syntax, but you can easily adapt it:

private void _lalrParser_OnTokenRead(LALRParser parser, TokenReadEventArgs e)
{
     // See if the token's name is enum, next time round grab the names/values
}


I would take a look at http://www.antlr.org/ which is the parser used by nhibernate.


Alright, needs some cleaning and some fixing but i got it working. Not sure why i didn't thought about it before but heres what i do. I read the files 1 line at a time check if its enum, public etc and then do the stuff i want. enum is abit tricky and its not quite working yet, a few bugs. in enum i check if its an enum, if it is then until a line stats with "}" i add the values i find. But thanks for your answers anyways, if i didn't get it to work! ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜