roslyn 可以生成枚举吗?
所以,我觉得这是可能的,但我没有能力把它放在一起。我有查找表(带有 ID 和名称字段)。我有与查找保持同步的枚举。我想要的是创建一个 SQL 表列表,并让 roslyn 在编译期间/之前创建枚举。这样我就知道我的枚举总是同步的,这是我要做的另一件事。
有人可以给我指路,或者告诉我为什么这是一个坏主意吗?我觉得解决方案并不是很多代码......
谢谢!
So, I feel like it's possible, but i don't have the chops to put it together. I have lookup tables (with ID and Name fields). I have enums that I keep in sync with the lookups. What I'd like is to make a list of SQL tables, and have roslyn create the enums during/before compile. this way I know my enums are always in sync, and it's one more thing off my plate.
Could someone out there show me the way, or tell me why it would be a bad idea? I feel like the solution isn't really a lot of code...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是使用 roslyn 创建枚举的方式;
这返回:
现在回到你的问题,我不知道这是否是你真正需要的。
首先,我认为还没有任何内置功能可以在预编译时执行此操作,因此您需要创建一个控制台 exe 并在预构建事件中调用它。
因此,此时可能最好通过代码生成手动执行此操作,而不是在每次构建时执行此操作,除非这些表更改非常频繁。
在这种情况下,您实际上并不需要 roslyn 为您做这件事,您可以使用任何代码生成软件将其吐出来,或者推出您自己的......
This is how you create enums with roslyn;
this returns:
Now to your question, I don't know if this is what you really need.
First I think there is not yet any built in functionality to do this at precompile, so you'd need to create a console exe and call it in your prebuild events.
So at this point probably it is a better idea to do this manually via code generation and not on every build, unless these tables are changing very frequently.
An in this case you don't really need roslyn to do it for you, you can just spit it out with any code generation software, or roll your own...
以下是针对当前版本的 Roslyn 更新的一些代码,还显示了向枚举项添加属性和值。
添加属性的语法有点尴尬,但我发现我经常需要添加一些属性。您可能已经拥有驱动代码生成的查找表中的属性和值的数据。
Here is some code updated for the current version of Roslyn and also shows adding attributes and values to the enum items.
The syntax for adding the attributes gets a little awkward but I find that I often need some attributes added. You probably already have the data for the attributes and values in the lookup tables driving the code generation.
塞巴斯蒂安 6 年前提供的答案可能曾经有效,但现在不再有效。
自 2017 年 12 月 18 日起完成相同任务的一组新示例代码
The Answer provided by Sebastian 6 years ago likely used to work but no longer does.
New set of sample code to accomplish the same thing as of 18/12/2017