如何在C#控制台应用程序中修复错误CS1061?

时间:2019-10-30 00:03:09

标签: c# .net unix visual-studio-code

我目前正在学习C#和.NET,目前正在学习如何制作NuGet软件包。我拥有的文件之一就是这样设置的:

using System.Text.RegularExpressions;

 namespace Packt.CS7
 {
public static class StringExtensions
{
    public static bool IsValidXmlTag(this string input)
    {
        return Regex.IsMatch(input, @"^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$");
    }

    public static bool IsValidPassword(this string input)
    {
        return Regex.IsMatch(input, "^[a-zA-Z0-9_-]{8,}$");
    }

    public static bool IsValidHex(this string input)
    {
        return Regex.IsMatch(input, "^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$");
    }
}
}

我的Program.cs设置如下:

using System;
using System.Xml.Linq;
using static System.Console;
using Packt.CS7;

namespace Assemblies
{
class Program
{
    static void Main(string[] args)
    {
        Write("Enter a valid color valid in hex: ");
        string hex = ReadLine();
        WriteLine($"Is {hex} a valid color value: {hex.IsValidHex()}");

        Write("Enter a valid XML tag: ");
        string xmlTag = ReadLine();
        WriteLine($"Is {xmlTag} a valid XML tag: {xmlTag.IsValidXmlTag()}");

        Write("Enter a valid password: ");
        string password = ReadLine();
        WriteLine($"Is {password} a valid password: {password.IsValidPassword()}");
    }
}
}

运行控制台应用程序时,它应输出:

Enter a valid color value in hex: 00ffc8
Is 00ffc8 a valid color value: True
Enter a valid XML tag: <h1 class="<" />
Is <h1 class="<" /> a valid XML tag: False
Enter a valid password: secretsauce
Is secretsauce a valid password: True

但是我在控制台中得到的是:

Program.cs(14,60): error CS1061: 'string' does not contain a definition for 'IsValidHex' and no 
accessible extension method 'IsValidHex' accepting a first argument of type 'string' could be found 
(are you missing a using directive or an assembly reference?) 
[/Users/steelwind/HardWay/c#and.NET/Chapter07/Assemblies/Assemblies.csproj]

The build failed. Fix the build errors and run again.

我相信问题在于,当我打开第一个文件时,在VSCode中打开该文件时,它说每个类都有“ 0引用”,这意味着在Program.cs中没有以某种方式调用它。 。但是我不确定为什么。

这是我的文件结构:

 Assemblies:
 >Assemblies.csproj
  ^bin
   ^Debug
    ^netcoreapp3.0
   ^Release
    ^netcoreapp3.0
     >Assemblies
    >Assemblies.deps.json
    >Assemblies.dll
    >Assemblies.pbd
    >Assemblies.runtimeconfig.dev.json
    >Assemblies.runtimeconfig.json
    >Newtonsoft.Json.dll
 ^obj
 >Program.cs

Shared library:
 ^bin
  ^Bin
   ^Debug
     ^netstandard2.0
      >SharedLibrary.deps.json
      >SharedLibrary.dll
      >SharedLibrary.pbd
     ^netstandard3.0
    wtsegarsMyFirstPackage1.0.0.nupkg
   ^Release
    ^netstandard
     >SharedLibrary.dll
     >SharedLibrary.pbd
 ^obj
 SharedLibrary.csproj
   StringExtensions.cs

1 个答案:

答案 0 :(得分:0)

(对我来说)有效。

A。测试

输出

// .NETCoreApp,Version=v3.0
Is 00ffc8 a valid color value: True
Is <h1 class="<" /> a valid XML tag: False
Is secretsauce a valid password: True

B。调试方法

您可以将namespace Packt.CS7更改为namespace MyNewNamespace,并将using Packt.CS7;更改为using MyNewNamespace;,看看是否可以编译。

如果您获得The type or namespace name 'Packt' could not be found (are you missing a using directive or an assembly reference?),则可能意味着您的应用程序集未引用必要的库/项目。

测试代码

using System;
using System.Text.RegularExpressions;
using Packt.CS7;

namespace Packt.CS7
{
    public static class StringExtensions
    {
        public static bool IsValidXmlTag(this string input)
         => Regex.IsMatch(input, @"^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$");

        public static bool IsValidPassword(this string input)
          => Regex.IsMatch(input, "^[a-zA-Z0-9_-]{8,}$");

        public static bool IsValidHex(this string input)
          => Regex.IsMatch(input, "^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$");
    }
}

namespace SixSixSix
{
    class Program
    {

        static void Main(string[] args)
        {
            string hex = "00ffc8";
            Console.WriteLine($"Is {hex} a valid color value: {hex.IsValidHex()}");

            string xmlTag = @"<h1 class=""<"" />";
            Console.WriteLine($"Is {xmlTag} a valid XML tag: {xmlTag.IsValidXmlTag()}");

            string password = "secretsauce";
            Console.WriteLine($"Is {password} a valid password: {password.IsValidPassword()}");
        }
    }
}