성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] 그냥 RSS Reader 기능과 약간의 UI 편의성 때문에 사용...
[이종효] 오래된 소프트웨어는 보안 위협이 되기도 합니다. 혹시 어떤 기능...
[정성태] @Keystroke IEEE의 문서를 소개해 주시다니... +_...
[손민수 (Keystroke)] 괜히 듀얼채널 구성할 때 한번에 같은 제품 사라고 하는 것이 아...
[정성태] 전각(Full-width)/반각(Half-width) 기능을 토...
[정성태] Vector에 대한 내용은 없습니다. Vector가 닷넷 BCL...
[orion] 글 읽고 찾아보니 디자인 타임에는 InitializeCompon...
[orion] 연휴 전에 재현 프로젝트 올리자 생각해 놓고 여의치 않아서 못 ...
[정성태] 아래의 글에 정리했으니 참고하세요. C# - Typed D...
[정성태] 간단한 재현 프로젝트라도 있을까요? 저런 식으로 설명만 해...
글쓰기
제목
이름
암호
전자우편
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# 10 - (9) 비동기 메서드가 사용할 AsyncMethodBuilder 선택 가능</h1> <p> C# 7.0에서 비동기 메서드의 반환 타입으로 사용자 정의 Task 타입을 사용하는 것이 가능했습니다. (<a target='tab' href='http://www.yes24.com/Product/Goods/97314203'>책의 경우 757페이지 12.7 참고</a>)<br /> <br /> 그래서 사용자 정의 Task 타입을 구현한 경우 이것을 C# 컴파일러가 어떻게 다뤄야 할지를 AsyncMethodBuilder 특성을 이용해 알려줬습니다. 가령, ValueTask 타입은 다음과 같이 정의했기 때문에,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <span style='color: blue; font-weight: bold'>[AsyncMethodBuilder(typeof(AsyncValueTaskMethodBuilder))]</span> public readonly struct <span style='color: blue; font-weight: bold'>ValueTask</span> : IEquatable<ValueTask> { // ...[생략]... } </pre> <br /> C# 컴파일러가 ValueTask를 반환하는 비동기 메서드에 대해 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.asyncvaluetaskmethodbuilder-1'>AsyncValueTaskMethodBuilder</a>를 기반으로 상태 머신 코드를 작성하는 것이 가능했습니다. 따라서 지금까지는 비동기 메서드의 반환 타입에 따라,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Task 반환: AsyncTaskMethodBuilder 타입 사용 ValueTask 반환: AsyncValueTaskMethodBuilder 타입 사용 </pre> <br /> 2가지의 AsyncMethodBuilder가 선택되었는데, 이것을 달리 말하면 다음과 같은 제약을 가지고 있는 것과 같습니다.<br /> <br /> <ul> <li>새로운 AsyncMethodBuilder를 사용하려면 반드시 사용자 정의 Task를 정의</li> <li>기존의 Task, ValueTask에 대한 신규 AsyncMethodBuilder 적용 불가능</li> </ul> <br /> 이러한 제약을, 이번 C# 10부터 비동기 메서드 레벨에서 AsyncMethodBuilder를 선택할 수 있도록 개선을 했습니다. 이로 인해 C# 10이 적용되는 .NET 6의 BCL에 포함된 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.asyncmethodbuilderattribute?view=net-6.0'>AsyncMethodBuilderAttribute</a>에는 새롭게 AttributeTargets.Method 옵션이 추가되었고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | <span style='color: blue; font-weight: bold'>AttributeTargets.Method</span> | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false, AllowMultiple = false)] public sealed class AsyncMethodBuilderAttribute : Attribute { // ...[생략]... } </pre> <br /> 따라서 비동기 메서드 수준에서 Task를 다룰 수 있는 타입을 C# 컴파일러에게 다음과 같이 알려줄 수 있게 되었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > // C# 9 또는 .NET 5 이하 컴파일 오류 - Attribute 'AsyncMethodBuilder' is not valid on this declaration type. It is only valid on 'class, struct, enum, interface, delegate' declarations. <span style='color: blue; font-weight: bold'>[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]</span> public static async ValueTask<int> GetSalaryAsync() { await Task.Delay(0); return 60; } </pre> <br /> 즉, (기존에는 무조건 AsyncValueTaskMethodBuilder를 사용했던) ValueTask를 반환하는 비동기 메서드를 C# 컴파일러로 하여금 PoolingAsyncValueTaskMethodBuilder를 사용하도록 알려 줄 수 있고, 새로운 C# 10 컴파일러는 이렇게 지정된 특성 값을 인식해 비동기 메서드에 대한 코드 생성 시 다음과 같이 반영할 수 있게 되었습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [AsyncStateMachine(typeof(<GetSalaryAsync>d__29))] [CompilerGenerated] [AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))] // retained but not necessary anymore static ValueTask<int> GetSalaryAsync() { <GetSalaryAsync>d__29 stateMachine; <span style='color: blue; font-weight: bold'>stateMachine.<>t__builder = PoolingAsyncValueTaskMethodBuilder<int>.Create();</span> stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } </pre> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1853&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그런데, 현실적인 기준으로 봤을 때 ^^ 예전 글에서도 다뤘지만,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - await을 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11456'>https://www.sysnet.pe.kr/2/0/11456</a> C# - async를 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11484'>https://www.sysnet.pe.kr/2/0/11484</a> </pre> <br /> 사용자 정의 AsyncMethodBuilder를 특수하게 재정의해야 할 요구 사항이 많지도 않을뿐더러 그것의 구현 방법 또한 그다지 쉽지도 않습니다. 실제로 Task와 ValueTask 말고 또 다른 Task를 정의해서 사용하는 국내 및 해외 개발자가 몇 명이나 될지 ^^ 궁금하군요. 마찬가지로 이번에도 역시 마이크로소프트 스스로 성능 향상을 위한 방법의 일환으로 이런 확장을 시도한 것이 아닌가 생각됩니다. <br /> <br /> 실제로 예제에 나온 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.poolingasyncvaluetaskmethodbuilder-1'>PoolingAsyncValueTaskMethodBuilder</a>는 .NET 6의 BCL부터 포함된 타입이고, 어쩌면 이후로도 AsyncMethodBuilder 특성을 가장 잘 사용하는 곳은 BCL 내의 코드들일 것입니다. ^^<br /> <br /> <hr style='width: 50%' /><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 10 - (1) 구조체를 생성하는 record struct (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/record-structs'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4334'>Static Abstract Members In Interfaces C# 10 Preview)</a> ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12790'>https://www.sysnet.pe.kr/2/0/12790</a> C# 10 - (2) 전역 네임스페이스 선언 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/globalusingdirective'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/3428'>Global Using Directive</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12792'>https://www.sysnet.pe.kr/2/0/12792</a> C# 10 - (3) 개선된 변수 초기화 판정 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/improved-definite-assignment'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4465'>Improved Definite Assignment</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12793'>https://www.sysnet.pe.kr/2/0/12793</a> C# 10 - (4) 상수 문자열에 포맷 식 사용 가능 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/improved-interpolated-strings'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/2951'>Constant Interpolated Strings</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12796'>https://www.sysnet.pe.kr/2/0/12796</a> C# 10 - (5) 속성 패턴의 개선 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/extended-property-patterns'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4394'>Extended property patterns</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12799'>https://www.sysnet.pe.kr/2/0/12799</a> C# 10 - (6) record class 타입의 ToString 메서드를 sealed 처리 허용 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4174'>Sealed record ToString</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12801'>https://www.sysnet.pe.kr/2/0/12801</a> C# 10 - (7) Source Generator V2 APIs (<a target='tab' href='https://github.com/dotnet/roslyn/issues/51257'>Source Generator V2 APIs</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12804'>https://www.sysnet.pe.kr/2/0/12804</a> C# 10 - (8) 분해 구문에서 기존 변수의 재사용 가능 (공식 문서, <a target='tab' href='https://github.com/dotnet/csharplang/issues/125'>Mix declarations and variables in deconstruction</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12805'>https://www.sysnet.pe.kr/2/0/12805</a> C# 10 - (9) 비동기 메서드가 사용할 AsyncMethodBuilder 선택 가능 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/async-method-builders'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/1407'>Async method builder override</a>); ; https://www.sysnet.pe.kr/2/0/12807 C# 10 - (10) 개선된 #line 지시자 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/enhanced-line-directives'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4747'>Enhanced #line directive</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12812'>https://www.sysnet.pe.kr/2/0/12812</a> C# 10 - (11) Lambda 개선 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/lambda-improvements'>공식 문서 1</a>, <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/lambda-attributes'>공식 문서 2</a>, <a target='tab' href='https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/lambda-improvements.md'>Lambda improvements</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12813'>https://www.sysnet.pe.kr/2/0/12813</a> C# 10 - (12) 문자열 보간 성능 개선 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/improved-interpolated-strings'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/4487'>Interpolated string improvements</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12826'>https://www.sysnet.pe.kr/2/0/12826</a> C# 10 - (13) 단일 파일 내에 적용되는 namespace 선언 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/137'>File-scoped namespace</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12828'>https://www.sysnet.pe.kr/2/0/12828</a> C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/parameterless-struct-constructors'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/99'>Parameterless struct constructors</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12829'>https://www.sysnet.pe.kr/2/0/12829</a> C# 10 - (15) CallerArgumentExpression 특성 추가 (<a target='tab' href='https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/caller-argument-expression'>공식 문서</a>, <a target='tab' href='https://github.com/dotnet/csharplang/issues/287'>Caller expression attribute</a>) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12835'>https://www.sysnet.pe.kr/2/0/12835</a> Language Feature Status ; <a target='tab' href='https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md'>https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1621
(왼쪽의 숫자를 입력해야 합니다.)