示例#1
0
        public void SimpleClassCompilation()
        {
            // arrange
            string sourceText = @"
                namespace FS
                {
                    public class Foo
                    {
                        public string Bar()
                        {
                            return ""Hello World"";
                        }
                    }
                }
            ";

            // act
            Assembly assembly = CSharpCompiler.Compile(sourceText);

            // assert
            object obj = assembly.CreateInstance("FS.Foo");

            Assert.NotNull(obj);

            MethodInfo method = obj.GetType().GetMethod("Bar");
            object     result = method.Invoke(obj, Array.Empty <object>());

            Assert.IsType <string>(result);
            Assert.Equal("Hello World", result);
        }
        public IEnumerable <FieldResolver> Build(
            IEnumerable <FieldResolverDescriptor> fieldResolverDescriptors)
        {
            FieldResolverDescriptor[] descriptors = fieldResolverDescriptors.ToArray();
            if (descriptors.Length == 0)
            {
                yield break;
            }

            string   sourceText = _codeGenerator.Generate(descriptors);
            Assembly assembly   = CSharpCompiler.Compile(sourceText);
            Type     type       = assembly.GetType(FieldResolverSourceCodeGenerator.FullClassName);

            for (var i = 0; i < descriptors.Length; i++)
            {
                string    resolverName = _codeGenerator.GetResolverName(i);
                FieldInfo field        = type.GetField(resolverName,
                                                       BindingFlags.Static | BindingFlags.Public);
                yield return(new FieldResolver(
                                 descriptors[i].Field.TypeName,
                                 descriptors[i].Field.FieldName,
                                 (FieldResolverDelegate)field.GetValue(field)));
            }
        }
示例#3
0
        public void SimpleClassWithReferenceToCurrentAssembly()
        {
            // arrange
            string sourceText = @"
                using HotChocolate.Resolvers;

                namespace FS
                {
                    public class DynamicFoo
                    {
                        public string Bar(Foo foo)
                        {
                            return foo.Bar;
                        }
                    }
                }
            ";

            // act
            Assembly assembly = CSharpCompiler.Compile(sourceText);

            // assert
            Foo foo = new Foo {
                Bar = Guid.NewGuid().ToString()
            };

            object obj = assembly.CreateInstance("FS.DynamicFoo");

            Assert.NotNull(obj);

            MethodInfo method = obj.GetType().GetMethod("Bar");
            object     result = method.Invoke(obj, new object[] { foo });

            Assert.IsType <string>(result);
            Assert.Equal(foo.Bar, result);
        }