예제 #1
0
        /// <summary>
        /// Creates the temporary assembly.
        /// </summary>
        /// <param name="sourceCodes">The source codes.</param>
        /// <param name="codeReferencedAssembly">The referenced assembly.</param>
        /// <returns>TempAssembly.</returns>
        public RemoteRuntimeCompileResult CreateRemoteTempAssembly(string[] sourceCodes, HashSet <string> codeReferencedAssembly = null)
        {
            try
            {
                sourceCodes.CheckNullObject("sourceCodes");

                var tempAssembly = provider.CreateTempAssembly(sourceCodes, codeReferencedAssembly);
                var name         = tempAssembly.Name;
                tempAssemblies.Add(tempAssembly.Name, tempAssembly);

                return(new RemoteRuntimeCompileResult {
                    TempAssemblyName = name
                });
            }
            catch (Exception ex)
            {
                return(new RemoteRuntimeCompileResult {
                    CompileExceptionInfo = ex.Handle(sourceCodes).ToExceptionInfo().ToJson()
                });
            }
        }
예제 #2
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 });
            }
        }
        /// <summary>
        /// Creates the deep equality instance.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        internal static IDeepEquality <T> CreateDeepEqualityInstance <T>(StringComparison stringComparison)
        {
            Type   type = typeof(T);
            object instance;

            if (!equalityInstances.TryGetValue(type, out instance))
            {
                lock (locker)
                {
                    if (!equalityInstances.TryGetValue(type, out instance))
                    {
                        var typeName = type.GenerateUniqueTypeName();
                        var codes    = CreateDeepEqualityClassCode(type, typeName, stringComparison);

                        TempAssemblyProvider provider = new TempAssemblyProvider();
                        var tmpAssembly = provider.CreateTempAssembly(codes, TempAssemblyProvider.GetCurrentAppDomainAssemblyLocations(), type.Namespace.AsArray(), @namespace: _namespace);
                        instance = tmpAssembly.CreateInstance(string.Format("{0}.{1}", _namespace, typeName));
                        equalityInstances.Add(type, instance);
                    }
                }
            }

            return(instance as IDeepEquality <T>);
        }