What I want to do may seem like a weird scenario. Please keep in mind that I need to do this for a Demo project, where I output c# code to the user to teach them how certain controls are coded.
I am given a .cs file and I need to output the contents. There is at least one class in the file, and at most…a lot. I need to output the whole file, EXCEPT one type of class. The specific type of class that I want to prevent being outputted all inherit a certain base class, so they should be easy to distinguish.
Here is an example:
public abstract class A{}
public class B{]
public class C{}
Assume these are the base-types that some of my classes may inherit. I want to prevent outputting all classes that inherit from A. A is probably going to be the only abstract base class so if that can help in anyway, that would be awesome.
Let’s say I’m given a file, example.cs:
using System;
using OtherStuff;
namespace blah.blahagain.someotherblah
{
[AttributeOne]
[AttributeTwo]
[AttributeThree]
public class AA: A
{
//stuff
}
public class BB: B
{
//stuff
}
public class CC: C
{
//stuff
}
public class D
{
//stuff
}
}
And the output should be
using System;
using OtherStuff;
namespace blah.blahagain.someotherblah
{
public class BB: B
{
//stuff
}
public class CC: C
{
//stuff
}
public class D
{
//stuff
}
}
The only way I have thought of is brute-force string manipulation. I can’t, however, use whitespace as a separator between classes because there is no guarantee if there will even be white space between classes. I will need to keep track of open and closed curly brackets to discover where one class begins and another end. I also need to test for the base class of each class by testing the string tokens before the first {} pair.
Also I need to prevent the attributes of AA from outputted too.
Since there are many brighter minds out there, I am here to ask if there is another simpler/cleaner method for doing what I need.
Thanks for reading!
Edit after YetAnotherUser’s answer: The output should be exactly the same as the file, which includes all comments.
Another edit: Instead of answering with certain software or libraries that could do this, I would more prefer algorithms. Maybe regular expressions? I am not good with them so I do not know the extend that they can be used for.
Could you wrap everything you need to exclude with:
See the #region documentation
This should be relatively easy to scan for and exclude. It also gives you the added benefit of showing what you’re hiding in the IDE.