我可以在常规 .NET 类中使用 Razor (或类似的),即获得某种字符串插值吗?
我可以发誓我不久前看到过一些关于 C 语言不完美但有用的字符串插值方法的文章,但现在没有这样的运气。不过,Razor 或多或少可以满足我的需求。
假设您有一个带有票证的数据库客户端应用程序,并且每当创建票证、重要参数更改等时都会发送电子邮件通知。用户希望自定义这些通知电子邮件的措辞邮件,使用字符串插值最简单,即从字符串中访问 ticket
对象的各种属性,如下所示:
亲爱的
@user
,工单
@ticket.ID
(@ticket.URL
) 的优先级已从@previousTicket.priority
更改为@ currentTicket.priority
.
我想要的是一种传递各种对象(在本例中为 user
、oldTicket
和 ticket
)的方法,并让它评估string 并通过反射获取必要的属性。
I could've sworn I saw some articles a while ago about imperfect but useful string interpolation methods for C, but no such luck right now. However, there's Razor, which does more or less what I want.
Suppose you have a database client application with tickets, and e-mail notifications are to be sent whenever tickets get created, significant parameters change, etc. The user would like to customize the wording of those notification e-mails, which would be easiest using string interpolation, i.e. accessing various properties of the ticket
object from within the string, like so:
Dear
@user
,the ticket
@ticket.ID
(@ticket.URL
) has changed in priority from@previousTicket.priority
to@currentTicket.priority
.
What I'd like is a method that I pass various objects (in this case user
, oldTicket
and ticket
), and have it evaluate the string and get the necessary properties through reflection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然我确信有很多引擎可以做到这一点,但我们选择了 Castle NVelocity,它做得非常好。
http://www.castleproject.org/others/nvelocity/usingit.html
它通过名称/值对接受数据,并通过模板运行它。它可用于在内存中生成各种文本输出。它支持包含、条件部分以及重复数据(例如订单上的行)。
最重要的是,它非常容易使用。
While I'm sure there's many engines out there that do this, we settled on Castle NVelocity, and it does it really well.
http://www.castleproject.org/others/nvelocity/usingit.html
It accepts the data via name/value pairs, and runs it through a template. It can be used for generating all kinds of textual output in memory. It supports includes, conditional sections, and also repeating data (eg. lines on an order).
Most importantly, it's damn easy to use.
我没有看到这两个答案,所以我继续做了我自己的实现:
这是一个测试:
它比 Albin 的变体多了很多代码,但不需要手动设置 ID(尽管它仍然需要您提前知道哪些对象要“导出”以进行潜在的插值)。
谢谢你们!
I hadn't seen the two answers, so I went ahead and did my own implementation:
And here's a test:
It's quite a bit more code than Albin's variant, but doesn't require manually setting up IDs (though it still needs you to know ahead of time which objects to 'export' for potential interpolation).
Thanks guys!
另请参阅:http://nuget.org/List/Packages/Expansive
See also: http://nuget.org/List/Packages/Expansive
您可以使用简单的替换步骤来实现简单的关键字替换功能。
只需将关键字替换为
{0}
、{1}
等,并在正确的位置使用带有正确参数的string.Format
即可。这假设您有明确定义且数量有限的关键字。
You can use a simple replacement step to achieve a simple keyword replacement functionality.
Just replace your keywords with
{0}
,{1}
, etc and usestring.Format
with the right parameter in the right place.This assumes that you have a well defined and limited amount of keywords.