Hi there,
I'm working on a parser/scaffolding proggy to insert a .cs File and automatically read out all the scopes (new namespace, new class, new methos, attributes, fields, constructor etc.) and for noe I'm reading the input line by line and check wether the line starts with a "{". If it does, that means there's a new scope starting and whatever follows defines what is in the scope.
BUT:
the TYPE of scope (wether it's a class, or a method or a namespace etc) is written in the line above the "{". At least if we go by auto formatted c# code in VS 2012.
so what i need it so actually read line by line, and if i hit a "{", read the line above that and add it to my list of lines for that scope.
this is what i got so for for that particular part
Code:
while ((line = input.ReadLine()) != null)
{
line = line.Trim();
if (line == "{")
{
Scope s = new Scope();
//add the line above the opened scope as line to the scope
ScopeBlock.Add(File.ReadLines(path).Take(lineNumber-1).First());
while (line != "}")
{
ScopeBlock.Add(line);
}
}
foreach (string zeile in ScopeBlock)
{
ReadScope(line);
}
lineNumber++;
}
it throws an exception cause of course it can't find anything at the line specified.
anyone got an idea? Anything is greatly appreciated
1. you don't show the code where you allocate the new stream reader.
2. You don't show the code where you handle the exception.
3. If the file in #1 doesn't exist....
Please poste a complete code listing if you hope to get help here. Additionally
please post your input file.
I just wanted to focus on the part where I'm trying to access the line above the "{" but alright, here´s the two main methods for reading and writing
Code:
public void Read(string path)
{
string line = "";
int lineNumber = 0;
this.ScopeBlock = new List<string>();
//.cs File einlesen
StreamReader input = new StreamReader(path);
//read each line
while ((line = input.ReadLine()) != null)
{
line = line.Trim();
if (line == "{")
{
Scope s = new Scope();
//add the line above the opened scope as line to the scope
ScopeBlock.Add(File.ReadLines(path).Take(lineNumber-1).First());
while (line != "}")
{
ScopeBlock.Add(line);
}
}
foreach (string zeile in ScopeBlock)
{
ReadScope(line);
}
lineNumber++;
}
input.Close();
// writeEntityFile(n, c);
}
private void ReadScope(string line)
{
Class c = new Class();
Namespace n = new Namespace();
c.Attributes = new List<Attribute>();
c.Interfaces = new List<Interface>();
c.Usings = new List<Using>();
int attributeCounter = c.Attributes.Count;
string[] words = line.Split(' ');
if (words.Length >= 2 && words[1] != "{" && words[1] != "}")
{
switch (words[0])
{
case "using":
{
Scope s = new Scope();
s.Type = "using";
Using u = new Using();
u.Name = words[0] + " ";
u.Value = words[1];
c.Usings.Add(u);
} break;
case "namespace":
{
Scope s = new Scope();
s.Type = "namespace";
n.Name = words[words.Length - 1];
} break;
};
switch (words[1])
{
case "partial":
{
Scope s = new Scope();
s.Type = "Class";
c.Modifier = new Modifier();
c.Modifier.Value = words[0] + " " + words[1] + " class ";
c.Name = words[words.Length - 1];
} break;
};
if (words[1] == "System.DateTime" || words[1] == "bool")
{
Attribute att = new Attribute();
att.Modifier = new Modifier();
att.Modifier.Value = words[0] + " ";
att.Type = words[1] + " ";
att.Value = words[2];
c.Attributes.Add(att);
if (words[2] == "isDeleted")
{
Interface i = new Interface();
i.Name = "ISoftDeletable";
c.Interfaces.Add(i);
}
else if (words[2] == "CreateDate")
{
Interface i = new Interface();
i.Name = "IHasCreateDate";
c.Interfaces.Add(i);
}
else if (words[2] == "LastChange")
{
Interface i = new Interface();
i.Name = "IHasLastChange";
c.Interfaces.Add(i);
}
}
else if (words[1] == "string" || words[1] == "int" || words[1] == "System.Guid" || words[1] == "Nullable<System.DateTime>")
{
Attribute att = new Attribute();
att.Modifier = new Modifier();
att.Modifier.Value = words[0] + " ";
att.Type = words[1] + " ";
att.Value = words[2];
c.Attributes.Add(att);
if (words[2] == "NiceName")
{
Interface i = new Interface();
i.Name = "IHasNiceName";
c.Interfaces.Add(i);
}
if (words[2] == "Id")
{
Interface i = new Interface();
i.Name = "IEntityPOCO";
c.Interfaces.Add(i);
}
}
}
here's a basic example input file
I want to "take out" all information and write it into my classes to automatically create mapper files, .js models etc. depending on which fields, attributes, methods etc. are in the input file
You should handle the exeption in code, and display the Message in order to understand it.
Exception handling is different than error handling. An exception contains descriptions
of its self, and an exception handler provides facilities to recover from the error.
It is easy to run the debugger on your code, and display the exeption and the message,
without much effort or any additional code.
Here have a look at this use of the stream reader with exception handling
from Microsoft.
No reply now? D:
I was wondering if i can start 2 parallel readers, and one of them stores the line above an occuring "{" but I don't know how to accomplish that.
Any ideas?
Not sure what your end goal is (to parse files or to extract class information). If the latter, I would take a different approach and load up the file as an assembly and then use reflection to extract the information.
that would be someting completely new for me to get into. I thought i could make it easier.
my end goal is, to read the customer.cs file and auto generate mappers, .js model files etc. depending on different occurences of attributes, methods, fields and so on.
when the field "CreateDate" of type Datetime is set, i want to auto generate the interfae IHasCreateDate in the mapper file.
when the field "IsDeleted" of type bool is set i want to auto generate the Interface ISoftDeletable in the mapper File.
when the user is asked if additional data is supposed to be added, i want the mapper file to contain certain lines, creating a new dictionary..
and so on and so forth.
many dependencies.
I'll read up a bit on what you posted Arjay, thanks a bunch
Hey, so i read up on Reflection and it´s looking pretty good so far
Unfortunately there is one method I can't find or are too fuzzy by now to identify as the one I need
I need to find out the Accessor / modifier of my type I'm passing.
If we stay in my example, I'm passing the Class Customer of type Customer. When i read the class information with reflection I need to find the value of "public partial class" of Customer. Any hint on where i can find that?
same for the properties. what i can get it System.int (some int value) {get;set;} but i also need if it`s private or public and print that out at well.
There must be something I'm missing regarding property / class Access modifiert.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.