示例#1
0
        private void Handle <T, A>(List <IFact <T> > facts, Type type, MethodInfo method,
                                   Func <A, object, IFact <T> > withContext,
                                   Func <A, object, IFact <T> > withoutContext,
                                   Func <A, object, IFact <T> > asyncWithContext,
                                   Func <A, object, IFact <T> > asyncWithoutContext)
            where A : Attribute
        {
            var innerParameterType   = typeof(T);
            var contextParameterType = typeof(Context <T>);
            var label = _labelMaker.MakeLabel(method);

            foreach (A attribute in method.GetCustomAttributes(typeof(A), false))
            {
                var instance      = type.GetConstructor(new Type[0]).Invoke(new object[0]);
                var parameterType = method.GetParameters().First().ParameterType;

                if (method.ReturnType == typeof(Task))
                {
                    if (parameterType == contextParameterType)
                    {
                        var fact = asyncWithContext(attribute, instance);
                        fact.Label = label;
                        facts.Add(fact);
                    }
                    else if (parameterType == innerParameterType)
                    {
                        var fact = asyncWithoutContext(attribute, instance);
                        fact.Label = label;
                        facts.Add(fact);
                    }
                }
                else
                {
                    if (parameterType == contextParameterType)
                    {
                        var fact = withContext(attribute, instance);
                        fact.Label = label;
                        facts.Add(fact);
                    }
                    else if (parameterType == innerParameterType)
                    {
                        var fact = withoutContext(attribute, instance);
                        fact.Label = label;
                        facts.Add(fact);
                    }
                }
            }
        }