它不适用于使用较早版本的编译器创建的库,因此您将不能在那里依赖它.它不适用于以.NET之前的版本为4.5的库. (说实话,我不确定VB编译器在做什么.它可能会忽略该属性,或者可能要求您将.NET定位为最新版本才能使用迭代器方法.)我想说,方法上存在IteratorStateMachineAttribute可以很好地表明它是迭代器方法(尽管没有什么可以阻止顽皮的开发人员将其应用于其他方法的),但是由于C#编译器的版本较旧,这还不够充分.I did a Go To Definition () on a class I was trying to derive from and I noticed that one of the methods was marked with AsyncStateMachineAttribute. Which in turn inherits StateMachineAttribute. I was curious and decide to read up on this attribute and all its derivates on MSDN. That led me to this and I came across this statement:Because that statement is made to stand out, there must be serious implications to it but there is no further explanation as to why that is so. Does anyone have insights in this regard? 解决方案 I'm 99% sure it's historical. Basically, C# introduced iterator blocks in C# 2 - a long time before this attribute was introduced.The equivalent async attribute was introduced at the same time as async methods in C#, so that was fine... but even though the C# compiler now applies IteratorStateMachineAttribute to iterator blocks:It doesn't apply to libraries created with older versions of the compiler, so you wouldn't be able to rely on it there.It can't apply to libraries targeting versions of .NET prior to 4.5. (I'm not sure what the VB compiler does here, to be honest. It may omit the attribute, or it may require you to be targeting a recent version of .NET in order to use iterator methods.)I would say that the presence of an IteratorStateMachineAttribute on a method is a good indicator that it is an iterator method (although there's nothing to stop a mischievous developer applying it to other methods), but it's not a sufficient test due to older versions of the C# compiler. 这篇关于为什么我们不能在C#中使用IteratorStateMachineAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 08:19
查看更多