0
在一个项目上工作,我只需要在页面中使用NVelocity生成一个随机数,而不需要编辑C#后面的代码。我是NVelocity的新手,我查看了所有的网页,但找不到答案。Nvelocity - 创建一个随机数
任何帮助表示赞赏。
注意:标记为可能重复问题的用户。我试图将该答案中列出的Velcocity/Java解决方案适应NVelocity/C#而没有运气。我假设答案会有所不同。
在一个项目上工作,我只需要在页面中使用NVelocity生成一个随机数,而不需要编辑C#后面的代码。我是NVelocity的新手,我查看了所有的网页,但找不到答案。Nvelocity - 创建一个随机数
任何帮助表示赞赏。
注意:标记为可能重复问题的用户。我试图将该答案中列出的Velcocity/Java解决方案适应NVelocity/C#而没有运气。我假设答案会有所不同。
NVelocity不具备生成伪随机数的内置支持,但是如果您创建新的System.Random
并将其添加到VelocityContext
,则可以从模板访问它。
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.Init();
VelocityContext context = new VelocityContext();
context.Put("random", new Random());
using (StringWriter sw = new StringWriter())
{
for (int i = 0; i < 5; i++)
{
velocityEngine.Evaluate(context, sw, "", "$random.Next(1, 100)\n");
}
Console.WriteLine(sw.ToString());
}
非常感谢! ! – GeekInTheBox
[速度 - 随机数生成的模板]的可能的复制(http://stackoverflow.com/questions/24758937/velocity-random-number-generation-in-a-template) –