在 Visual Studio 中设计时反映并加载代码
我有一个 XML 文件,其中列出了一系列项目,并且这些项目通常在代码中通过其名称进行引用。
有没有什么方法可以在 Visual Studio 中使用反射来使这个列表像智能一样“可访问”?我以前见过这样做的——而且我很确定这非常困难,但我认为至少问一下也没什么坏处。
I have an XML file that lists a series of items, and the items are often referenced by their name in code.
Is there any way to use reflection within Visual Studio to make this list 'accessible' sort of like intellisence? I've seen it done before - and I'm pretty sure it's ridiculously difficult, but I figure it can't hurt to at least ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要为此使用反射。
除了增加代码的复杂性之外,您还会开放代码以供其他人滥用,修改您的 XML 以使您的代码执行他们想要的操作(例如注入攻击)。
您最好像往常一样解析 XML,但使用一个大的 if / switch 语句来定义代码的运行方式。这样您就有更多机会发现任何问题并验证输入。
从字符串到函数调用听起来不错,但会让你感到困惑。
I would recommend against using reflection for this.
Apart from the added complexity in the code you are also opening the code up to abuse from somebody modifying your XML to get your code to do what they want (think injection attack).
You would be better off parsing the XML as usual but using a big if / switch statement to define what how the code runs. That way you have more chance of catching any problems and validating the input.
From string to function call sounds great but will bite you in the bum.
我认为他想用智能从 C# 代码访问 xml。
我的猜测是,您必须构建某种代码生成器来生成具有 xml 字段属性的 C# 类...类似于 Visual Studio 为资源文件生成代码的方式。
I think he wants to access xml from c# code with intelligences.
My guess is that you would have to build some sort of code generator that would generate c# class that has the properties of you xml field... kind of how visual studio generates code for resourcefiles.