可以使用C#中的代理返回方法的代码(作为字符串)吗? [英] Is it possible to return the code of a method (as a string) using delegates in C#?

查看:95
本文介绍了可以使用C#中的代理返回方法的代码(作为字符串)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将举例说明,因为我不知道我是否正确地提出了这个问题(英语不是我的主要语言,还有我还在学习C#)。


$ b $我已经开始执行Project Euler,并决定创建一个应用来跟踪我的结果,并把我的小C#知识进行测试。



在一个特定的类中,我拥有用于​​解决每个问题的静态函数。

  static class Answers()
{
public static string A1(){...}
public static string A2(){...}
public static string A3(){...}
//它在
}

问题对象将被创建为这样(问题类定义和对象创建在运行时)。

 类问题
{
public string Description;
public Solution SolutionFinder;

public问题(string Desc,Solution Solver)
{
this.Description = Desc;
this.SolutionFinder = Solver;
}

public delegate string Solution();

public string SolveProblem()
{
return SolutionFinder.Invoke();
}
}

这是在我的表单创建代码:

  {
...
列表<问题> Euler = new List< Problem>();
Euler.Add(new Problem(Find the sum of all the multiple each of 3 or 5 below 1000.,Answers.A1));
Euler.Add(新问题(通过考虑斐波纳契序列中的值不超过四百万的条件,找到偶数项的总和,Answers.A2));
Euler.Add(new问题(600851475143的最大素数是什么?,Answers.A3));
...
}

我得到了课程,甚至代表了正确地工作,我很高兴。然后我终于决定在表单上显示整件事情。 我想要显示的是:每当我使用我的表单解决问题时,对问题的描述(这样做)和每种方法的代码(无论是A1,A2等) strong>



是否清楚?只是我想要我的表单显示结果,如何我得到每个问题的解决方案,但不必重新输入每个方法的内容只是为了显示 - 方法已经存在。 p>

请不要介意凌乱的代码,过度使用公共成员:我明白这是一个不好的做法,但现在我只是试图通过这个个人项目,我相信这是可以的,因为这只是一个小的学习经验。



谢谢。



编辑]
我要查找的格式是:

  void UpdateForm(int Current)
{
问题CurrentProblem =欧拉[Current-1];
string Desc = CurrentProblem.Description;
string Code = CurrentProblem.SolutionFinder.Method.ReturnType.Name;
//我有这么远,但是我需要显示的不仅仅是方法的名称!
...
}

澄清



给定方法:

  public static string A1(){ 
var answer = 1 + 1;
return answer.ToString();
}

是否可以在字符串中获取以下行? p>

  var answer = 1 + 1; 
return answer.ToString();


解决方案

虽然不是最好的方法, 复制到输出目录值为始终复制(或复制如果较新)通过源文件的属性(右键单击/属性),您可以自己解析代码文件,同时优化编码风格



要添加,您是否知道您基本上是重建NUnit,除了源代码转储?


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?

这篇关于可以使用C#中的代理返回方法的代码(作为字符串)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆