Пример #1
0
        public void CSharpCodeWriter_SimpleVariable()
        {
            var writer = new CSharpCodeWriter();

            var expression = "return intValue + 1";

            var classText = writer.GetClassText(
                expression: expression,
                variables: new List <CSharpCodeWriter.Variable>
            {
                new CSharpCodeWriter.Variable
                {
                    Name = "intValue",
                    Type = typeof(int)
                }
            },
                usings: new List <string> {
            },
                methods: new List <string> {
            },
                withReturn: true);

            Assert.AreEqual(
                ResourceReader.CSharpSimpleVariable.Replace("\r\n", "\n"),
                classText.Replace("\r\n", "\n"));
        }
Пример #2
0
        public void CSharpCodeWriter_SimpleExpression()
        {
            var writer = new CSharpCodeWriter();

            var expression = "return 1 + 1";

            var classText = writer.GetClassText(
                expression: expression,
                variables: new List <CSharpCodeWriter.Variable> {
            },
                usings: new List <string> {
            },
                methods: new List <string> {
            },
                withReturn: true);

            Assert.AreEqual(
                // line ending types don't matter.
                // making sure tests work on Windows and *nix platforms.
                ResourceReader.CSharpSimpleExpression.Replace("\r\n", "\n"),
                classText.Replace("\r\n", "\n"));
        }
Пример #3
0
        private void Init(
            string caller,
            bool hasReturn)
        {
            CSharpCodeWriter writer = new CSharpCodeWriter();

            string classText = writer.GetClassText(
                expression,
                variables.Select(entry => new CSharpCodeWriter.Variable
            {
                Name = entry.Key,
                Type = entry.Value.Type
            }).ToList(),
                usings,
                methods,
                withReturn: hasReturn);

            if (DebugFileOutputName != null)
            {
                File.WriteAllText(
                    DebugFileOutputName,
                    classText);
            }

            // instead of taking the everytime hit of a synchronized lock
            // choosing to take the infrequent possible hit of simultaneous
            // calls creating multiple types with the same class text
            // only the last one's definition will be cached for the next caller
            bool alreadyCompiled = compiledTypes.ContainsKey(classText);

            if (alreadyCompiled)
            {
                execution = compiledTypes[classText];
            }
            else
            {
                references.Add(caller);

                // add references to containing assemblies for all used variable types
                variables
                .Select(v => v.Value.Type.Assembly.Location)
                .Distinct()
                .ToList()
                .ForEach(a => references.Add(a));

                execution = new Execution();

                Compiler compiler = new Compiler();

                Type newType = compiler.Compile(
                    classText,
                    references,
                    "EvalAssembly",
                    "CustomEvaluator");

                execution.Constructor = new DefaultClassConstructorExpression().GetFunc(
                    newType);

                foreach (string key in variables.Keys)
                {
                    Variable variable = variables[key];

                    Func <object, object>   getter = null;
                    Action <object, object> setter = null;

                    if (variable.Type.IsPublic)
                    {
                        getter = new GetInstanceMemberValueExpression().GetFunc(
                            newType,
                            key);

                        setter = new SetInstanceMemberValueExpression().GetAction(
                            newType,
                            key);
                    }
                    else
                    {
                        getter = new WrapperTranslator().GetGetAndUnwrap(
                            newType,
                            key);

                        setter = new WrapperTranslator().GetWrapAndSet(
                            newType,
                            key);
                    }

                    execution.Variables[key] = new ExecutionVariable
                    {
                        Getter = getter,
                        Setter = setter
                    };
                }

                if (hasReturn)
                {
                    execution.Evaluate = new ExecuteInstanceMethodExpression().GetFuncWithReturn(
                        newType,
                        "Eval");
                }
                else
                {
                    execution.Execute = new ExecuteInstanceMethodExpression().GetFuncWithNoReturn(
                        newType,
                        "Eval");
                }

                compiledTypes[classText] = execution;
            }

            initialized = true;
        }