如何创建共享方法的扩展

时间:2011-07-24 08:05:25

标签: .net vb.net

我想为Image.FromStream

创建一个扩展方法
Public Shared Function FromStream(ByVal stream As System.IO.Stream) As System.Drawing.Image

有可能取消像

这样的处理
Public Shared Function FromStream(ByVal stream As System.IO.Stream, ByVal CloseTask As ManualResetEvent) As System.Drawing.Image

有可能吗?

2 个答案:

答案 0 :(得分:10)

不,您只能添加似乎将实例方法添加到某个类型的扩展方法 - 您无法使其看起来像您添加了新的共享方法到一个类型。

答案 1 :(得分:0)

我有点狡猾的方式来共享扩展程序。

我使用Extensions类来解决与我的函数具有相同签名的冲突

Module Main
    Public Class Extensions
        Public Class [Boolean]
            Public Shared Function TryParse(ByVal strValue As String, ByRef blnResult As Boolean) As Boolean
                If Not Object.Equals(strValue, Nothing) Then
                    If strValue = "Y" Then
                        blnResult = True
                        Return True
                    End If

                    If strValue = "N" Then
                        blnResult = False
                        Return True
                    End If
                End If

                Return Boolean.TryParse(strValue, blnResult)
            End Function
        End Class
    End Class

End Module
相关问题