有没有办法将这两个VB.Net扩展方法合并为一个?

时间:2014-07-28 10:23:55

标签: vb.net embed extension-methods asp.net-mvc-5.1

我将两种扩展方法链接在一起,以便在页面中处理和嵌入YouTube视频。原因是,直到刚才,嵌入是用短代码处理的。我刚才通过CodePlex的oEmbed.dll添加了嵌入式支持。下面的第一个方法是新方法,第二个方法是我需要向后兼容的短代码,所以我不打破以前的帖子。

那么有没有办法将这两个方法组合成一个方法,所以我不必将它们链接在一起?

<Extension>
Public Function EmbedYoutubeVideos(input As String) As String
    Dim regex__1 As String = "http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=‌​]*)?"
    Dim matches As MatchCollection = Regex.Matches(input, regex__1)
    If matches.Count = 0 Then
        Return input
    End If
    Dim width As Int32 = 620
    Dim height As Int32 = 349
    Dim objYouTube As New WillStrohl.API.oEmbed.Providers.YouTube
    Dim strVideo As String
    For i As Integer = 0 To matches.Count - 1
        Dim mediaFile As String = matches(i).Value
        strVideo = objYouTube.GetVideo(matches(i).Value, width, height)
        Return input.Replace(matches(i).Value, strVideo)
    Next
End Function

<Extension>
Public Function ConvertYouTubeShortCode(text As String) As String
    Dim regex__1 As String = "\[youtube:.*?\]"
    Dim matches As MatchCollection = Regex.Matches(text, regex__1)
    If matches.Count = 0 Then
        Return text
    End If
    Dim width As Int32 = 620
    Dim height As Int32 = 349
    Dim BaseURL As String = "http://www.youtube.com/v/"
    For i As Integer = 0 To matches.Count - 1
        Dim length As Int32 = "[youtube:".Length
        Dim mediaFile As String = matches(i).Value.Substring(length, matches(i).Value.Length - length - 1)
        Dim player As String = "<div class=""video-container"">"
             <iframe width=""{2}"" height=""{3}"" src=""{4}{1}"" 
              frameborder=""0""    allowfullscreen></iframe></div>"
        Return text.Replace(matches(i).Value, [String].Format(player, i, mediaFile, width, height, BaseURL))
    Next
End Function

1 个答案:

答案 0 :(得分:0)

或许像以下那样?

<System.Runtime.CompilerServices.Extension()>
Public Function ConvertYouTubeShortCode(text As String, Optional ByVal IsEmbed As boolean = false) As String
    Dim regex__1 As String
    Dim matches As MatchCollection
    Dim width As Int32 = 620
    Dim height As Int32 = 349  
    Dim mediaFile As String

    If (IsEmbed) Then
        regex__1 = "http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=‌​]*)?"
        matches = Regex.Matches(text, regex__1)

        If matches.Count = 0 Then Return text

        Dim objYouTube As New WillStrohl.API.oEmbed.Providers.YouTube
        Dim strVideo As String  
        For i As Integer = 0 To matches.Count - 1 Step 1
            mediaFile = matches(i).Value
            strVideo = objYouTube.GetVideo(matches(i).Value, width, height)
            Return text.Replace(matches(i).Value, strVideo)
        Next      
    Else
        regex__1 = "\[youtube:.*?\]"
        matches = Regex.Matches(text, regex__1)

        If matches.Count = 0 Then Return text

        Dim BaseURL As String = "http://www.youtube.com/v/"
        Dim length As Int32
        Dim player As String         
        For i As Integer = 0 To matches.Count - 1 Step 1
            length = "[youtube:".Length
            mediaFile = matches(i).Value.Substring(length, matches(i).Value.Length - length - 1)
            player = "<div class=""video-container""><iframe width=""{2}"" height=""{3}"" src=""{4}{1}"" frameborder=""0"" allowfullscreen></iframe></div>"
            Return text.Replace(matches(i).Value, [String].Format(player, i, mediaFile, width, height, BaseURL))
        Next
    End If
End Function

编辑:第一次迭代返回时,循环的重点是什么?