英文:
How to provide fix for all parameters of the method (Roslyn - FixProvider)
问题
我为添加验证以确保参数不为空的代码分析器和修复提供程序编写了代码。它适用于多个方法,但我不能一次检查所有参数。分析器将标记所有尚未检查的参数,但修复只能一次完成一个参数。
详细信息:
这不是不可变节点问题,如果需要,该代码会添加使用语句。
在分析器中,我为每个参数注册了诊断。
Diagnostic diagnostic = Diagnostic.Create(
Rule,
parameter.GetLocation(),
null,
prop.ToImmutableDictionary(),
parameter.Identifier.Text);
在修复提供程序中,我从上下文中获取第一个诊断并为其注册代码修复。
也许我做错了什么,因此在修复提供程序中无法获得多个诊断。
正确的方法是如何进行多个修复?诊断的键/ID应该相同吗?还是应该直接注册诊断以针对所有参数提出投诉。
英文:
I wrote code analyzer & fix provider for adding validations to ensure argument is not null. It works good for multiple methods, but I cannot check all the parameters at once. The analyzer will mark all the parameters that are not checked already, but fix can be done only for one parameter at once.
Details:
It is not immutable nodes issue, the code adds using statement if needed.
In the analyzer i register diagnostic for every parameter.
Diagnostic diagnostic = Diagnostic.Create(
Rule,
parameter.GetLocation(),
null,
prop.ToImmutableDictionary(),
parameter.Identifier.Text);
In the fix provider I am taking first diagnostic from the context and register code fix for that.
Maybe I am doing something wrong and thus do not get multiple diagnostics in the fix provider.
What is the correct way to make multiple fixes? Should be the key/id of the diagnostic the same? Or should the diagnostic be registered directly to complain about all the parameters.
答案1
得分: 1
我已经找到了一个希望是好的解决方案:
分析器报告每个无效参数的诊断信息,因此您可以为特定参数生成验证。然后,分析器报告所有无效参数的一个附加诊断,并将方法标识符设置为位置。
相同的代码修复提供程序用于解决这两种类型的诊断。一个是特定参数的诊断(发送单个参数数据),另一个是所有参数的诊断(发送参数数据的集合)。
英文:
I have found hopefully good solution:
The analyzer report diagnostics for every invalid parameter so you can generate validation for specific parameter. After that the analyzer reports one more diagnostic for all the invalid parameters and sets the method identifier as the location.
The same code fix provider is used to solve both types of diagnostics. The specific parameter one (which sends the single parameter data) and all the parameters one (which sends collection of the parameters data).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论