Exemplo n.º 1
0
 static void Main(string[] args)
 {
     using (AppContext context = new AppContext())
     {
         var type     = DynamicTypeBuilder.CreateType();
         var myObject = Activator.CreateInstance(type);
         Console.WriteLine("Hello World!");
     }
 }
Exemplo n.º 2
0
        public void DynamicTypeBuilder_NoParams(DynamicTypeBuilder builder, IExportLocatorScope scope, IDisposalScope disposalScope, IInjectionContext context)
        {
            var proxyType = builder.CreateType(typeof(IBasicServiceProvider), out List <DynamicTypeBuilder.DelegateInfo> methods);

            var newBasicService = new BasicService();

            var func = new ActivationStrategyDelegate((s, d, c) => newBasicService);

            var instance = (IBasicServiceProvider)Activator.CreateInstance(proxyType, scope, disposalScope, context, func);

            var basicService = instance.CreateBasicService();

            Assert.NotNull(basicService);
            Assert.Same(newBasicService, basicService);
        }
Exemplo n.º 3
0
        public void DynamicTypeBuilder_StringParam(DynamicTypeBuilder builder, IExportLocatorScope scope, IDisposalScope disposalScope, InjectionContext context)
        {
            var proxyType = builder.CreateType(typeof(IComplexProvider <string>), out List <DynamicTypeBuilder.DelegateInfo> methods);

            string value = "hello world";
            DependentService <string> service = null;

            var func = new ActivationStrategyDelegate((s, d, c) =>
            {
                Assert.True(c.Keys.Any(key => key.ToString().StartsWith(UniqueStringId.Prefix) &&
                                       Equals(c.GetExtraData(key), value)));
                return(service = new DependentService <string>(value));
            });

            var instance = (IComplexProvider <string>)Activator.CreateInstance(proxyType, scope, disposalScope, context, func);

            var instanceService = instance.CreateValue(value);

            Assert.NotNull(instanceService);
            Assert.Same(service, instanceService);
        }
Exemplo n.º 4
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            if (System.Diagnostics.Debugger.IsAttached == false)
            {
                System.Diagnostics.Debugger.Launch();
            }
            //modelBuilder.Entity("EntityName", config =>
            //{
            //    config.Property("Id").HasColumnType("int");
            //    config.Property("Name").HasMaxLength(255);
            //    config.HasKey("Id");

            //});

            modelBuilder.Entity <Student>();
            var type   = DynamicTypeBuilder.CreateType();
            var method = modelBuilder.GetType().GetMethods().Where(x => x.IsGenericMethod && x.Name == "Entity").First();

            method = method.MakeGenericMethod(new Type[] { type });
            method.Invoke(modelBuilder, null);

            base.OnModelCreating(modelBuilder);
        }