성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
[정성태] 저렇게 조각 코드 말고, 실제로 재현이 되는 예제 프로젝트를 압...
[정성태] Modules 창(Ctrl+Shift+U)을 띄워서, 해당 Op...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# 3.0의 기본 인자(default parameter)가 .NET 1.1/2.0에서도 실행될까?</h1> <p> 확인해 보면 될 문제입니다. ^^ 다음과 같이 기본 인자를 사용한 예제를 만들고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; class Program { static void Main(string[] args) { Test(); } public static void Test(<span style='color: blue; font-weight: bold'>int n = -1</span>) { Console.WriteLine(n); } } </pre> <br /> 컴파일 된 결과를 IL 코드로 보면, 기본 인자가 ".param ... " 구문으로 표현되는 것을 볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > .method public hidebysig static void Test([opt] int32 n) cil managed { <span style='color: blue; font-weight: bold'>.param [1] = int32(-1)</span> .maxstack 8 L_0000: nop L_0001: ldarg.0 L_0002: call void [mscorlib]System.Console::WriteLine(int32) L_0007: nop L_0008: ret } </pre> <br /> 즉, 저 구문을 가진 IL 코드를 .NET 1.1 용 ilasm.exe로 컴파일해보면 될텐데요. 그래서 다음과 같이 ".param [1] = int32(-1)" 코드는 보존하면서 내용상 .NET 1.1과 맞지 않는 부분을 제거해봤습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) <span style='color: blue; font-weight: bold'>.ver 2:0:0:0</span> // 2.0.0.0으로 되어 있지만 그래도 .NET 1.1 ilasm.exe에서 오류가 발생하지 않습니다. } .assembly ConsoleApp1 { .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20 // ...Copyright .. 20 32 30 31 34 00 00 ) // 2014.. .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 ) .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 ) .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 ) .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 13 43 6F 6E 73 6F 6C 65 41 70 70 6C 69 63 // ...ConsoleApplic 61 74 69 6F 6E 31 00 00 ) // ation1.. .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 13 43 6F 6E 73 6F 6C 65 41 70 70 6C 69 63 // ...ConsoleApplic 61 74 69 6F 6E 31 00 00 ) // ation1.. .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 66 30 34 36 37 37 66 65 2D 61 38 32 65 // ..$f04677fe-a82e 2D 34 66 33 38 2D 39 61 35 33 2D 38 38 63 62 31 // -4f38-9a53-88cb1 61 39 36 37 37 34 61 00 00 ) // a96774a.. .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 ) // ...1.0.0.0.. // --- The following custom attribute is added automatically, do not uncomment ------- // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. .hash algorithm 0x00008004 .ver 1:0:0:0 } .module ConsoleApp1.exe // MVID: {91066B88-B54D-43E0-8598-3BEDD98ADDA5} .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) .imagebase 0x00400000 .file alignment 0x00000200 .subsystem 0x0003 // WINDOWS_CUI .corflags 0x00000001 // ILONLY // Image base: 0x00E70000 // =============== CLASS MEMBERS DECLARATION =================== .class private auto ansi beforefieldinit Program extends [mscorlib]System.Object { .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 9 (0x9) .maxstack 8 IL_0000: nop IL_0001: ldc.i4.m1 IL_0002: call void Program::Test(int32) IL_0007: nop IL_0008: ret } // end of method Program::Main .method public hidebysig static void Test([opt] int32 n) cil managed { <span style='color: blue; font-weight: bold'>.param [1] = int32(0xFFFFFFFF)</span> // 기본 인자 값 구문이 있습니다. // Code size 9 (0x9) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 IL_0002: call void [mscorlib]System.Console::WriteLine(int32) IL_0007: nop IL_0008: ret } // end of method Program::Test .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 7 (0x7) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: ret } // end of method Program::.ctor } // end of class Program // ============================================================= // *********** DISASSEMBLY COMPLETE *********************** // WARNING: Created Win32 resource file test.res </pre> <br /> 이 파일을 test.il로 저장하고 .NET Framework 1.1만 설치된 PC에서 컴파일해 봤는데요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > D:\temp><span style='color: blue; font-weight: bold'>ilasm test.il /output:test.exe</span> Microsoft (R) .NET Framework <span style='color: blue; font-weight: bold'>IL Assembler. Version 1.1.4322.2307</span> Copyright (C) Microsoft Corporation 1998-2002. All rights reserved. Assembling 'test.il' , no listing file, to EXE --> 'test.exe' Source file is ANSI Assembled method Program::Main Assembled method Program::Test Assembled method Program::.ctor Creating PE file Emitting members: Global Class 1 Methods: 3; Resolving member refs: 1 -> 1 defs, 0 refs Writing PE file Operation completed successfully C:\temp><span style='color: blue; font-weight: bold'>dir</span> Volume in drive C has no label. Volume Serial Number is 5C71-DD99 Directory of C:\temp 2014-10-03 오전 10:26 <DIR> . 2014-10-03 오전 10:26 <DIR> .. 2014-10-03 오전 10:26 2,560 <span style='color: blue; font-weight: bold'>test.EXE</span> 2014-10-03 오전 10:25 4,965 test.il 2 File(s) 7,525 bytes 2 Dir(s) 17,396,654,080 bytes free C:\temp><span style='color: blue; font-weight: bold'>test</span> <span style='color: blue; font-weight: bold'>-1</span> </pre> <br /> 보시는 바와 같이 컴파일도 잘 되고 실행까지 정상동작합니다.<br /> <br /> 이것이 의미하는 바가 뭐냐면? 기본 인자 기능은 이미 .NET 1.1 초기부터 IL 코드에 구현되어 있었던 것입니다. 하지만, 문법적으로 C#에서는 3.0까지 와서야 비로소 지원하게 된 것이고.<br /> <br /> 즉, 닷넷 프레임워크는 꽤나 유연하게 디자인 되었던 것입니다. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
7534
(왼쪽의 숫자를 입력해야 합니다.)