如何将列表字符串转换为特定对象[关闭]

问题描述 投票:0回答:1

我有一个由 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)?

c# dynamic eval
1个回答
2
投票

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

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

MeuObjeto
的实例。

© www.soinside.com 2019 - 2024. All rights reserved.