private static DeclarationsProvider GetDeclarationsProvider(Target target)
        {
            var method = target.Method;

            if (method.ReturnType == typeof(Task))
            {
                return new TaskDeclarationsProvider(target);
            }
            if (method.ReturnType.GetTypeInfo().IsGenericType 
                && method.ReturnType.GetTypeInfo().GetGenericTypeDefinition() == typeof(Task<>))
            {
                return new GenericTaskDeclarationsProvider(target, typeof(TaskMethodInvoker<>));
            }
            if (method.ReturnType.GetTypeInfo().IsGenericType 
                && method.ReturnType.GetTypeInfo().GetGenericTypeDefinition() == typeof(ValueTask<>))
            {
                return new GenericTaskDeclarationsProvider(target, typeof(ValueTaskMethodInvoker<>));
            }

            if (method.ReturnType == typeof(void))
            {
                var isUsingAsyncKeyword = method.HasAttribute<AsyncStateMachineAttribute>();
                if (isUsingAsyncKeyword)
                {
                    throw new NotSupportedException("async void is not supported by design");
                }

                return new VoidDeclarationsProvider(target);
            }
            return new NonVoidDeclarationsProvider(target);
        }
예제 #2
0
 private static Target CreateTarget(Type type, MethodInfo setupMethod, MethodInfo methodInfo, BenchmarkAttribute attr)
 {
     var target = new Target(type, methodInfo, setupMethod, attr.Description, baseline: attr.Baseline, operationsPerInvoke: attr.OperationsPerInvoke);
     AssertMethodHasCorrectSignature("Benchmark", methodInfo);
     AssertMethodIsAccessible("Benchmark", methodInfo);
     AssertMethodIsNotGeneric("Benchmark", methodInfo);
     return target;
 }
 public NonVoidDeclarationsProvider(Target target) : base(target) { }
 internal DeclarationsProvider(Target target)
 {
     Target = target;
 }
 public GenericTaskDeclarationsProvider(Target target, Type invoker) : base(target)
 {
     invokerFullName = invoker.GetTypeInfo().FullName.Split(GenericArgumentSign).First();
 }
 public TaskDeclarationsProvider(Target target) : base(target) { }
        private static string GetExtraAttributes(Target target)
        {
#if !CORE
            if (target.Method.GetCustomAttributes(false).OfType<System.STAThreadAttribute>().Any())
            {
                return "[System.STAThreadAttribute]";
            }
#endif

            return string.Empty;
        }