Exemplo n.º 1
0
 private Func <DisplayContext, Task <IHtmlContent> > CreateDelegate(
     ShapeAttributeOccurrence attributeOccurrence,
     ShapeDescriptor descriptor)
 {
     return(context =>
     {
         var serviceInstance = context.ServiceProvider.GetService(attributeOccurrence.ServiceType);
         // oversimplification for the sake of evolving
         return PerformInvokeAsync(context, attributeOccurrence.MethodInfo, serviceInstance);
     });
 }
Exemplo n.º 2
0
        private Func <DisplayContext, Task <IHtmlContent> > CreateDelegate(ShapeAttributeOccurrence attributeOccurrence)
        {
            var type       = attributeOccurrence.ServiceType;
            var methodInfo = attributeOccurrence.MethodInfo;
            var parameters = methodInfo.GetParameters();

            // Create an array of lambdas that will generate the value of each parameter. This prevents from having to test each name and type on every invocation.
            var argumentBuilders = parameters.Select(parameter => BindParameter(parameter)).ToArray();

            var methodWrapper = CreateMethodWrapper(type, methodInfo);

            Func <object, DisplayContext, Task <IHtmlContent> > action;

            if (methodInfo.ReturnType == typeof(Task <IHtmlContent>))
            {
                action = (s, d) =>
                {
                    var arguments = new object[argumentBuilders.Length];
                    for (var i = 0; i < arguments.Length; i++)
                    {
                        arguments[i] = argumentBuilders[i](d);
                    }

                    return((Task <IHtmlContent>)methodWrapper(s, arguments));
                };
            }
            else if (attributeOccurrence.MethodInfo.ReturnType == typeof(IHtmlContent))
            {
                action = (s, d) =>
                {
                    var arguments = new object[argumentBuilders.Length];
                    for (var i = 0; i < arguments.Length; i++)
                    {
                        arguments[i] = argumentBuilders[i](d);
                    }

                    return(Task.FromResult((IHtmlContent)methodWrapper(s, arguments)));
                };
            }
            else if (attributeOccurrence.MethodInfo.ReturnType != typeof(void))
            {
                action = (s, d) =>
                {
                    var arguments = new object[argumentBuilders.Length];
                    for (var i = 0; i < arguments.Length; i++)
                    {
                        arguments[i] = argumentBuilders[i](d);
                    }

                    return(Task.FromResult(CoerceHtmlContent(methodWrapper(s, arguments))));
                };
            }
            else
            {
                action = (s, d) => null;
            }

            return(context =>
            {
                var serviceInstance = context.ServiceProvider.GetService(attributeOccurrence.ServiceType);
                return action(serviceInstance, context);
            });
        }