Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C中的常量类创建字典#_C#_Dictionary - Fatal编程技术网

C# 用C中的常量类创建字典#

C# 用C中的常量类创建字典#,c#,dictionary,C#,Dictionary,我有一个包含几个字符串常量的类: public class TemplateConstants { public static readonly string Type = "«Type»"; public static readonly string Purpose = "«Purpose»"; public static readonly string FirstName = "«FirstName»"; .... } 每一个都代表一个“占位符”。我有一个

我有一个包含几个字符串常量的类:

 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);
.....

是否有一种方法可以在这里使用字典,这样我就可以以某种方式循环文件,并根据字典进行替换(而不是一次一个替换)?

使用Razor view engine编程,如本例所示: (nuget RazorEngine)

当然,您的模板应该使用Razor语法 比如:


...
@模式.目的
@Model.FirstName
...

另请参见:

我将静态字符串转换为常量,然后添加了一个将常量转换为字典的方法

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);
    }
}
公共类TemplateConstants
{
public const string Type=“«Type»”;
公用常量字符串Purpose=“«Purpose»”;
public const string FirstName=“«FirstName»”;
私有静态只读字典=
新懒惰(CreateDictionary);
公共静态字典AsDictionary()
{
返回ConstantsDictionary.Value;
}
私有静态字典CreateDictionary()
{
变量字段=类型(TemplateConstants)
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Flatterhierarchy);
var常量=字段。其中(f=>f.IsLiteral&&!f.IsInitOnly)。ToList();
var result=newdictionary();
var instance=new TemplateConstants();
//如果您想进一步过滤常量以仅包含这些常量,则由您决定
//使用字符串值。
constants.ForEach(constant=>result.Add(constant.Name,constant.GetValue(instance.ToString()));
返回结果;
}
}
[测试类]
公共类TestTemplateConstants
{
[测试方法]
公共void TestDictionary()
{
var dictionary=TemplateConstants.AsDictionary();
AreEqual(dictionary[“Type”],TemplateConstants.Type);
}
}

在C#中不是这样做的字符串常量-你应该用“const”替换“static readonly”。只读字符串与const字符串不同-区别在于readonly是一个运行时常量,const是一个编译时常量,因此如果你使用const,编译器可以静态地进行替换。nakisa,如果可能的话,不要去查字典。试着用一个能做艰苦工作的工作。请参见下面的答案。这会节省你很多时间。我同意阿米尔的看法。您试图做的是HTML模板,并且有专门为此编写的整个库。它们解决了您甚至还没有考虑过的问题(例如,如果有人将恶意HTML和JavaScript注入到您的应用程序的名字字段中会发生什么?@StriplingWarrior它将是内部应用程序,因此不太担心安全性。另一件事是它是Web API,所以Rasor视图的答案不会是work@nakisa:当然可以。提供的示例Amir是一个控制台应用程序,而不是MVC。Razor引擎可以作为通用HTML模板引擎调用:它不必是HTTP请求的一部分或任何内容。安全性并不是像Razor这样的引擎所解决的唯一问题——这只是一个例子。顺便说一句——我同意其他评论,即使用字典可能不是你想要做的事情的方式。我只是逐字回答这个问题。在某些情况下,这样做可能很有用。
<html>
...
<div>@Model.Purpose</div>
<div>@Model.FirstName</div>
...
</html>
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);
    }
}