Reputation: 758
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();
Upvotes: 1
Views: 408
Reputation: 50235
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?
Upvotes: 3
Reputation:
There's no easy way to run code (ie: contents of a method) from a string since C# is already compiled before its executable is launched.
However, there are tricks you can do with Reflection and Diagnostics, but that requires you to run your code in Debug mode. If it's just a small app, this shouldn't be a problem. But larger applications with suffer performance issues when ran in debug mode.
For more info: Execute a string in C# 4.0
Upvotes: 0
Reputation: 2583
Since at runtime the code is in IL (intermediate language), you actually don't have the readable code at your disposal.
Anyway you could use reflection to create an on-fly decompiler, or use third party library like this one or this one to decompile the methods... but I don't know if they expose some API, I just know them as GUI tools.
Upvotes: 2
Reputation: 333
valeriano your static methods could be Linq Expressions, then you could iterate through each node of the expression. Using Reflection works too but you need more code
Upvotes: 0
Reputation: 33738
You might think about storing your solutiuons (source code) in text files.. (eg. Problem1.projecteuler, ProblemN.projecteuler) and loading these text files both for display and for compilation/execution plugin-style. Check out System.Reflection
namespace and do some web searches for "C# plugin tutorial" or some such to get started.
Upvotes: 1