/// <summary> /// 精准获取动态类 /// </summary> /// <param name="classIndex">类索引,1开始</param> /// <param name="namespaceIndex">命名空间索引,1开始</param> /// <returns></returns> public Type GetType(int classIndex = 1, int namespaceIndex = 1) { Complier.Add(this); string name = default; switch (OopTypeEnum) { case OopType.Class: name = ScriptHelper.GetClassName(Script, classIndex, namespaceIndex); break; case OopType.Struct: name = ScriptHelper.GetStructName(Script, classIndex, namespaceIndex); break; case OopType.Interface: name = ScriptHelper.GetInterfaceName(Script, classIndex, namespaceIndex); break; case OopType.Enum: name = ScriptHelper.GetEnumName(Script, classIndex, namespaceIndex); break; } return(Complier.GetType(name)); }
public static void RunClassName1() { //ScriptComplier.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;} } } "; //根据脚本创建动态类 AssemblyComplier oop = new AssemblyComplier(); oop.Add(text); Type type = oop.GetType("TestIndex3"); Assert.Equal("TestIndex3", type.Name); }
public static void RunClassName0() { //ScriptComplier.Init(); string text = @" namespace HelloWorld {public class Test{public Test(){ Name=""111""; }public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 AssemblyComplier oop = new AssemblyComplier(); oop.Add(text); Type type = oop.GetType("Test"); Assert.Equal("Test", type.Name); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 AssemblyComplier oop = new AssemblyComplier("test"); oop.Add(text); Type type = oop.GetType("Test"); Console.WriteLine(type.Name); var action = NDomain.Random().Action(""); var a = action.Method; Console.WriteLine(action.Method.Module.Assembly); Console.ReadKey(); }
static void Main(string[] args) { /* * 在此之前,你需要右键,选择工程文件,在你的.csproj里面 * * 写上这样一句浪漫的话: * * <PreserveCompilationContext>true</PreserveCompilationContext> */ string text = @"namespace HelloWorld { public class Test { public Test(){ Name=""111""; } public string Name; public int Age{get;set;} } }"; //根据脚本创建动态类 AssemblyComplier oop = new AssemblyComplier("test"); oop.Add(text); Type type = oop.GetType("Test"); var func = "return arg;".Delegate <Func <string, string> >(); Console.WriteLine(func("111")); Console.ReadKey(); }