Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1.  
  2. 呼び出し側
  3. ```cs
  4. using Microsoft.CSharp;
  5. using System;
  6. using System.CodeDom.Compiler;
  7. using System.Reflection;
  8.  
  9. namespace CSharpCodeProviderSample
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. using (CodeDomProvider codeDomProvider = new CSharpCodeProvider())
  16. {
  17. CompilerParameters compilerParameters = new CompilerParameters();
  18. // .exe作らずメモリ上で処理
  19. compilerParameters.GenerateInMemory = true;
  20.  
  21. // ファイルの読み込み
  22. CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromFile(compilerParameters, "Sample.cs");
  23.  
  24. Assembly assembly = compilerResults.CompiledAssembly;
  25.  
  26. // インスタンスの作成
  27. Type dynamicClassT = assembly.GetType("DynamicClass");
  28. dynamic dynamicClass = Activator.CreateInstance(dynamicClassT);
  29.  
  30. // プロパティ取得、メソッド実行
  31. Console.WriteLine(dynamicClass.Foo);
  32. Console.WriteLine(dynamicClass.Bar(1, 2));
  33. Console.WriteLine(dynamicClass.Hoge("だー"));
  34. }
  35.  
  36. Console.ReadKey();
  37. }
  38. }
  39. }
  40. ```
  41.  
  42. 呼び出される側
  43. ```cs
  44. using System;
  45.  
  46. public class DynamicClass
  47. {
  48. public String Foo = "いくぞー";
  49. public Double Bar (Double a, Double b)
  50. {
  51. Console.WriteLine(a);
  52. Console.WriteLine(b);
  53. return a+b;
  54. }
  55. public String Hoge (String s)
  56. {
  57. return s + "!!!";
  58. }
  59. }
  60. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement