如何在方法中使用`using`语句

时间:2018-04-17 08:26:28

标签: c#

我想使用SendKeys.SendWait("{TAB}")方法,但不是在类的顶部声明它,而是在方法中。 我尝试过以下方式,但我做错了。

using (System.Windows.Forms.SendKeys w  =  new System.Windows.Forms.SendKeys)
{
    w.SendWait("{TAB}");
}

我收到错误说

  

语句必须可以隐式转换为System.IDisposable。

5 个答案:

答案 0 :(得分:2)

从Nutshell书中的C#x中可以很好地解释使用声明:

  

.NET Framework为需要的类型定义了一个特殊的接口   拆卸方法:

<template>
    <div>
        <input type="text" v-model="keywords">
        <ul v-if="results.length > 0">
            <li v-for="result in results" :key="result.id" v-text="result.name"></li>
        </ul>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        data() {
            return {
                keywords: null,
                results: []
            };
        },

        watch: {
            keywords(after, before) {
                this.fetch();
            }
        },

        methods: {
            fetch() {
                axios.get('api/search', { params: { keywords: this.keywords } })
                    .then(response => this.results = response.data)
                    .catch(error => {});
            }
        }
    }
</script>
     

C#的public interface IDisposable { void Dispose(); } 语句为调用提供了一个语法快捷方式   使用using处理实现IDisposable的对象   块。

     

例如:

try/finally
     

编译器将其转换为:

using (FileStream fs = new FileStream ("myFile.txt", FileMode.Open))
{  
    // ... Write to the file ... 
}
     

finally块确保即使在调用Dispose方法时也是如此   抛出异常或代码提前退出块。

在您的情况下:

FileStream fs = new FileStream ("myFile.txt", FileMode.Open); try { // ... Write to the file ... } finally { if (fs != null) ((IDisposable)fs).Dispose(); } 未实现System.Windows.Forms.SendKeys,因此您无需将其包含在IDisposable语句中,因为编译器无法调用using它的方法。

这将是长话短说。我希望它有所帮助。

答案 1 :(得分:1)

如果要阻止从外部作用域使用SendKeys w变量,只需使用不带using语句的大括号:

{
    System.Windows.Forms.SendKeys w = new System.Windows.Forms.SendKeys();
    w.SendWait("{TAB}");
}

顺便说一下:你在()之后错过了括号new System.Windows.Forms.SendKeys

答案 2 :(得分:1)

<强> TL; DR;

您尝试实例化的类没有实例方法,您希望使用类实例调用它。所以在实例化课程方面没有任何意义。可以在不实例化类的情况下调用静态方法。这就是你的代码块实际上应该是这样的:

System.Windows.Forms.SendKeys.SendWait("{TAB}");

完整明细

好吧,因为我可能感觉到你的印象是,如果你不想将变量声明为类的成员变量,而是作为方法中的局部变量,那么你需要使用using关键字。这的情况。

如果您想在方法中使用变量,那么您可以免费使用该变量而无需关注using关键字。

using关键字与如何清除/回收类实例占用的内存有关的特殊相关性,它不属于公共语言运行时(CLR)的范围。类别,例如SqlConnectionFileStream等属于此类别。这些类所消耗的内存不能被CLR清理,因为它不是.NET应用程序的托管内存空间(SQL Server,文件句柄)。

您的班级System.Windows.Forms.SendKeys不属于该类别。所以编译器禁止你在using块内使用。

现在,就解决编译问题而言,您将不得不简单地删除使用块。

问题是.NET框架类库(FCL)中处理非托管内存的任何类都实现了接口IDisposable。通过类实现IDisposable接口是我处理非托管内存的编译器的指示器。然后只有编译器允许在using块内实例化这些类。由于System.Windows.Forms.SendKeys是.NET FCL中的预定义类,您无法对其进行修改,因此您无法实现IDisposable。因此,摆脱错误的唯一选择是删除using块。

我又检查了一件事。 SendKey类的定义如下所示:

namespace System.Windows.Forms
{
    public class SendKeys
    {
        //
        // Summary:
        //     Processes all the Windows messages currently in the message queue.
        public static void Flush();

        public static void Send(string keys);

        public static void SendWait(string keys);
    }
}

因此,您尝试实例化的类没有实例方法。因此,根本没有必要实例化该类。您可以在不实例化类的情况下调用方法。这就是你的代码块实际上应该是这样的:

System.Windows.Forms.SendKeys.SendWait("{TAB}");

答案 3 :(得分:1)

方法中的using关键字用于实现IDisposable的对象,而不是别名类。你想做什么是可能的,但是我并不是真的推荐它,因为它没有任何意义:

public class Foo : IDisposable {
    public void SendWait(string keys) {
        SendKeys.SendWait(keys);
    }
    public void Dispose() { }
}

然后你可以按照自己的意愿使用它:

using (Foo w = new Foo()) {
    w.SendWait("{TAB}");
}

但实际上,只是按照你原来想要的别名。

另见using Statement vs using Directive

答案 4 :(得分:0)

您的类必须实现IDisposible接口才能使其正常使用。这是使用using

装饰课程的必备指南

您收到错误,因为Sendkeys未实施IDisposible

参考文档:using Statement (C# Reference) - 提供方便的语法,确保正确使用IDisposable对象。