一、 要动态执行的代码字符串
using System; nnamespace Jinshuai { public class jinshuaiExe { public string get() { return string.Format(" 月色变得黯然 在深秋的黎明{0} 而你双手是那么暖{0} 离别最好的季节 是风里透着凉意{0} 才知道两颗心能靠多紧{0} 夜雾慢慢散去 在深秋的黎明{0} 而我双眼离不开你{0} 我爱你就象呼吸 感觉平淡无奇{0} 用生命全心全意让爱能继续{0} 深秋的这样一个黎明{0} 无限清醒在心底{0} 远行的我看着天空慢慢亮起来{0} 深秋的这样一个黎明{0} 你不必怕寂寞{0} 爱的情意漫天过海会包围你{0} 深秋的这样一个黎明{0} 无限清醒在心底{0} 远行的我看着天空慢慢亮起来{0} 深秋的这样一个黎明{0} 你不必怕寂寞{0} 爱的情意漫天过海会包围你{0} 深秋的这样一个黎明{0} 无限清醒在心底{0} 远行的我看着天空慢慢亮起来{0} 深秋的这样一个黎明{0} 你不必怕寂寞{0} 爱的情意漫天过海会包围你{0} 冷雨静静飘落 在深秋的黎明{0} 你的背影那么孤单{0} 红叶象烈火燃烧 比不过我的心{0} 我的爱永不会熄灭燃烧不尽{0} 我的爱永不会熄灭燃烧不尽{0}" ,"\\n"); } } }
二、 对上述的 代码字符串进行转BYTE 操作
byte[] b = { 117, 115, 105, 110, 103, 32, 83, 121, 115, 116, 101, 109, 59, 13, 10, 110, 97, 109, 101, 115, 112, 97, 99, 101, 32, 74, 105, 110, 115, 104, 117, 97, 105, 13, 10, 123, 13, 10, 32, 32, 32, 32, 32, 112, 117, 98, 108, 105, 99, 32, 99, 108, 97, 115, 115, 32, 106, 105, 110, 115, 104, 117, 97, 105, 69, 120, 101, 13, 10, 32, 32, 32, 32, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 112, 117, 98, 108, 105, 99, 32, 115, 116, 114, 105, 110, 103, 32, 103, 101, 116, 40, 41, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 32, 32, 115, 116, 114, 105, 110, 103, 46, 70, 111, 114, 109, 97, 116, 40, 34, 32, 212, 194, 201, 171, 177, 228, 181, 195, 247, 246, 200, 187, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 182, 248, 196, 227, 203, 171, 202, 214, 202, 199, 196, 199, 195, 180, 197, 175, 123, 48, 125, 192, 235, 177, 240, 215, 238, 186, 195, 181, 196, 188, 190, 189, 218, 32, 202, 199, 183, 231, 192, 239, 205, 184, 215, 197, 193, 185, 210, 226, 123, 48, 125, 178, 197, 214, 170, 181, 192, 193, 189, 191, 197, 208, 196, 196, 220, 191, 191, 182, 224, 189, 244, 123, 48, 125, 210, 185, 206, 237, 194, 253, 194, 253, 201, 162, 200, 165, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 182, 248, 206, 210, 203, 171, 209, 219, 192, 235, 178, 187, 191, 170, 196, 227, 123, 48, 125, 206, 210, 176, 174, 196, 227, 190, 205, 207, 243, 186, 244, 206, 252, 32, 184, 208, 190, 245, 198, 189, 181, 173, 206, 222, 198, 230, 123, 48, 125, 211, 195, 201, 250, 195, 252, 200, 171, 208, 196, 200, 171, 210, 226, 200, 195, 176, 174, 196, 220, 188, 204, 208, 248, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 192, 228, 211, 234, 190, 178, 190, 178, 198, 174, 194, 228, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 196, 227, 181, 196, 177, 179, 211, 176, 196, 199, 195, 180, 185, 194, 181, 165, 123, 48, 125, 186, 236, 210, 182, 207, 243, 193, 210, 187, 240, 200, 188, 201, 213, 32, 177, 200, 178, 187, 185, 253, 206, 210, 181, 196, 208, 196, 123, 48, 125, 206, 210, 181, 196, 176, 174, 211, 192, 178, 187, 187, 225, 207, 168, 195, 240, 200, 188, 201, 213, 178, 187, 190, 161, 123, 48, 125, 206, 210, 181, 196, 176, 174, 211, 192, 178, 187, 187, 225, 207, 168, 195, 240, 200, 188, 201, 213, 178, 187, 190, 161, 123, 48, 125, 34, 32, 32, 44, 34, 92, 110, 34, 41, 59, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 125, 13, 10, 32, 32, 32, 32, 32, 125, 13, 10, 125 };
三、 具体代码
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 7 byte[] b = { 117, 115, 105, 110, 103, 32, 83, 121, 115, 116, 101, 109, 59, 13, 10, 110, 97, 109, 101, 115, 112, 97, 99, 101, 32, 74, 105, 110, 115, 104, 117, 97, 105, 13, 10, 123, 13, 10, 32, 32, 32, 32, 32, 112, 117, 98, 108, 105, 99, 32, 99, 108, 97, 115, 115, 32, 106, 105, 110, 115, 104, 117, 97, 105, 69, 120, 101, 13, 10, 32, 32, 32, 32, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 112, 117, 98, 108, 105, 99, 32, 115, 116, 114, 105, 110, 103, 32, 103, 101, 116, 40, 41, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 114, 101, 116, 117, 114, 110, 32, 32, 32, 115, 116, 114, 105, 110, 103, 46, 70, 111, 114, 109, 97, 116, 40, 34, 32, 212, 194, 201, 171, 177, 228, 181, 195, 247, 246, 200, 187, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 182, 248, 196, 227, 203, 171, 202, 214, 202, 199, 196, 199, 195, 180, 197, 175, 123, 48, 125, 192, 235, 177, 240, 215, 238, 186, 195, 181, 196, 188, 190, 189, 218, 32, 202, 199, 183, 231, 192, 239, 205, 184, 215, 197, 193, 185, 210, 226, 123, 48, 125, 178, 197, 214, 170, 181, 192, 193, 189, 191, 197, 208, 196, 196, 220, 191, 191, 182, 224, 189, 244, 123, 48, 125, 210, 185, 206, 237, 194, 253, 194, 253, 201, 162, 200, 165, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 182, 248, 206, 210, 203, 171, 209, 219, 192, 235, 178, 187, 191, 170, 196, 227, 123, 48, 125, 206, 210, 176, 174, 196, 227, 190, 205, 207, 243, 186, 244, 206, 252, 32, 184, 208, 190, 245, 198, 189, 181, 173, 206, 222, 198, 230, 123, 48, 125, 211, 195, 201, 250, 195, 252, 200, 171, 208, 196, 200, 171, 210, 226, 200, 195, 176, 174, 196, 220, 188, 204, 208, 248, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 206, 222, 207, 222, 199, 229, 208, 209, 212, 218, 208, 196, 181, 215, 123, 48, 125, 212, 182, 208, 208, 181, 196, 206, 210, 191, 180, 215, 197, 204, 236, 191, 213, 194, 253, 194, 253, 193, 193, 198, 240, 192, 180, 123, 48, 125, 201, 238, 199, 239, 181, 196, 213, 226, 209, 249, 210, 187, 184, 246, 192, 232, 195, 247, 123, 48, 125, 196, 227, 178, 187, 177, 216, 197, 194, 188, 197, 196, 175, 123, 48, 125, 176, 174, 181, 196, 199, 233, 210, 226, 194, 254, 204, 236, 185, 253, 186, 163, 187, 225, 176, 252, 206, 167, 196, 227, 123, 48, 125, 192, 228, 211, 234, 190, 178, 190, 178, 198, 174, 194, 228, 32, 212, 218, 201, 238, 199, 239, 181, 196, 192, 232, 195, 247, 123, 48, 125, 196, 227, 181, 196, 177, 179, 211, 176, 196, 199, 195, 180, 185, 194, 181, 165, 123, 48, 125, 186, 236, 210, 182, 207, 243, 193, 210, 187, 240, 200, 188, 201, 213, 32, 177, 200, 178, 187, 185, 253, 206, 210, 181, 196, 208, 196, 123, 48, 125, 206, 210, 181, 196, 176, 174, 211, 192, 178, 187, 187, 225, 207, 168, 195, 240, 200, 188, 201, 213, 178, 187, 190, 161, 123, 48, 125, 206, 210, 181, 196, 176, 174, 211, 192, 178, 187, 187, 225, 207, 168, 195, 240, 200, 188, 201, 213, 178, 187, 190, 161, 123, 48, 125, 34, 32, 32, 44, 34, 92, 110, 34, 41, 59, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 125, 13, 10, 32, 32, 32, 32, 32, 125, 13, 10, 125 }; 8 9 var cc= System.Text.Encoding.Default.GetString(b); 10 11 var js = JinshuaiCodeProvider.Execute<string>(cc, "Jinshuai.jinshuaiExe", "get"); 12 13 14 Console.WriteLine(js); 15 16 Console.ReadLine(); 17 18 } 19 }
1 using System; 2 using System.CodeDom.Compiler; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Reflection; 6 using System.Text; 7 using Microsoft.CSharp; 8 9 namespace ConsoleCode 10 { 11 public class JinshuaiCodeProvider 12 { 13 14 public static T Execute<T>(string code, string runInstanceType,string method) 15 { 16 var codeProvider = new CSharpCodeProvider(); 17 18 var objCompilerParameters = new CompilerParameters(); 19 objCompilerParameters.ReferencedAssemblies.Add("System.dll");//引用dll 20 objCompilerParameters.GenerateExecutable = false; 21 objCompilerParameters.GenerateInMemory = true; 22 objCompilerParameters.IncludeDebugInformation = false; 23 24 25 var cr = codeProvider.CompileAssemblyFromSource(objCompilerParameters, new string[] { code }); 26 27 if (cr.Errors.HasErrors) 28 { 29 throw new Exception("代码编译失败,原因:帅哥就不给你说。"); 30 } 31 32 var objAssembly = cr.CompiledAssembly; 33 object jinshuaiInstance = objAssembly.CreateInstance(runInstanceType); 34 if (jinshuaiInstance != null) 35 { 36 var methodPoint = jinshuaiInstance.GetType().GetMethod(method); 37 var result=(T)methodPoint.Invoke(jinshuaiInstance, null); 38 39 return result; 40 } 41 42 throw new Exception("代码执行失败,原因:帅哥快点说"); 43 44 } 45 46 } 47 }
就当是写着玩吧。