コード例 #1
0
        public CompilerErrorCollection SetExpression(string expression)
        {
            CompilerErrorCollection compilerErrors = new CompilerErrorCollection();

            IUnitPriceExpression newExpression = CreateDynamicExpression(expression, out compilerErrors);

            if (!compilerErrors.HasErrors)
            {
                this.expression = newExpression;
            }

            return(compilerErrors);
        }
コード例 #2
0
        private static IUnitPriceExpression CreateDynamicExpression(string expression, out CompilerErrorCollection errors)
        {
            errors = new CompilerErrorCollection();

            if (String.IsNullOrEmpty(expression))
            {
                return(null);
            }

            CompilerParameters parms = new CompilerParameters();

            parms.GenerateExecutable      = false;
            parms.GenerateInMemory        = true;
            parms.IncludeDebugInformation = false;

            string assemblyFileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);

            parms.ReferencedAssemblies.Add("System.dll");
            parms.ReferencedAssemblies.Add(assemblyFileName);

            CodeDomProvider compiler  = CSharpCodeProvider.CreateProvider("CSharp");
            string          classCode = string.Format(ClassTemplate, typeof(UnitPriceConverter).FullName, expression);

            CompilerResults compilerResults = compiler.CompileAssemblyFromSource(parms, classCode);

            if (compilerResults.Errors.HasErrors)
            {
                errors = compilerResults.Errors;
                return(null);
            }

            Assembly             assembly            = compilerResults.CompiledAssembly;
            IUnitPriceExpression unitPriceExpression = assembly.CreateInstance("DynamicUnitPriceExpression") as IUnitPriceExpression;

            return(unitPriceExpression);
        }