HIRASE CONNECTION WK

programming collection

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

Link Collection

[C#] String文字列からLinq実行

Regexのテスターみたいに、Linqのテスターを作ろうと思ったら、StringからLinqする術がわからなかった。

ので、ゴリゴリ手書き。もっと良い方法があればフィードバックプリーズ。

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Microsoft.CSharp;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("====== String文字列からLinq実行。 ======");

        // パート1
        var data1 = new Int32[] { 10, 11, 14, 18, 19, 21, 22, 28 };
        Console.Write("データ1: ");
        foreach (var value in data1) Console.Write(value + ",");
        Console.WriteLine();
        var res1 = LinqFrom<Int32>("from v in data where predicater(v) select v;"
            , "TempNamespace", "TempClass", "TempFunc"
            , data1, delegate(Int32 v) { return v % 2 == 0; });
        Console.Write("データ1の偶数要素: ");
        foreach (var value in res1) Console.Write(value + ",");
        Console.WriteLine();
        Console.WriteLine();

        // パート2
        var data2 = new String[] { "RedHat", "Fedora", "Mac", "Windows XP", "Windows Vista" };
        Console.Write("データ2: ");
        foreach (var value in data2) Console.Write(value + ",");
        Console.WriteLine();
        var res2 = LinqFrom<String>("from v in data where predicater(v) select v;"
            , "TempNamespace", "TempClass", "TempFunc"
            , data2, delegate(String v) { return v.StartsWith("Windows"); });
        Console.Write("データ2のWindows: ");
        foreach (var value in res2) Console.Write(value + ",");
        Console.WriteLine();
    }

    public static IEnumerable<T> LinqFrom<T>(String linqText, String tempNamespace, String tempClassName, String tempFuncName, IEnumerable<T> data, Predicate<T> predicater)
    {
        CodeDomProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateInMemory = true;
        cp.ReferencedAssemblies.Add("System.Core.dll");
        cp.ReferencedAssemblies.Add("System.Xml.Linq.dll");

        String compileSource =
          "using System.Linq;" + System.Environment.NewLine
        + "namespace " + tempNamespace + " {" + System.Environment.NewLine
        + "    public class " + tempClassName + System.Environment.NewLine
        + "    {" + System.Environment.NewLine
        + "        public System.Collections.Generic.IEnumerable<" + typeof(T).FullName + "> " + tempFuncName + "(System.Collections.Generic.IEnumerable<" + typeof(T).FullName + "> data, System.Predicate<" + typeof(T).FullName + "> predicater)" + System.Environment.NewLine
        + "        {" + System.Environment.NewLine
        + "            return " + linqText + System.Environment.NewLine
        + "        }" + System.Environment.NewLine
        + "    }" + System.Environment.NewLine
        + "}" + System.Environment.NewLine;
        CompilerResults cr = provider.CompileAssemblyFromSource(cp, compileSource);
        if (cr.Errors.HasErrors)
        {
            throw new System.Exception("構文エラーらしいよ。");
        }
        else
        {
            Assembly asm = cr.CompiledAssembly;
            Type tempClassType = asm.GetType(tempNamespace + "." + tempClassName);
            Object classInstance = Activator.CreateInstance(tempClassType);
            MethodInfo methodInfo = tempClassType.GetMethod(tempFuncName);
            Object result = methodInfo.Invoke(classInstance, BindingFlags.InvokeMethod, null, new Object[] { data, predicater }, CultureInfo.CurrentCulture);
            return (System.Collections.Generic.IEnumerable<T>)result;
        }
    }
}

投稿日時 : 2008年4月8日 18:17

コメントを追加

# re: [C#] String文字列からLinq実行 2008/04/08 18:27 シャノン

LINQ句丸ごとできるかなぁ…試してません。
http://www.chica2.dev-asp.net/ScottGu/result2.aspx?target=%E5%8B%95%E7%9A%84+LINQ+%EF%BC%88%E3%83%91%E3%83%BC%E3%83%88+1%3A+LINQ+%E5%8B%95%E7%9A%84%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E4%BD%BF%E7%94%A8%EF%BC%89

# re: [C#] String文字列からLinq実行 2008/04/10 20:47 T.Hirase

TO: シャノンさま。
返事遅れまして、すみません(38度ほど、熱を出しておりました・・)。

いまいち試せてないですが、
DynamicLINQとやらは、whereやfromをまるごと含むことはできなさそうです。

# SwxrrrvFDQSUVNGlZnn 2011/12/17 19:37 http://www.neojogos.com

Last a few years has been to Ibiza, so met a person there whose style of presentation is very similar to yours. But, unfortunately, that person is too far from the Internet!...

# DpUIuFyyYaP 2011/12/22 22:05 http://www.discreetpharmacist.com/ita/index.asp

Sent the first post, but it wasn`t published. I am writing the second. It's me, the African tourist.

# rIOVMDQRBzSkLc 2015/04/30 5:38 chaba

f6X7fK http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# oKHQXQObjrt 2015/05/19 22:51 Matthew

Looking for work http://www.ctahperd.org/about-us.html erexin v opinie forum "They have deferred it to December after we agreed to someof their demands relating to VRS (voluntary retirement schemes)and other issues," Personnel & Industrial Relations Director RMohan Das said by telephone from Kolkata. "The divestment shouldgo through now."

タイトル
名前
URL
コメント