ようこそ。睡眠不足なプログラマのチラ裏です。

C#でEval先から自身のメソッドが呼び出せる件

C#でもEvalできちゃう件については、既に語りつくされていますが、
Eval先から自身のアセンブリのメソッドを呼び出せる件については案外知られていないかも、とか。
私は知りませんでした。もしご存知でしたら、華麗にスルーしてくださいw

using System;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using System.Reflection;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Evalな文字列
            string source = @"public class Fuga
                            {
                                public static int Calc()
                                {
                                    int piyo = ConsoleApplication1.Program.CallBack();
                                    return 2*piyo+1;
                                }
                            }";
            
            hoge = new Hoge(12);

            //コンパイル準備
            CodeDomProvider cp = new Microsoft.CSharp.CSharpCodeProvider();
            CompilerParameters cps = new CompilerParameters();
            CompilerResults cres;
            
            //メモリ内で出力を生成
            cps.GenerateInMemory = true;
            cps.ReferencedAssemblies.Add("ConsoleApplication1.exe");

            //コンパイル
            cres = cp.CompileAssemblyFromSource(cps, source);

            if (cres.Errors.Count > 0)
            {   
                string errors = "コンパイル/(^o^)\シクジッタ:\n";
                foreach (CompilerError err in cres.Errors)
                {
                    errors += err.ToString() + "\n";
                }
                Console.WriteLine(errors);
                Console.ReadLine();
                return;
            }

            //コンパイルしたアセンブリを取得
            Assembly asm = cres.CompiledAssembly;

            //Evalする
            int d = (int)asm.GetType("Fuga").InvokeMember("Calc", BindingFlags.InvokeMethod, null, null, null);
            Console.WriteLine(d);
            Console.ReadLine();
        }

        public class Hoge
        {
            public Hoge(int piyo)
            {
                this.Piyo = piyo;
            }
            public int Piyo { get;set; }
        };

        static private Hoge hoge;
        public static int CallBack()
        {
            return hoge.Piyo;
        }
    }
}

それはそうとC#でEvalが必要となるケースってのが、これといって思い当たらない件。
どーゆーときに利用価値があるんだろうか(^ω^;)