让我的界面像IDisposable一样添加额外的样板魔术 [英] Make my interface add the extra boilerplate magic like IDisposable does

查看:70
本文介绍了让我的界面像IDisposable一样添加额外的样板魔术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你有一个实现IDisposable的类并且在IDisposable之后按下return时,IDE会在类中添加一堆代码,如: -



When you have a class that implements IDisposable and you press return after IDisposable, the IDE adds a load of code into the class like:-

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region





有什么方法可以获得类似的行为我自己构建的界面?



注意 - 我不想对IDisposable本身做任何事情。我只想对我的接口做IDE用IDisposable做的事情 - 当我在Implements Disposable(评论,区域等)之后点击返回时,所有上述内容都由IDE输入 - 我希望我的界面也能这样做因此,当开发人员实现我的界面时,可以输入使用提示。



注2:好的 - 我决定使用代码片段相反 - 从开发人员的角度来看也很容易。



is there any way to get a similar behaviour with my own built interfaces?

Note - I don't want to do anything with IDisposable itself. I just want to do to my interfaces what the IDE does with IDisposable - all of the above is put in by the IDE when I hit return after "Implements Disposable" (comments , region etc.) - I want my interface to do the same so that usage hints can be put in when a developer implements my interface.

Note 2: OK - I have decided to use code snippets instead - it is just as easy from the developer point of view.

推荐答案

请参阅我对该问题的评论。从你的问题来看,很难理解你是否真的理解现有的语法糖是如何工作的。



我怀疑你没有。我是这么认为的,因为你甚至没有试图证明语法糖的用法应该如何运作。对于这种类型,本质不仅仅是接口本身,而是VB.NET或C#使用语句

https://msdn.microsoft.com/en-us/library/htd05whh.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs。 110%29.aspx [ ^ ]。



如果我弄错了你;你真的明白它是如何工作的,我会很高兴的。



参见:http://en.wikipedia.org/wiki/Syntactic_sugar [ ^ ]。



您从未显示过类似使用...结束使用。此外,您的受保护的可覆盖子处置(处置为布尔值) 完全无关



从代码示例中可以看出,End using发生在代码中。这是调用resourcelist | resourceexpression的 IDisposable.Dispose 方法的代码点。 注意:没有别的。您所能做的就是以不同的方式实现 IDisposable 。如果您从而不是 IDisposable ,而是从这个界面派生自己的界面,一切都将完全相同,因为您仍然必须实现 IDisposable .Dispose ,这个实现将以完全相同的方式调用。你只会添加一些更多的成员,并且必须实现它们。



现在,注意使用...结束使用语言语法的一部分。 并且您无法更改语言语法。如果您实现自己的语言,则只能更改语法,这也是可能的。否则,你的问题没有多大意义。



这样,你得到了答案。如果你有其他一些想法,你必须解释一下。现在,它几乎没有。对不起,我希望你有一些想法。



-SA
Please see my comments to the question. From your question, it's hard to understand if you really understand how even the existing syntactic sugar works.

I suspect that you don't. I think so because you did not even try to demonstrate how the usage of syntactic sugar should work. For this kind, the essence is not just the interface itself, but the VB.NET or C# using statement:
https://msdn.microsoft.com/en-us/library/htd05whh.aspx[^],
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^].

If I'm mistaken about you; and you are really do understand how it works, I would be only very glad.

See also: http://en.wikipedia.org/wiki/Syntactic_sugar[^].

You never showed anything like Using... End using. Besides, your Protected Overridable Sub Dispose(disposing As Boolean) is totally irrelevant.

As you can see from the code sample, "End using" take place in the code. This is the point on code where the IDisposable.Dispose methods of "resourcelist | resourceexpression" are called. Note: nothing else. All you can do is to implement IDisposable in different ways. If, instead of IDisposable, you derive your own interface instead from this interface, everything will work exactly the same, because you will still have to implement IDisposable.Dispose, and this implementation will be called exactly in the same way. You only will add some more members and will have to implement them.

Now, pay attention that Using... End using is the part of the language syntax. And you cannot alter the language syntax. You can only change the syntax if you implement your own language, which is also possible. Otherwise, your question makes little sense.

This way, you got the answer. If you have some other ideas, you have to explain that. Right now, it's pretty much nothing. Sorry, but I hope you got some food for thought.

—SA


这篇关于让我的界面像IDisposable一样添加额外的样板魔术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆