Ejemplo n.º 1
0
        /// <summary>
        /// Invokes the static method.
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="methodParameters">The method parameters.</param>
        /// <returns>RemoteInvokeResult.</returns>
        /// <exception cref="InvalidObjectException"></exception>
        public SandboxMarshalInvokeResult InvokeStaticMethod(string typeName, string methodName, params object[] methodParameters)
        {
            SandboxMarshalInvokeResult result = new SandboxMarshalInvokeResult();

            try
            {
                typeName.CheckEmptyString(nameof(typeName));
                methodName.CheckEmptyString(nameof(methodName));

                var type = ReflectionExtension.SmartGetType(typeName);
                type.CheckNullObject(nameof(type));

                var methodInfo = type.GetMethod(methodName);
                methodInfo.CheckNullObject(nameof(methodInfo));

                if (!methodInfo.IsStatic)
                {
                    throw new InvalidObjectException(nameof(methodName), data: methodName, reason: "Method is not static");
                }

                result.SetValue(methodInfo.Invoke(null, methodParameters));
            }
            catch (Exception ex)
            {
                result.SetException(ex.Handle(new { typeName, methodName, methodParameters }));
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the instance and invoke method.
        /// </summary>
        /// <param name="typeFullName">Name of the type.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="methodParameters">The method parameters.</param>
        /// <returns>RemoteInvokeResult.</returns>
        /// <exception cref="InvalidObjectException"></exception>
        public SandboxMarshalInvokeResult CreateInstanceAndInvokeMethod(string typeFullName, object[] constructorParameters, string methodName, params object[] methodParameters)
        {
            SandboxMarshalInvokeResult result = new SandboxMarshalInvokeResult();

            try
            {
                typeFullName.CheckEmptyString(nameof(typeFullName));
                methodName.CheckEmptyString(nameof(methodName));

                var type = ReflectionExtension.SmartGetType(typeFullName);
                type.CheckNullObject(nameof(type));

                var instance = Activator.CreateInstance(type, (object[])constructorParameters);

                var methodInfo = type.GetMethod(methodName);
                methodInfo.CheckNullObject(nameof(methodInfo));

                if (methodInfo.IsStatic)
                {
                    throw new InvalidObjectException(nameof(methodName), data: methodName, reason: "Method is static");
                }

                result.SetValue(methodInfo.Invoke(instance, methodParameters));
            }
            catch (Exception ex)
            {
                result.SetException(ex.Handle(new { typeFullName, constructorParameters, methodName, methodParameters }));
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the API contract interfaces.
        /// </summary>
        /// <returns>RemoteInvokeResult.</returns>
        public SandboxMarshalInvokeResult GetApiContractInterfaces()
        {
            SandboxMarshalInvokeResult result = new SandboxMarshalInvokeResult();

            try
            {
                Dictionary <string, string> interfaces = new Dictionary <string, string>();

                foreach (var one in EnvironmentCore.DescendingAssemblyDependencyChain)
                {
                    foreach (var item in one.GetTypes())
                    {
                        if (item.IsInterface && item.HasAttribute <ApiContractAttribute>(false) && !item.IsGenericTypeDefinition)
                        {
                            interfaces.Merge(item.GetFullName(), item.Name, false);
                        }
                    }
                }

                result.SetValue(interfaces);
            }
            catch (Exception ex)
            {
                result.SetException(ex.Handle());
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates the instance and invoke method.
        /// NOTE: items in parameters should all inherited from <see cref="MarshalByRefObject" /> and set <see cref="SerializableAttribute" /> in class for proxy transfer.
        /// </summary>
        /// <param name="assemblyName">Name of the assembly.</param>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="method">The method.</param>
        /// <param name="methodParameters">The parameters.</param>
        /// <returns>RemoteInvokeResult.</returns>
        public SandboxMarshalInvokeResult CreateInstanceAndInvokeMethod(string assemblyName, string typeName, string method, params object[] methodParameters)
        {
            SandboxMarshalInvokeResult result = new SandboxMarshalInvokeResult();

            try
            {
                assemblyName.CheckEmptyString(nameof(assemblyName));
                method.CheckEmptyString(nameof(method));

                TempAssembly assembly;
                if (tempAssemblies.TryGetValue(assemblyName, out assembly))
                {
                    var objectToRun = assembly.CreateInstance(typeName);
                    objectToRun.CheckNullObject(nameof(objectToRun));

                    var methodInfo = objectToRun.GetType().GetMethod(method);
                    methodInfo.CheckNullObject(nameof(methodInfo));

                    result.SetValue(methodInfo.Invoke(objectToRun, methodParameters));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                result.SetException(ex.Handle(new { typeName, method }));
            }

            return(result);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SandboxInvokeResult"/> class.
 /// </summary>
 /// <param name="result">The result.</param>
 internal SandboxInvokeResult(SandboxMarshalInvokeResult result)
 {
     if (result != null)
     {
         Type          = result.Type;
         ExceptionInfo = result.ExceptionInfo;
         Value         = result.Value;
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Wraps to current application domain.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <returns>Beyova.SandboxInvokeResult.</returns>
 public static SandboxInvokeResult WrapToCurrentAppDomain(SandboxMarshalInvokeResult result)
 {
     return(result == null ? null : new SandboxInvokeResult(result));
 }