コード例 #1
0
ファイル: Program.cs プロジェクト: YeldarOralov/EFLibrary
        static void Main(string[] args)
        {
            var authors = new List <Author>();

            authors.Add(new Author {
                FullName = "Иван Иванов"
            });
            authors.Add(new Author {
                FullName = "Петр Петров"
            });
            authors.Add(new Author {
                FullName = "Сергей Сергеев"
            });

            var books = new List <Book>();

            books.Add(new Book {
                Name = "Сказки на ночь", Authors = new List <Author> {
                    authors[0], authors[1]
                }
            });
            books.Add(new Book {
                Name = "Зеленая шапочка", Authors = new List <Author> {
                    authors[1], authors[2]
                }
            });
            books.Add(new Book {
                Name = "Песик Гав-Гав", Authors = new List <Author> {
                    authors[2], authors[0]
                }
            });

            var students = new List <Student>();

            students.Add(new Student {
                FullName = "Бека", Books = new List <Book> {
                    books[0], books[1]
                }
            });
            students.Add(new Student {
                FullName = "Сека", Books = new List <Book> {
                    books[2]
                }
            });
            students.Add(new Student {
                FullName = "Эрик", Books = new List <Book>()
            });

            //using (var context = new AppContext())
            //{
            //    context.Authors.AddRange(authors);
            //    context.Books.AddRange(books);
            //    context.Students.AddRange(students);
            //    context.SaveChanges();
            //}

            using (var context = new AppContext())
            {
                //1
                foreach (var student in context.Database.SqlQuery <Student>
                             ("Select distinct s.id, s.fullname from Students s, StudentBooks sb where s.id = sb.student_id and sb.book_id is not null"))
                {
                    Console.WriteLine(student.FullName);
                }

                //2
                foreach (var author in context.Database.SqlQuery <Author>
                             ($"Select a.id, a.fullname from Books b, BookAuthors ba, Authors a where b.id=ba.book_id and ba.author_id=a.id and b.id = '{context.Books.ToList()[2].Id}'"))
                {
                    Console.WriteLine(author.FullName);
                }

                //3
                foreach (var book in context.Database.SqlQuery <Book>
                             ("Select b.id, b.name from Books b, StudentBooks sb where b.id=sb.book_id and not exists"))
                {
                    Console.WriteLine(book.Name);
                }
            }
            Console.Read();
        }
コード例 #2
0
        public static void Compile()
        {
            var testTree = CSharpSyntaxTree.ParseText(@"
using System;
using ExternalLibrary;

namespace test{

    public class Power
    {

        public void power(int number)
        { 
            new ExternalClass().Print(number * number);
        } 

    }

}");

            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);

            options = options.WithAssemblyIdentityComparer(AssemblyIdentityComparer.Default);

            var references = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51").Location),
                MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ExternalLibrary.dll"))
            };

            if (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") is string trustedAssemblies)
            {
                Console.WriteLine(trustedAssemblies);
                references.AddRange(
                    trustedAssemblies.Split(Path.PathSeparator).Select(path =>
                                                                       MetadataReference.CreateFromFile(path))
                    );
            }

            var compilation = CSharpCompilation.Create("DummyAssembly", options: options, references: references)
                              .AddSyntaxTrees(testTree);

            using (var stream = new MemoryStream())
            {
                var compilationResult = compilation.Emit(stream);

                if (!compilationResult.Success)
                {
                    Console.WriteLine(string.Join("\n", compilationResult.Diagnostics.Select(d => d.GetMessage())));
                }
                else
                {
                    stream.Seek(0, SeekOrigin.Begin);
                    var assembly = Assembly.Load(stream.ToArray());
                    var type     = assembly.GetType("test.Power");
                    var power    = Activator.CreateInstance(type);
                    type.InvokeMember("power", BindingFlags.Default | BindingFlags.InvokeMethod, null, power, new object[] { 2 });
                }
            }
        }