I’ll exemplify, since I’m not sure if I’m asking the question correctly (English is not my primary language, plus I’m still learning C#).
I’ve started going through Project Euler, and decided to create an application to keep track of my results, and to put my little C# knowledge to test.
In a particular class I hold static functions that are used to solve each of the problems.
static class Answers()
{
public static string A1(){...}
public static string A2(){...}
public static string A3(){...}
//it goes on
}
Problem objects will be created like this (Problem class definition and object creation in runtime).
class Problem
{
public string Description;
public Solution SolutionFinder;
public Problem(string Desc, Solution Solver)
{
this.Description = Desc;
this.SolutionFinder = Solver;
}
public delegate string Solution();
public string SolveProblem()
{
return SolutionFinder.Invoke();
}
}
This is on my Form creation code:
{
...
List<Problem> Euler = new List<Problem>();
Euler.Add(new Problem("Find the sum of all the multiples of 3 or 5 below 1000.", Answers.A1));
Euler.Add(new Problem("By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.", Answers.A2));
Euler.Add(new Problem("What is the largest prime factor of the number 600851475143 ?", Answers.A3));
...
}
I got the classes and even the delegate thing to work correctly, and I’m thrilled with that. Then I finally decided to show the whole thing on a form. What I’m trying to show is: the description of the problem (this is done) and the code for each method (whatever is inside A1, A2, etc.) whenever I solve a problem using my form.
Is that clear? It’s just that I want my form to show the result and how I got the solution for each problem, but without having to retype the contents of each method just for display – the methods are already there.
And please don’t mind the messy code and overuse of public members: I understand it’s a bad practice, but for now I’m just trying to get through this personal project, and I believe it’s OK to do this here since it’s just a small learning experience.
Thanks.
[EDIT]
The format I’m looking for is:
void UpdateForm(int Current)
{
Problem CurrentProblem = Euler[Current-1];
string Desc = CurrentProblem.Description;
string Code = CurrentProblem.SolutionFinder.Method.ReturnType.Name;
//I got this far, but I need to display more than just the name of the method!
...
}
To Clarify
Given the method:
public static string A1() {
var answer = 1 + 1;
return answer.ToString();
}
Is it possible to obtain the following lines in a string..?
var answer = 1 + 1;
return answer.ToString();
While it’s not the fanciest approach, if you set the “Copy to Output Directory” value to “Copy Always” (or “Copy if newer”) via the Properties of the source file (right-click/ Properties), you can parse the code file on your own while optimizing for your coding style.
To add, are you aware that you are basically rebuilding NUnit, aside from the source code dump?