如何将列表字符串转换为特定对象

时间:2013-11-02 02:14:08

标签: c# dynamic compiler-construction eval

我有一个由javascript发送的字符串列表。

List<string> options = new List<string>()
        {
            {"new MeuObjeto(\"teste 1\", 10, 12, 50, 70, 0, new System.Drawing.Font(\"Arial\", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"},
            {"new MeuObjeto(\"teste 2\", 10, 14, 50, 70, 0, new System.Drawing.Font(\"Arial\", 14, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"},
            {"new MeuObjeto(\"teste 3\", 10, 16, 50, 70, 0, new System.Drawing.Font(\"Arial\", 35, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"},
            {"new MeuObjeto(\"teste 4\", 10, 18, 50, 70, 0, new System.Drawing.Font(\"Arial\", 10, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"},
            {"new MeuObjeto(\"teste 5\", 10, 20, 50, 70, 0, new System.Drawing.Font(\"Arial\", 9, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"},
            {"new MeuObjeto(\"teste 6\", 10, 22, 50, 70, 0, new System.Drawing.Font(\"Arial\", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point), System.Drawing.Color.Black, false)"}
        };

如何将此数据转换为对象(MeuObjeto)?

1 个答案:

答案 0 :(得分:2)

可以从这样的字符串编译和执行C#代码。你绝对不想这样做,因为它会打开服务器中最大的安全漏洞,因为它会尽职尽责地编译并执行发送给它的任何东西。

相反,您应该只发送数据并编写代码以从该数据创建MeuObjeto的实例。