Example #1
0
        static void Main(string[] args)
        {
            string text = @"namespace HelloWorld
{
    public class Test
    {
        public Test(){
            Name=""111"";
        }

        public string Name;
        public int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder("test");

            oop.Add(text);
            Type type = oop.GetTypeFromShortName("Test");

            Console.WriteLine(type.Name);

            var action = NDelegate.RandomDomain().Action("");
            var a      = action.Method;

            Console.WriteLine(action.Method.Module.Assembly);
            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            NatashaInitializer.Initialize(false);
            string text = @"namespace HelloWorld
{
    public class Test
    {
        public Test(){
            Name=""111"";
        }

        public string Name;
        public int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder("test");

            oop.Domain = DomainManagement.Random;
            oop.Domain.AddReferencesFromDllFile(typeof(object).Assembly.Location);
            oop.Add(text);
            Type type = oop.GetTypeFromShortName("Test");

            Console.WriteLine(type.Name);

            var action = NDelegate.UseDomain(oop.Domain).Action("");
            var a      = action.Method;

            Console.WriteLine(action.Method.Module.Assembly);
            Console.WriteLine(DomainManagement.IsDeleted(action.GetDomain().Name));
            Console.ReadKey();
        }
Example #3
0
        public void RunClassName1()
        {
            //ScriptCompiler.Init();
            string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HelloWorld
{
    public class TestIndex1
    {
        public string Name;
        public int Age{get;set;}
    }
    public class TestIndex2
    {
        public string Name;
        public int Age{get;set;}
    }

    public class TestIndex3
    {
        public string Name;
        public int Age{get;set;}
    }
}

namespace HelloWorld{
    public struct TestStruct1{}
    public struct TestStruct2{}
    public class TestIndex4
    {
        public string Name;
        public int Age{get;set;}
    }
}

";
            //根据脚本创建动态类
            AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder();

            oop.Compiler.Domain = DomainManagement.Random;
            oop.Add(text);
            Type type = oop.GetTypeFromShortName("TestIndex3");

            Assert.Equal("TestIndex3", type.Name);
        }
Example #4
0
        static void Main(string[] args)
        {
            NatashaInitializer.Initialize();
            string text = @"namespace HelloWorld
            {
                public class Test
                {
                    public Test(){
                        Name=""111"";
                    }

                    public string Name;
                    public int Age{get;set;}
                }
            }";
            //根据脚本创建动态类
            AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder("test");

            oop.Domain = DomainManagement.Random;
            //oop.Domain.AddReferencesFromDllFile(typeof(object).Assembly.Location);
            oop.Add(text);
            Type type = oop.GetTypeFromShortName("Test");

            Console.WriteLine(type.Name);

            var action = NDelegate.UseDomain(oop.Domain).Action("");
            var a      = action.Method;

            Console.WriteLine(action.Method.Module.Assembly);
            Console.WriteLine(DomainManagement.IsDeleted(action.GetDomain().Name));

            //var nClass = NClass
            //    .RandomDomain();
            //nClass.DelegateHandler.
            //type = NClass
            //    .RandomDomain().Namespace("")
            //    .Name("Test")
            //    .Ctor(item => item
            //        .Public()
            //        .Body("Name = null;")
            //    )
            //    .PublicField<string>("Name")
            //    .GetType();
            //type.Assembly
            //CSharpScript.EvaluateAsync

            Console.ReadKey();
        }
Example #5
0
        public void TestCall4()
        {
            //ScriptComplier.Init();
            string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HelloWorld
{
    public class Test
    {
        public Test(){
            Name=""111"";
            Pp = 10;
            Rp=""aa"";
        }
        private long Pp;
        private readonly string Rp;
        public string Name;
        public int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            var oop = new AssemblyCSharpBuilder();

            oop.Add(text);
            Type type = oop.GetTypeFromShortName("Test");
            //创建动态类实例代理
            var instance = PrecisionDictOperator.CreateFromType(type);
            var obj      = Activator.CreateInstance(type);

            instance.SetObjInstance(obj);
            instance["Pp"] = 30L;
            instance["Rp"] = "ab";
            //Get动态调用
            Assert.Equal("111", (string)instance["Name"]);
            Assert.Equal("ab", (string)instance["Rp"]);
            Assert.Equal(30, (long)instance["Pp"]);
            //调用动态委托赋值
            instance.Set("Name", "222");

            Assert.Equal("222", (string)instance["Name"]);
        }
Example #6
0
        public static void RunClassName0()
        {
            //ScriptCompiler.Init();
            string text = @"
namespace HelloWorld
{public class Test{public Test(){
            Name=""111"";
        }public string Name;
        public int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder();

            oop.Compiler.Domain = DomainManagement.Random;
            oop.Syntax.Add(text);
            Type type = oop.GetTypeFromShortName("Test");

            Assert.Equal("Test", type.Name);
        }
Example #7
0
        public void TestCall1()
        {
            //ScriptComplier.Init();
            string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HelloWorld
{
    public class Test2
    {
        public Test2(){
            Name=""111"";
        }

        public string Name;
        public int Age{get;set;}
    }
}";

            //根据脚本创建动态类
            var oop = new AssemblyCSharpBuilder();

            oop.Syntax.Add(text);
            var type = oop.GetTypeFromShortName("Test2");

            NErrorLog.Enabled = true;
            //创建动态类实例代理
            CallerManagement.AddType(type);
            var instance = LinkOperator.CreateFromType(type);

            instance.New();
            //Get动态调用
            Assert.Equal("111", instance["Name"].Get <string>());
            //调用动态委托赋值
            instance.Set("Name", "222");

            Assert.Equal("222", instance["Name"].Get <string>());
        }
Example #8
0
        public void TestCall1()
        {
            //ScriptComplier.Init();
            string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace HelloWorld
{
    public static class StaticTest2
    {
        static StaticTest2(){
            Name=""111"";
        }

        public static string Name;
        public static int Age{get;set;}
    }
}";
            //根据脚本创建动态类
            var oop = new AssemblyCSharpBuilder();

            oop.Syntax.Add(text);
            var type = oop.GetTypeFromShortName("StaticTest2");

            CallerManagement.AddType(type);
            //创建动态类实例代理
            var instance = DictOperator.CreateFromType(type);

            //Get动态调用
            Assert.Equal("111", (string)instance["Name"]);
            //调用动态委托赋值
            instance["Name"] = "222";

            Assert.Equal("222", (string)instance["Name"]);
            Assert.Equal("222", instance.Get <string>("Name"));
        }