示例#1
0
        public IFunctionInvoker Create(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            Type?reflectedType = method.ReflectedType;

            if (reflectedType == null)
            {
                throw new InvalidOperationException("No reflected type was found.");
            }

            MethodInfo genericMethodDefinition = typeof(DefaultFunctionInvokerFactory).GetMethod(nameof(DefaultFunctionInvokerFactory.CreateGeneric), BindingFlags.NonPublic | BindingFlags.Static) !;

            if (!TryGetReturnType(method, out Type? returnType))
            {
                returnType = typeof(object);
            }

            MethodInfo genericMethod = genericMethodDefinition.MakeGenericMethod(reflectedType, returnType !);


            IFunctionActivator activator = _functionActivator;

            if (method.IsStatic)
            {
                activator = NullFunctionActivator.Instance;
            }

            var lambda = (Func <MethodInfo, IMethodInvokerFactory, IFunctionActivator, IFunctionInvoker>)Delegate.CreateDelegate(typeof(Func <MethodInfo, IMethodInvokerFactory, IFunctionActivator, IFunctionInvoker>), genericMethod);

            return(lambda.Invoke(method, _methodInvokerFactory, activator));
        }
示例#2
0
        public IFunctionInvoker Create(FunctionDefinition definition)
        {
            var method = _methodLocator.GetMethod(definition.PathToAssembly, definition.EntryPoint);

            Type?reflectedType = method.ReflectedType;

            if (reflectedType == null)
            {
                throw new InvalidOperationException($"No reflected type '{method.ReflectedType?.Name}' was found.");
            }

            MethodInfo genericMethodDefinition = typeof(DefaultFunctionInvokerFactory).GetMethod(nameof(DefaultFunctionInvokerFactory.CreateGeneric), BindingFlags.NonPublic | BindingFlags.Static) !;

            if (!TryGetReturnType(method, out Type? returnType))
            {
                returnType = typeof(object);
            }

            MethodInfo genericMethod = genericMethodDefinition.MakeGenericMethod(reflectedType, returnType !);

            IFunctionActivator activator = _functionActivator;

            if (method.IsStatic)
            {
                activator = NullFunctionActivator.Instance;
            }

            var lambda = (Func <MethodInfo, IMethodInvokerFactory, IFunctionActivator, IFunctionInvoker>)Delegate.CreateDelegate(typeof(Func <MethodInfo, IMethodInvokerFactory, IFunctionActivator, IFunctionInvoker>), genericMethod);

            return(lambda.Invoke(method, _methodInvokerFactory, activator));
        }
        /// <summary>
        /// Creates an instance of the specified generic type argument, <typeparamref name="T"/>, to be used as the target of the invocation.
        /// </summary>
        /// <typeparam name="T">The type of the instance to create.</typeparam>
        /// <param name="activator">The <see cref="IFunctionActivator"/> instance to use when creating the instance.</param>
        /// <param name="context">The <see cref="FunctionContext"/> for the invocation triggering the instance creation.</param>
        /// <returns>The created instance.</returns>
        public static T?CreateInstance <T>(this IFunctionActivator activator, FunctionContext context)
            where T : class
        {
            if (activator is null)
            {
                throw new ArgumentNullException(nameof(activator));
            }

            return(activator.CreateInstance(typeof(T), context) as T);
        }
        public void Create_ReturnsNull()
        {
            // Arrange
            IFunctionActivator product = NullFunctionActivator.Instance;

            // Act
            object instance = product.CreateInstance(typeof(object), new TestFunctionContext());

            // Assert
            Assert.Null(instance);
        }
示例#5
0
        public void Create_ReturnsNull()
        {
            // Arrange
            IFunctionActivator product = NullFunctionActivator.Instance;

            // Act
            object instance = product.CreateInstance <object>(new ServiceCollection().BuildServiceProvider());

            // Assert
            Assert.Null(instance);
        }
        public void Create_IfMethodIsNull_Throws()
        {
            // Arrange
            MethodInfo            method = null;
            IMethodInvokerFactory methodInvokerFactory = CreateMethodInvokerFactory();
            IFunctionActivator    activator            = CreateDummyActivator();

            IFunctionInvokerFactory _invokerFactory = new DefaultFunctionInvokerFactory(methodInvokerFactory, activator);

            // Act & Assert
            var ex = Assert.Throws <ArgumentNullException>(() => _invokerFactory.Create(method));

            Assert.Equal("method", ex.ParamName);
        }
        public void Create_ReturnsFunctionInvoker()
        {
            // Arrange
            MethodInfo method = GetMethodInfo(nameof(DefaultFunctionInvokerFactoryTests.StaticReturnVoid));

            IMethodInvokerFactory methodInvokerFactory = CreateMethodInvokerFactory();
            IFunctionActivator    activator            = CreateDummyActivator();

            IFunctionInvokerFactory _invokerFactory = new DefaultFunctionInvokerFactory(methodInvokerFactory, activator);

            // Act
            IFunctionInvoker invoker = _invokerFactory.Create(method);

            // Assert
            Assert.IsType <DefaultFunctionInvoker <DefaultFunctionInvokerFactoryTests, object> >(invoker);
        }
示例#8
0
 public DefaultFunctionDefinitionFactory(IFunctionInvokerFactory functionInvokerFactory, IFunctionActivator functionActivator)
 {
     _functionInvokerFactory = functionInvokerFactory ?? throw new ArgumentNullException(nameof(functionInvokerFactory));
     _functionActivator      = functionActivator ?? throw new ArgumentNullException(nameof(functionActivator));
 }
示例#9
0
        private static IFunctionInvoker CreateGeneric <TReflected, TReturnValue>(MethodInfo method, IMethodInvokerFactory methodInvokerFactory, IFunctionActivator functionActivator)
        {
            List <string?> parameterNames = method.GetParameters().Select(p => p.Name).ToList();

            IMethodInvoker <TReflected, TReturnValue> methodInvoker = methodInvokerFactory.Create <TReflected, TReturnValue>(method);

            return(new DefaultFunctionInvoker <TReflected, TReturnValue>(methodInvoker, functionActivator));
        }
示例#10
0
 public DefaultFunctionInvokerFactory(IMethodInvokerFactory methodInvokerFactory, IFunctionActivator functionActivator, IMethodInfoLocator methodLocator)
 {
     _methodInvokerFactory = methodInvokerFactory ?? throw new ArgumentNullException(nameof(methodInvokerFactory));
     _functionActivator    = functionActivator ?? throw new ArgumentNullException(nameof(functionActivator));
     _methodLocator        = methodLocator ?? throw new ArgumentNullException(nameof(methodLocator));
 }
 public DefaultFunctionInvoker(IMethodInvoker <TInstance, TReturn> methodInvoker, IFunctionActivator functionActivator)
 {
     _methodInvoker    = methodInvoker ?? throw new ArgumentNullException(nameof(methodInvoker));
     FunctionActivator = functionActivator ?? throw new ArgumentNullException(nameof(functionActivator));
 }