Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

VB.NET 어셈블리의 .NET Reflector 소스 코드를 분석할 때 알아두면 좋은 사항

VB.NET으로 만들어진 모듈을 살펴보면서 컴파일까지 해보고 싶은 마음에 .NET Reflector를 이용하여 프로젝트 내보내기를 했습니다. 그랬더니 다음과 같은 식의 코드가 생성되었습니다.

<STAThread> _
Public Shared Sub Main()
    Dim test2 As Short = 2
    Dim test As String = Conversions.ToString(
        Operators.ConcatenateObject((Conversions.ToString(CInt(test2)) & "test"), "TEST"))
    Try 
    Catch exception1 As Exception
        ProjectData.SetProjectError(exception1)
        Dim ex As Exception = exception1
        Console.WriteLine(ex.Message)
        Throw ex
        ProjectData.ClearProjectError
    End Try
End Sub

애석하게도 위의 소스 코드는 정상적으로 컴파일되지 않습니다. 왜냐하면 Conversions, Operators, ProjectData 타입들에 대해 "'ProjectData' is not declared. It may be inaccessible due to its protection level." 같은 식의 접근 제한 오류가 발생하기 때문인데요. .NET Reflector에서 해당 소스 코드의 연결된 링크를 따라가 보면 Microsoft.VisualBasic DLL의 Microsoft.VisualBasic.CompilerServices 네임스페이스에 각각 정의된 것을 확인할 수 있었습니다.

그래서, Microsoft.VisualBasic 어셈블리를 Visual Basic .NET 프로젝트에서 참조를 시도했는데... ^^; "A reference to 'Microsoft.VisualBasic' could not be added. This component is already automatically referenced by the build system."이라는 오류를 내면서 진행이 안됩니다. 어허~~~ ^^; 문제군요. 그러다가, 문득 예전에 VB를 하면서 알았던 CStr 같은 VB 내장함수가 생각났습니다. 혹시 그런 것들의 확장이 아닐까 싶어서 테스트 삼아서 VB.NET으로 몇 가지 소스 코드를 테스트 해보니 답이 나왔습니다. ^^

우선, Conversions는 CStr 유의 VB 내장함수를 사용하는 경우 .NET에 의해 치환되는 것이 맞았습니다. 그다음 Operators 유들은 VB에서 "& (ampersand)" 연산자를 이용하는 경우 발생했습니다. 예를 들어, Object 개체와 문자열 개체를 &로 연결하면 Operators.ConcatenateObject 메서드 호출로 변경됩니다.

마지막으로 ProjectData의 SetProjectError나 ClearProjectError가 문제였는데요. 이건 별도로 바꿔주기보다는 그냥 삭제해 주시면 됩니다. 따라서, 위의 .NET Reflector 소스 코드를 재현하는 VB.NET 코드는 다음과 같이 재구성됩니다.

Module Module1

    Sub Main()
        Dim test As String
        Dim test2 As Int16

        Dim objTest As Object

        test2 = 2

        test = CStr(test2) & "test"

        objTest = test

        test = CStr(objTest & "TEST")

        Try
            Dim a As Array
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw (ex)
        End Try

    End Sub

End Module

어찌되었든, 하위 호환성을 위한 VB.NET 팀의 노력이 엿보이는 부분입니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 6/27/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12211정성태4/27/202019264개발 환경 구성: 486. WSL에서 Makefile로 공개된 리눅스 환경의 C/C++ 소스 코드 빌드
12210정성태4/20/202020706.NET Framework: 903. .NET Framework의 Strong-named 어셈블리 바인딩 (1) - app.config을 이용한 바인딩 리디렉션 [1]파일 다운로드1
12209정성태4/13/202017411오류 유형: 614. 리눅스 환경에서 C/C++ 프로그램이 Segmentation fault 에러가 발생한 경우 (2)
12208정성태4/12/202015973Linux: 29. 리눅스 환경에서 C/C++ 프로그램이 Segmentation fault 에러가 발생한 경우
12207정성태4/2/202015820스크립트: 19. Windows PowerShell의 NonInteractive 모드
12206정성태4/2/202018438오류 유형: 613. 파일 잠금이 바로 안 풀린다면? - The process cannot access the file '...' because it is being used by another process.
12205정성태4/2/202015103스크립트: 18. Powershell에서는 cmd.exe의 명령어를 지원하진 않습니다.
12204정성태4/1/202015101스크립트: 17. Powershell 명령어에 ';' (semi-colon) 문자가 포함된 경우
12203정성태3/18/202017944오류 유형: 612. warning: 'C:\ProgramData/Git/config' has a dubious owner: '...'.
12202정성태3/18/202021204개발 환경 구성: 486. .NET Framework 프로젝트를 위한 GitLab CI/CD Runner 구성
12201정성태3/18/202018435오류 유형: 611. git-credential-manager.exe: Using credentials for username "Personal Access Token". [1]
12200정성태3/18/202018528VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
12199정성태3/17/202016171오류 유형: 610. C# - CodeDomProvider 사용 시 Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '...\f2_6uod0.tmp'.
12198정성태3/17/202019531오류 유형: 609. SQL 서버 접속 시 "Cannot open user default database. Login failed."
12197정성태3/17/202018823VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기 [1]
12196정성태3/17/202015945오류 유형: 608. The ServicedComponent being invoked is not correctly configured (Use regsvcs to re-register).
12195정성태3/16/202018267.NET Framework: 902. C# - 프로세스의 모든 핸들을 열람 - 세 번째 이야기
12194정성태3/16/202020996오류 유형: 607. PostgreSQL - Npgsql.NpgsqlException: sorry, too many clients already
12193정성태3/16/202017908개발 환경 구성: 485. docker - SAP Adaptive Server Enterprise 컨테이너 실행 [1]
12192정성태3/14/202019938개발 환경 구성: 484. docker - Sybase Anywhere 16 컨테이너 실행
12191정성태3/14/202021044개발 환경 구성: 483. docker - OracleXE 컨테이너 실행 [1]
12190정성태3/14/202015625오류 유형: 606. Docker Desktop 업그레이드 시 "The process cannot access the file 'C:\Program Files\Docker\Docker\resources\dockerd.exe' because it is being used by another process."
12189정성태3/13/202021228개발 환경 구성: 482. Facebook OAuth 처리 시 상태 정보 전달 방법과 "유효한 OAuth 리디렉션 URI" 설정 규칙
12188정성태3/13/202026020Windows: 169. 부팅 시점에 실행되는 chkdsk 결과를 확인하는 방법
12187정성태3/12/202015594오류 유형: 605. NtpClient was unable to set a manual peer to use as a time source because of duplicate error on '...'.
12186정성태3/12/202017402오류 유형: 604. The SysVol Permissions for one or more GPOs on this domain controller and not in sync with the permissions for the GPOs on the Baseline domain controller.
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...