PocketPC Windows CE 上的 C# 扩展方法

发布于 2024-07-24 17:33:10 字数 301 浏览 11 评论 0原文

CE 框架上也可以使用扩展方法吗? 我有一个字符串扩展方法,可以在 Windows 窗体项目中正常工作,但它不会在 PocketPC 应用程序中构建。

我认为这很容易找到,但是我无法找到有关 PocketPC 上扩展方法的任何信息。

编辑:哎呀,这是我的错误。 我在 Visual Studio 2008 中编写了扩展方法,但是 PocketPC 项目是在 Visual Studio 2005 中编译的,我没有意识到这一点。 好吧,那是我一生中再也回不来的一个小时了。 不管怎样,谢谢大家的回答。

Are extension methods available on CE framework as well? I have an extension method for string that works fine in a windows forms project, however it wont build in PocketPC application.

I figured this would be an easy thing to find out, however I was unable to find any info regarding extension methods on PocketPC.

Edit: Ooops this was my mistake. I wrote the extension method in Visual Studio 2008, however the PocketPC project was being compiled in Visual Studio 2005, which I didn't realised. Well that's a hour of my life I'm never getting back. Thanks everyone for answers anyway.

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

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

发布评论

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

评论(5

醉酒的小男人 2024-07-31 17:33:11

是的,CF 3.5 支持它们。 如果您使用 CF 2.0,您将需要定义 ExtensionAttribute,然后它们才能工作。

    // this is a definition of a 3.5 class for use in 2.0.  If we upgrade to target CF3.5, we will need to remove it...
    namespace System.Runtime.CompilerServices 
    { 
        public class ExtensionAttribute : Attribute { } 
    }

namespace TestExtension
{
    public static class Extensions
    {
        public static int TestMethod(this string value)
        {
            return value.ToString();
        }
    }
}

Yes, they are supported in CF 3.5. If you are using CF 2.0 you will need to define the ExtensionAttribute and then they will work.

    // this is a definition of a 3.5 class for use in 2.0.  If we upgrade to target CF3.5, we will need to remove it...
    namespace System.Runtime.CompilerServices 
    { 
        public class ExtensionAttribute : Attribute { } 
    }

namespace TestExtension
{
    public static class Extensions
    {
        public static int TestMethod(this string value)
        {
            return value.ToString();
        }
    }
}
唯憾梦倾城 2024-07-31 17:33:11

根据 这篇博文。 不过,我预计他们需要 Compact Framework 3.5 版。 您使用的是哪个版本?

They're supported in the Compact Framework according to this blog post. However, I expect they require Compact Framework version 3.5. Which version are you using?

长亭外,古道边 2024-07-31 17:33:11

您的目标框架版本是什么? 如果您的目标是 VS2008 中的 CF 2.0,您可能需要声明 ExtensionAttribute...

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

What framework version are you targetting? If you are targetting CF 2.0 from VS2008, you may need to declare ExtensionAttribute...

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
         | AttributeTargets.Method)]
    public sealed class ExtensionAttribute : Attribute {}
}
自控 2024-07-31 17:33:11

您可以根据 此博客。

You can use it with the .NET Compact Framework 2.0 and VS2008 through a small hack according to this blog.

葮薆情 2024-07-31 17:33:10

想在这里澄清一些混乱。 扩展方法是编译器的一个功能,不一定是框架的特定版本。 因此,扩展方法可以在任何平台上使用,只要有一个编译器版本既支持扩展方法又支持该平台。

C# 3.0 编译器可以将目标降低到 2.0 框架并支持扩展方法,因此它们应该可以在紧凑框架上使用。

框架实际为扩展方法提供的唯一内容是 ExtensionAttribute。 但是,它没有任何与之关联的功能,并且可以由您的应用程序定义(如果它不可用)。 这是我写的关于该主题的博客文章

Wanted to clear up a bit of confusion here. Extension methods are a feature of the compiler, not necessarily a particular version of the framework. Therefore, extension methods can be used on any platform where there is a version of the compiler that supports both extension methods and that platform.

The C# 3.0 compiler can down target to 2.0 frameworks and supports extension methods so they should be available on the compact framework.

The only thing the framework actually provides for extension methods is the ExtensionAttribute. However this doesn't have any functionality associated with it and can be defined by your application if it's not available. Here is a blog post I wrote on the subject

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