성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
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# - CS8344 컴파일 에러: ref struct 타입의 사용 제한 메서드</h1> <p> 스택에만 생성 가능한 ref struct 구조체는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 7.2 - 스택에만 생성할 수 있는 값 타입 지원 - "ref struct" ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11530'>https://www.sysnet.pe.kr/2/0/11530</a> </pre> <br /> GC Heap에 위치하는 참조 타입의 필드로 선언될 수 없다는 제약이 있는데요. 이로 인해 다음과 같은 식으로 yield return을 포함한 메서드나, async 메서드 내에서는 사용할 수 없습니다.<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; using System.Collections.Generic; class Program { static void Main(string[] args) { Memory<byte> buffer = new Memory<byte>(); EnumerateElems(buffer); } private static <span style='color: blue; font-weight: bold'>IEnumerable<byte></span> EnumerateElems(Memory<byte> buffer) { // Error CS8344 foreach statement cannot operate on enumerators of type 'Span<byte>.Enumerator' in async or iterator methods because 'Span<byte>.Enumerator' is a ref struct. foreach (byte item in buffer.Span) { <span style='color: blue; font-weight: bold'>yield return</span> item; } } } </pre> <br /> 왜냐하면, C# 컴파일러는 위와 같은 특별한 메서드를 다음과 같이,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [CompilerGenerated] private sealed class <EnumerateElems>d__2 : IEnumerable<object>, IEnumerable, IEnumerator<object>, IDisposable, IEnumerator { // Fields private int <>1__state; private object <>2__current; <span style='color: blue; font-weight: bold'>private Span<byte> <buffer_span>3__1;</span> ...[생략]... // Properties object IEnumerator<object>.Current { [DebuggerHidden] get; } object IEnumerator.Current { [DebuggerHidden] get; } } </pre> <br /> 사용자 코드에서 사용하던 buffer.Span 변수를 필드에 담는 클래스로 재구성하기 때문에 저 구문이 허용되지 않아 컴파일 오류가 발생하는 것입니다. 마찬가지의 이유로 async 메서드에서도,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# 컴파일러 대신 직접 구현하는 비동기(async/await) 코드 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11351#gen_src'>https://www.sysnet.pe.kr/2/0/11351#gen_src</a> </pre> <br /> IAsyncStateMachine을 상속받아 자동 생성하는 코드에서 필드로 정의해 놓기 때문에 사용하지 못합니다. 그러고 보니, 예전에 이와 유사한 문제로 허용되지 않는 구문이 또 있었지요. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# async 메서드에서 out/ref/in 유형의 인자를 사용하지 못하는 이유 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/11850'>https://www.sysnet.pe.kr/2/0/11850</a> </pre> <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;' > using System; using System.Collections.Generic; class Program { static void Main(string[] args) { Memory<byte> buffer = new Memory<byte>(); EnumerateElems(buffer); } private static <span style='color: blue; font-weight: bold'>IEnumerable<byte></span> EnumerateElems(Memory<byte> buffer) { /* foreach (byte item in buffer.Span) { yield return item; } */ int length = <span style='color: blue; font-weight: bold'>GetLength(buffer)</span>; for (int i = 0; i < length; i ++) { <span style='color: blue; font-weight: bold'>yield return</span> <span style='color: blue; font-weight: bold'>GetItem(buffer, i);</span> } } private static int GetLength(Memory<byte> buffer) { return buffer.Length; } private static byte GetItem(Memory<byte> buffer, int idx) { return buffer.Span[idx]; // <a target='tab' href='https://www.sysnet.pe.kr/2/0/12475'>성능 손실</a> } } </pre> <br /> 위와 같이 buffer 변수 수준에서 자동 생성 코드 내에 들어가면 컴파일 오류가 없어지는데요, 왜냐하면 <a target='tab' href='https://www.sysnet.pe.kr/2/0/12475'>Memory<T></a> 타입은 일반적인 struct이기 때문에 자동 생성 코드의 필드로 넣는 것이 가능하기 때문입니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1701&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2780
(왼쪽의 숫자를 입력해야 합니다.)