扩展方法 +控制台应用程序

发布于 2024-08-07 16:26:37 字数 881 浏览 2 评论 0原文

好吧,我真的被难住了。

我以前写过扩展方法,从来没有遇到过任何问题。但是,我从来没有在控制台应用程序中使用过它们。以下代码无法编译,我不知道为什么!我创建了一个简单的控制台应用程序来尝试一下,但它不起作用:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hello";

            Console.WriteLine(s.TestMethod());

            Console.Read();
        }
    }

    public static class ExtensionTest
    {
        public static string TestMethod(this string input)
        {
            return input.ToUpper();
        }
    }
}

任何人都可以看到这里出了什么问题吗?

我遇到的第一个错误是第 21 行的“Type Expected”,即:

(this string input)

我知道我可以很容易地更改为:

Console.WriteLine(ExtensionTest.TestMethod(s));

但我真的很想知道为什么我不能在控制台中编写此扩展方法应用程序。

感谢您的帮助!

Ok, i'm well and truly stumped here.

I've written extension methods before, and never had any problems. However, i've never had to use them in Console Apps. The following code won't compile and I have no idea why! I created a simple console app to try it out and it just won't work:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hello";

            Console.WriteLine(s.TestMethod());

            Console.Read();
        }
    }

    public static class ExtensionTest
    {
        public static string TestMethod(this string input)
        {
            return input.ToUpper();
        }
    }
}

Can anyone see what's wrong here?

The first error i'm running into is "Type expected" on line 21, which is:

(this string input)

I know i could quite easily just change to:

Console.WriteLine(ExtensionTest.TestMethod(s));

But i'd really like to know why i can't write this extension method in a console app.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-08-14 16:26:37

刚刚使用该确切的文本创建了一个新的控制台应用程序,它按预期编译并运行。确定它针对的是正确的框架吗?

Just created a new Console application using that exact text and it compiled and worked as expected. Sure it's targeting the right framework?

帅气尐潴 2024-08-14 16:26:37

你确定你使用的是C#3.0吗?该代码没有问题。

如果您的目标是使用 C# 3.0 的 .NET 2.0,您仍然可以通过在项目中定义以下类型来使用扩展方法:

namespace System.Runtime.CompilerServices
{
     [AttributeUsage(AttributeTargets.Method)]
     public class ExtensionAttribute : Attribute
     {
         public ExtensionAttribute() { }
     }
}

You sure you are using C#3.0? The code doesn't have a problem in it.

If you are targeting .NET 2.0 with C# 3.0 you can still ue extension methods by defining the following type in your project:

namespace System.Runtime.CompilerServices
{
     [AttributeUsage(AttributeTargets.Method)]
     public class ExtensionAttribute : Attribute
     {
         public ExtensionAttribute() { }
     }
}
吃兔兔 2024-08-14 16:26:37

看起来,您的项目已设置为针对 .NET Framework v2.0 进行编译。尝试在项目属性中切换到 v3.5 并引用 System.Core 程序集。

It seems, your project is set up to compile for v2.0 of .NET framework. Try switching to v3.5 in project properties and referencing System.Core assembly..

潇烟暮雨 2024-08-14 16:26:37

确保您的项目面向 .NET Framework v3.0 或更高版本,并确保 System.Core 是项目中引用的程序集。

Ensure your project targets .NET Framework v3.0 or higher and make sure System.Core is a referenced assembly in your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文