1

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();
qxn
  • 17,162
  • 3
  • 49
  • 72
mkvlrn
  • 758
  • 1
  • 8
  • 20
  • 1
    Okay, so it looks like it should already work... what's the exact *question* here? – Jon Skeet Mar 07 '12 at 19:50
  • Are you trying to print out the output of a function and its source code as well? – Jon B Mar 07 '12 at 19:53
  • 1
    @JonSkeet -- He is asking, basically, if he can get the contents of a function into a string. So, if he has a method int MyFunc() { return 1; }, he wants a string that says "return 1;". – qxn Mar 07 '12 at 19:55
  • What you mean by `code for each method (whatever is inside A1, A2, etc.`? – sll Mar 07 '12 at 19:55
  • @ken: Are you sure? I don't see any indication of integers here, other than as the parameter for `UpdateForm`... – Jon Skeet Mar 07 '12 at 19:56
  • @ ken That's precisely what I'm trying to ask! God, I need to study some more English too! – mkvlrn Mar 07 '12 at 19:57
  • @MikeValeriano : I believe problem is not in your English but in missed expected results example – sll Mar 07 '12 at 19:58
  • @ Jon Skeet: Yes, I'm trying to get the actual code inside my methods as a string, just for display. – mkvlrn Mar 07 '12 at 19:58
  • @JonSkeet -- Well, I've never been wrong before, but there's a first time for everything, I guess. – qxn Mar 07 '12 at 19:58
  • @ken: Did you edit your comment? I'm sure it didn't say that when I first replied to it :) It makes sense now, and I agree... – Jon Skeet Mar 07 '12 at 20:02
  • @sll: Exactly what ken said: If I have `string A1() { string foo = "bar"; return foo; }` I want to be able to return a string containing "string foo = 'bar'; return foo;", so I can display it on my form. – mkvlrn Mar 07 '12 at 20:07

5 Answers5

3

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?

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • 1
    +1 for "Copy to Output Directory". Good idea for his problem. – qxn Mar 07 '12 at 20:41
  • That's what I'm going for, at least for now. Reflection seems a bit too advanced for me at the moment, and going through a file and fetching some text is something I'm comfortable with. Thanks! – mkvlrn Mar 07 '12 at 20:44
2

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.

themarcuz
  • 2,573
  • 6
  • 36
  • 53
1

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.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • Oh, this looks interesting! I haven't reached Reflection in my course, but I'll do a little research on the matter. Thanks. – mkvlrn Mar 07 '12 at 20:02
0

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

Rafael Enriquez
  • 333
  • 3
  • 20
0

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

Community
  • 1
  • 1
John Suit
  • 1,254
  • 12
  • 17