用类属性值替换所有不同的占位符
我有下面的课程
public class Details
{
public string CreatedAt {set;get;)
public Order Order { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public string Name { get; set; }
public CustomerAddress Address { get; set; }
}
public class CustomerAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
}
,并且我的HTML文件具有HTML内容,很少有占位符。我正在以下面的方式替换占位符。
public static string ReplaceStringPlaceHolders(User Details)
{
string MyHTML= File.ReadAllText(@"...Path");
//Replacing one by one
string newstring= MyHTML.
.Replace("{created_at}", Details.CreatedAt)
.Replace("{customer_info.address.line_1}", Details.Customer.Address.Line1)
.Replace("{customer_info.address.line_2}", Details.Customer.Address.Line2)
.Replace("{customer_info.address.city}", Details.Customer.Address.City)
.Replace("{customer_info.address.state}", Details.Customer.Address.State)
.Replace("{customer_info.address.postal_code}", Details.Customer.Address.PostalCode)
.Replace("{customer_info.address.country}", Details.Customer.Address.Country)
return newstring;
}
但是我不喜欢这种方式,因为我将50多个占位符放入HTML文件中。 有什么方法可以在占位符名称与类属性匹配时替换占位符。
如果可能的话,我正在寻找这样的东西:
MyHTML.replaceifPlaceHolderMatchesWithClassProperties(Label);
请建议。
I have a class as below
public class Details
{
public string CreatedAt {set;get;)
public Order Order { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public string Name { get; set; }
public CustomerAddress Address { get; set; }
}
public class CustomerAddress
{
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public string State { get; set; }
}
and I have HTML file with HTML content and few placeholder. I am replacing placeholders as below.
public static string ReplaceStringPlaceHolders(User Details)
{
string MyHTML= File.ReadAllText(@"...Path");
//Replacing one by one
string newstring= MyHTML.
.Replace("{created_at}", Details.CreatedAt)
.Replace("{customer_info.address.line_1}", Details.Customer.Address.Line1)
.Replace("{customer_info.address.line_2}", Details.Customer.Address.Line2)
.Replace("{customer_info.address.city}", Details.Customer.Address.City)
.Replace("{customer_info.address.state}", Details.Customer.Address.State)
.Replace("{customer_info.address.postal_code}", Details.Customer.Address.PostalCode)
.Replace("{customer_info.address.country}", Details.Customer.Address.Country)
return newstring;
}
but I don't like this way as I have put 50+ placeholders in my HTML file.
Is there a way that we can replace the placeholder when the placeholder name matches to class properties.
I am looking for something like this if possible:
MyHTML.replaceifPlaceHolderMatchesWithClassProperties(Label);
Kindly suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这会帮助您:
输出:
0,您好,World,15
i think this will help you:
output:
0, hello, world, 15
是的,您可以在 Reflection 和 linq 的帮助下阅读属性:
并在正则表达式的帮助下匹配占位符:
用法:
我假设在这里我们
_
在占位符中,例如{create_at}
对应于create> create> createat
属性{create_at} == {create_at} == {create_at}
等。{字母,数字,_,。}中
格式Yes, you can read properties with a help of Reflection and Linq:
And match placeholders with a help of regular expressions:
Usage:
Here I assumed that
_
in placeholders, e.g{created_at}
corresponds toCreatedAt
property{created_at} == {Created_At} == {CREATED_at}
etc.{letters,digits,_,.}
format