[vb.net] ToString()와 같은 확장 메서드(Visual Basic) > vb.net

본문 바로가기

vb.net

[기타] [vb.net] ToString()와 같은 확장 메서드(Visual Basic)

회원사진
하나를하더라도최선을
2021-05-06 20:29 6,073 0

본문



Option Strict Off
Imports System.Runtime.CompilerServices
 
Module Module4
 
    Sub Main()
        Dim aString As String = "Initial value for aString"
        aString.PrintMe()
 
        Dim anObject As Object = "Initial value for anObject"
        ' The following statement causes a run-time error when Option
        ' Strict is off, and a compiler error when Option Strict is on.
        'anObject.PrintMe()
    End Sub
 
    <Extension()> 
    Public Sub PrintMe(ByVal str As String)
        Console.WriteLine(str)
    End Sub
 
    <Extension()> 
    Public Sub PrintMe(ByVal obj As Object)
        Console.WriteLine(obj)
    End Sub
 
End Module
 
 
 
 
 
Imports System.Runtime.CompilerServices
 
Module StringExtensions
 
    <Extension()> 
    Public Sub Print(ByVal aString As String)
        Console.WriteLine(aString)
    End Sub
 
End Module 
 
 
 
 
확장 메서드의 정의 및 사용 (C#)
Module Class1
Imports System.Runtime.CompilerServices
 
Module ExtensionMethods
    <Extension()>
    Sub Print(ByVal s As String)
        Console.WriteLine(s)
    End Sub
End Module
 
Class ExampleClass
    Public Shared Sub Main(ByVal args As String())
        Dim text As String = "Hello from ExtensionMethods"
        '// 아래의 두 코드는 동일한 결과가 나옵니다.
        "Hello from ExtensionMethods".Print()
        text.Print()
    End Sub
End Class

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색