[英]Nvelocity - Create a random number
在一個項目中,我只需要在頁面中使用NVelocity生成一個隨機數,而無需編輯C#背后的代碼。 我是NVelocity的新手,已經遍歷了所有Interweb,但找不到答案。
任何幫助表示贊賞。
注意:標記為可能重復問題的用戶。 我試圖改編該答案中列出的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());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.