在C#

问题描述:

创建字典出常量类的我都包含若干字符串常量类:在C#

public class TemplateConstants 
    { 
    public static readonly string Type = "«Type»"; 
    public static readonly string Purpose = "«Purpose»"; 
    public static readonly string FirstName = "«FirstName»"; 
.... 
} 

每一个都代表“占位”。我有一个包含各种占位符的HTML文档。在我的Web API控制器(C#)我更换这些占位符一次一个:

string doc = string.Empty; 
string htmltest = System.IO.File.ReadAllText  
(HttpContext.Current.Server.MapPath(@"~\Templates\Template.html")); 
doc = htmltest.Replace(TemplateConstants.ApplicationType, applicationType); 
     doc = doc.Replace(TemplateConstants.LoanPurpose, loanType); 
     doc = doc.Replace(ApplicationDocTemplateConstants.BorrowerFirstName, FirstName); 
..... 

有没有在这里使用一个字典,而不是让我可以根据通过某种方式的文件回路和做换人的方式字典(而不是一次只做一个替换)?

+0

这不是你如何在C#中做一个字符串常量 - 你应该用“const”替换“static readonly”。只读字符串不是**,与常量字符串相同 - 区别在于readonly是运行时常量,const是编译时常量,所以如果使用const,编译器可以静态地进行替换。 – EJoshuaS

+1

nakisa,如果可能的话,不要去字典解决方案。尝试使用可以完成艰苦工作的工作。 请参阅下面的Razor答案。它会为你节省很多时间。 –

+1

我同意阿米尔。你想要做的是HTML模板,并且有为此特定目的而编写的整个库。他们解决了你还没有考虑过的问题(比如,如果有人将恶意HTML和JavaScript注入到应用程序的名字字段中会发生什么情况?) – StriplingWarrior

使用Razor视图引擎programically就像这个例子: http://razorengine.codeplex.com/ (的NuGet RazorEngine)

using RazorEngine; 
using RazorEngine.Templating; // For extension methods. 

class Program 
{ 
    static void Main(string[] args) 
    { 
     string template = "Hello @Model.Types, welcome to RazorEngine!"; 
     var result = 
      Engine.Razor.RunCompile(template, "templateKey", null, new TemplateConstants()); 

     Console.WriteLine(result); 
    } 
} 

public class TemplateConstants 
{ 
    public readonly string Types = "Some type"; 
    public readonly string Purpose = "«Purpose»"; 
    public readonly string FirstName = "«FirstName»"; 

} 
当然您的模板应该是在剃刀语法 喜欢的

<html> 
... 
<div>@Model.Purpose</div> 
<div>@Model.FirstName</div> 
... 
</html> 

另见: https://github.com/Antaris/RazorEngine

我将你的静态s转到常量,然后添加一个将常量转换为字典的方法。

public class TemplateConstants 
{ 
    public const string Type = "«Type»"; 
    public const string Purpose = "«Purpose»"; 
    public const string FirstName = "«FirstName»"; 

    private static readonly Lazy<Dictionary<string, string>> ConstantsDictionary = 
     new Lazy<Dictionary<string, string>>(CreateDictionary); 

    public static Dictionary<string, string> AsDictionary() 
    { 
     return ConstantsDictionary.Value; 
    } 

    private static Dictionary<string, string> CreateDictionary() 
    { 
     var fields = typeof(TemplateConstants) 
      .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); 

     var constants = fields.Where(f => f.IsLiteral && !f.IsInitOnly).ToList(); 
     var result = new Dictionary<string, string>(); 
     var instance = new TemplateConstants(); 

     // Up to you if you want to filter the constants further to only include those 
     // with string values. 
     constants.ForEach(constant => result.Add(constant.Name, constant.GetValue(instance).ToString())); 
     return result; 
    } 
} 

[TestClass] 
public class TestTemplateConstants 
{ 
    [TestMethod] 
    public void TestDictionary() 
    { 
     var dictionary = TemplateConstants.AsDictionary(); 
     Assert.AreEqual(dictionary["Type"],TemplateConstants.Type); 
    } 
} 
+2

顺便说一句 - 我同意其他意见,使用字典可能不是做你想做的事。我只是从字面上回答这个问题。有些情况下可能会有用。 –