我使用表达式来标识类中的特定方法,并返回该方法的属性.当方法是异步的时,编译器会给我一个警告,即应该等待该方法.
有没有其他方法可以识别方法,或任何方式来抑制警告,而不使用pragma?我不想使用字符串来识别方法.
Resharper建议使用async / await,但异步lambda表达式不能转换为表达式树.
示例代码:
class Program { static void Main(string[] args) { var attributeProvider = new AttributeProvider(); var attributeText = attributeProvider.GetAttribute<Program>( p => p.MethodA()); //Warning: Because this call is not awaited,... } [Text("text")] public async Task<string> MethodA() { return await Task.Fromresult(""); } } public class AttributeProvider { public string GetAttribute<T>(Expression<Action<T>> method) { var expr =(MethodCallExpression) method.Body; var attribute = (TextAttribute)Attribute.GetCustomAttribute( expr.Method,typeof(TextAttribute)); return attribute.Text; } } public class TextAttribute : Attribute { public string Text { get; set; } public TextAttribute(string text) { Text = text; } }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。