예제 #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);
        }
예제 #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);
        }
예제 #3
0
        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns>System.Object.</returns>
        public object CreateInstance(string typeName, params object[] parameters)
        {
            typeName.CheckEmptyString(nameof(typeName));

            if (!typeName.StartsWith(Namespace))
            {
                typeName = string.Format("{0}.{1}", Namespace, typeName);
            }

            return(Activator.CreateInstance(ReflectionExtension.SmartGetType(typeName, false), parameters));
        }
        /// <summary>
        /// Gets the object.
        /// </summary>
        /// <returns>System.Object.</returns>
        public object GetObject()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Value) || string.IsNullOrWhiteSpace(Type))
                {
                    return(null);
                }

                Type type = ReflectionExtension.SmartGetType(Type, true);
                return(JToken.Parse(Value).ToObject(type));
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { Value, Type });
            }
        }
예제 #5
0
        /// <summary>
        /// Initializes the specified options.
        /// </summary>
        /// <param name="options">The options.</param>
        private void Initialize(DynamicStaticMethodOptions options)
        {
            try
            {
                options.CheckNullObject(nameof(options));
                options.DynamicCode.CheckEmptyString(nameof(options.DynamicCode));
                Type type = null;

                if (!string.IsNullOrWhiteSpace(options.ClassName) && !string.IsNullOrWhiteSpace(options.MethodName))
                {
                    if (options.ClassName == options.MethodName)
                    {
                        throw new InvalidObjectException(nameof(options), data: new { options }, reason: "Conflict name for compile closure.");
                    }

                    type = ReflectionExtension.SmartGetType(string.Format("{0}.{1}", _namespace, options.ClassName));
                    if (type != null)
                    {
                        _methodInfo = type.GetMethod(options.MethodName);

                        if (_methodInfo != null && _methodInfo.IsStatic)
                        {
                            return;
                        }
                    }
                }

                var tmpId         = Guid.NewGuid().ToString("N");
                var generatedCode = BuildMethodCodeAsClass(options, tmpId);

                TempAssemblyProvider provider = new TempAssemblyProvider();
                var tempAssembly = provider.CreateTempAssembly(generatedCode.AsArray());

                type = ReflectionExtension.SmartGetType(string.Format("{0}.DynamicStatiClass{1}", _namespace, tmpId));
                type.CheckNullObjectAsInvalid(nameof(options), data: new { options, generatedCode, tmpId });

                _methodInfo = type.GetMethod(string.Format("DynamicStaticMethod{0}", tmpId));
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { options });
            }
        }
예제 #6
0
        /// <summary>
        /// Creates the method invoker.
        /// </summary>
        /// <param name="methodCodeFullName">Full name of the method code.</param>
        /// <returns></returns>
        public static MethodInvoker CreateMethodInvoker(string methodCodeFullName)
        {
            try
            {
                methodCodeFullName.CheckEmptyString(nameof(methodCodeFullName));
                //TODO. Consider simple case in this stage.
                // No generic, no instance. Only static method. {namespace}+.{class}.{method}

                var lastDot = methodCodeFullName.LastIndexOf('.');
                if (lastDot > -1)
                {
                    string method       = methodCodeFullName.Substring(lastDot);
                    var    typeFullName = methodCodeFullName.Substring(0, lastDot);

                    var type = ReflectionExtension.SmartGetType(typeFullName, false);
                    type.CheckNullObjectAsInvalid(nameof(methodCodeFullName), data: new { method, typeFullName });

                    var methodInfo = type.GetMethod(method);
                    methodInfo.CheckNullObjectAsInvalid(nameof(methodCodeFullName), data: new { method, typeFullName });

                    return(new MethodInvoker(methodInfo));
                }
                else
                {
                    throw ExceptionFactory.CreateInvalidObjectException(nameof(methodCodeFullName), data: new { methodCodeFullName });
                }
            }
            catch (BaseException bex)
            {
                throw bex;
            }
            catch (Exception ex)
            {
                throw ex.Handle(new { methodCodeFullName });
            }
        }
        /// <summary>
        /// Froms the raw.
        /// </summary>
        /// <param name="sourceAssembly">The source assembly.</param>
        /// <param name="readerType">Type of the reader.</param>
        /// <param name="coreComponentVersion">The core component version.</param>
        /// <param name="configurationItem">The configuration item.</param>
        /// <returns></returns>
        public static RuntimeConfigurationItem FromRaw(string sourceAssembly, string readerType, Version coreComponentVersion, ConfigurationRawItem configurationItem)
        {
            try
            {
                configurationItem.CheckNullObject(nameof(configurationItem));
                configurationItem.Type.CheckEmptyString(nameof(configurationItem.Type));

                var result = new RuntimeConfigurationItem(configurationItem)
                {
                    Assembly    = sourceAssembly,
                    ReaderType  = readerType,
                    RuntimeType = ReflectionExtension.SmartGetType(configurationItem.Type, false) ?? ReflectionExtension.SmartGetType(configurationItem.Type, true)
                };

                result.IsActive = JudgeActive(coreComponentVersion, result.MinComponentVersionRequired, result.MaxComponentVersionLimited);
                result.Value    = RawStringToObject(configurationItem.Value, result.RuntimeType);

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }
예제 #8
0
        /// <summary>
        /// Initializes static members of the <see cref="EnvironmentCore"/> class.
        /// </summary>
        static EnvironmentCore()
        {
            var baseDirectoryByAppDomain        = AppDomain.CurrentDomain.BaseDirectory;
            var baseDirectoryByAssemblyLocation = Path.GetDirectoryName(typeof(EnvironmentCore).Assembly.Location);

            // NOTE:
            // In IIS Express cases, baseDirectoryByAssemblyLocation would be allocated into asp.net tmp folders, by each library.
            // In other cases, IIS or Console or Windows environments, baseDirectoryByAssemblyLocation should be correct.
            DirectoryInfo baseDirectory = new DirectoryInfo((baseDirectoryByAppDomain.StartsWith(baseDirectoryByAssemblyLocation, StringComparison.OrdinalIgnoreCase)) ?
                                                            baseDirectoryByAssemblyLocation
                 : Path.Combine(baseDirectoryByAppDomain, "bin"));

            if (baseDirectory?.Exists ?? false)
            {
                ApplicationBaseDirectory = baseDirectory.ToString();
            }
            else
            {
                throw ExceptionFactory.CreateInvalidObjectException(nameof(baseDirectory), new
                {
                    baseDirectory = baseDirectory?.ToString(),
                    baseDirectoryByAppDomain,
                    baseDirectoryByAssemblyLocation
                });
            }

            LogDirectory  = Path.Combine(ApplicationBaseDirectory, "logs");
            ApplicationId = System.AppDomain.CurrentDomain.Id;

            var dependencyChain = ReflectionExtension.GetAppDomainAssemblies().GetAssemblyDependencyChain(true);

            AscendingAssemblyDependencyChain = new List <Assembly>(dependencyChain).AsReadOnly();
            dependencyChain.Reverse();

            DescendingAssemblyDependencyChain = dependencyChain.AsReadOnly();

            CommonComponentInfo = typeof(EnvironmentCore).Assembly.GetCustomAttribute <BeyovaComponentAttribute>()?.UnderlyingObject;

            try
            {
                MachineName = Environment.MachineName;
            }
            catch { MachineName = string.Empty; }

            try
            {
                LocalMachineHostName = Dns.GetHostName();

                var host = Dns.GetHostEntry(LocalMachineHostName);
                foreach (var ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        LocalMachineIpAddress = ip.ToString();
                        break;
                    }
                }
            }
            catch { }

            try
            {
                ProductName = FindProductName();

                if (string.IsNullOrWhiteSpace(ProductName))
                {
                    ProductName = Assembly.GetEntryAssembly()?.FullName;
                }

                if (string.IsNullOrWhiteSpace(ProductName) && AppDomain.CurrentDomain != null)
                {
                    ProductName = AppDomain.CurrentDomain.FriendlyName;
                }
            }
            catch { ProductName = string.Empty; }
        }