简体   繁体   English

C#-如何在VS2015中构建/调试此代码?

[英]C# - How do I build/debug this code in VS2015?

I was given some code for a simple text file parser that I would like to build on and modify. 给了我一些我想在其上进行修改的简单文本文件解析器的代码。 It was built in VS and I've installed VS2015 Community so that I can work with it, but for the life of me I can't figure out how to set it up in VS2015. 它是在VS中构建的,并且我已经安装了VS2015社区,以便可以使用它,但是对于我一生来说,我不知道如何在VS2015中进行设置。

A snippet of the very beginning of the code is below. 下面是代码开头的一小段。 Do I build it as a class, or a console application, or something else? 是否将其构建为类,控制台应用程序或其他? How can I modify it to read a local file line by line? 如何修改它以逐行读取本地文件?

Any help would be tremendously appreciated! 任何帮助将不胜感激!

using System;
using System.Collections.Generic;
using System.IO;

public static class Cawk
{
public static IEnumerable<Dictionary<string, object>> Execute(StreamReader input)
{
    Dictionary<string, object> row = new Dictionary<string, object>();

    string line;
    //string[] lines = File.ReadAllLines(path);

    //read all rows
    while ((line = input.ReadLine()) != null)
    {

The snippet you posted is a class file definition. 您发布的代码段是一个类文件定义。

You will need a class called Cawk.cs with that code inside. 您将需要一个名为Cawk.cs的类,其中包含该代码。

To run it, you will need something to invoke it, either a console app or a unit test will do. 要运行它,您需要一些东西来调用它,控制台应用程序或单元测试都可以。

For a console app: 对于控制台应用程序:

  • create a new console application project. 创建一个新的控制台应用程序项目。
  • add a new class named Cawk.cs with your code inside. 在您的代码中添加一个名为Cawk.cs的新类。
  • in the 'program.cs' class (created when you create the console project), inside the Main method, call your Execute method. 在Main方法内的“ program.cs”类(在创建控制台项目时创建)中,调用Execute方法。

To debug it, put a breakpoint on a line and press F5. 要调试它,请将断点放在一行上,然后按F5。

Considering you've never build an application in visual studio, the easiest way are: 考虑到您从未在Visual Studio中构建过应用程序,最简单的方法是:

  1. Start VS 开始VS
  2. Create new project: File -> New -> Project 创建新项目:File-> New-> Project
  3. Select Templates -> Visual C# -> "Console application" 选择模板-> Visual C#->“控制台应用程序”
  4. Choose a folder to save the project, click OK. 选择一个文件夹来保存项目,然后单击“确定”。

That will give you a basic console application with one file Program.cs that has static method Main() inside. 这将为您提供一个基本的控制台应用程序,其中包含一个文件Program.cs,其中包含静态方法Main()。 Now let's add the new class. 现在让我们添加新类。

  1. Right click the solution tree, choose Add -> New item 右键单击解决方案树,选择添加->新建项目
  2. Choose "Class", enter name "Cawk", click OK. 选择“类别”,输入名称“ Cawk”,然后单击“确定”。

You will create a new file "Cawk.cs" for Cawk class. 您将为Cawk类创建一个新文件“ Cawk.cs”。 Let's fill it up. 让我们填补它。

  1. Copy-paste your snippet in Cawk.cs, overwriting it's contents. 将您的代码段复制粘贴到Cawk.cs中,以覆盖其内容。
  2. Correct the namespace - it should be the same as in the Program.cs 更正名称空间-它应该与Program.cs中的名称空间相同

So it will become something like: 因此它将变为:

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication1
{
public static class Cawk
{
...
  1. Now you can call Cawk.Execute() static method from the Main() method. 现在,您可以从Main()方法调用Cawk.Execute()静态方法。

The method Execute() accepts a StreamReader object. 方法Execute()接受StreamReader对象。 This object reads data from a byte stream - a flow of data coming from some kind of source - user input, file, another application, etc. 该对象从字节流中读取数据-某种来源的数据流-用户输入,文件,其他应用程序等。

In order to parse a file through Cawk you need to instantiate StreamReader first with a proper constructor , and dispose it afterwards (see " using " statement in C#). 为了通过Cawk解析文件,您需要首先使用适当的构造函数实例化StreamReader,然后再对其进行处理(请参阅C#中的“ using ”语句)。

Let me provide you an example of code: 让我为您提供示例代码:

using (var sr = new StreamReader("C:\temp\file.txt"))
{
    var results = Cawk.Execute(sr);
    foreach (item in results)
    {
        // do something with item which is Dictionary<string, object>
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM