我怎样才能知道如果C#方法是异步/通过反射等待? [英] How can I tell if a C# method is async/await via reflection?
问题描述
例如
类Foo {公共异步任务栏(){等待Task.Delay(500); }}
如果我们反映在这个类和方法,我怎么能确定这是否是一个实际的异步/ await方法,而不是简单地,恰好返回任务的方法?
类Foo {公共任务栏(){返回Task.Delay(500); }}
在我的code的复印件,的MethodInfo
为异步
方法中包含在 CustomAttributes
财产的下列项目:
- 在
DebuggerStepThroughAttribute
- 在
AsyncStateMachineAttribute
,而的MethodInfo
对于正常方法中包含的没有的在其 CustomAttributes
属性的项目
这似乎是 AsyncStateMachineAttribute
should的可靠的可以在一个异步
方法,而不是在一个标准找到。
编辑:事实上,网页甚至有例子中的以下
如下面的例子显示,你可以决定一个方法是否标有异步(Visual Basic)或异步(C#参考)修饰符。在这个例子中,IsAsyncMethod执行以下步骤:
•使用Type.GetMethod获取的方法名的MethodInfo对象。
•使用的GetType运算符(Visual Basic)或typeof运算(C#参考)获得属性的Type对象。
•获取用于该方法的属性对象,并通过使用MethodInfo.GetCustomAttribute属性类型。如果GetCustomAttribute返回Nothing(Visual Basic)或空(C#),该方法不包含属性。
块引用>私有静态布尔IsAsyncMethod(类型classType所,字符串methodName中)
{
//获取具有指定名称的方法。
MethodInfo的方法= classType.GetMethod(方法名); 键入attType = typeof运算(AsyncStateMachineAttribute); //获取该方法的自定义属性。
//返回的值包含StateMachineType财产。
如果属性不是方法present返回//空。
VAR ATTRIB =(AsyncStateMachineAttribute)method.GetCustomAttribute(attType); 回报(ATTRIB!= NULL);
}e.g.
class Foo { public async Task Bar() { await Task.Delay(500); } }
If we are reflecting over this class and method, how can I determine if this is an actual async/await method rather than simply a method that happens to return a Task?
class Foo { public Task Bar() { return Task.Delay(500); } }
解决方案In my copy of your code, the
MethodInfo
for theasync
method contains the following items in theCustomAttributes
property:
- a
DebuggerStepThroughAttribute
- a
AsyncStateMachineAttribute
whereas the
MethodInfo
for the normal method contains no items in itsCustomAttributes
property.It seems like the
AsyncStateMachineAttribute
should reliably be found on anasync
method and not on a standard one.Edit: In fact, that page even has the following in the examples!
As the following example shows, you can determine whether a method is marked with Async (Visual Basic) or async (C# Reference) modifier. In the example, IsAsyncMethod performs the following steps:
• Obtains a MethodInfo object for the method name by using Type.GetMethod.
• Obtains a Type object for the attribute by using GetType Operator (Visual Basic) or typeof (C# Reference).
• Obtains an attribute object for the method and attribute type by using MethodInfo.GetCustomAttribute. If GetCustomAttribute returns Nothing (Visual Basic) or null (C#), the method doesn't contain the attribute.
private static bool IsAsyncMethod(Type classType, string methodName) { // Obtain the method with the specified name. MethodInfo method = classType.GetMethod(methodName); Type attType = typeof(AsyncStateMachineAttribute); // Obtain the custom attribute for the method. // The value returned contains the StateMachineType property. // Null is returned if the attribute isn't present for the method. var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(attType); return (attrib != null); }
这篇关于我怎样才能知道如果C#方法是异步/通过反射等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!