文本在所有对象属性上进行转换,有更好的方法吗?
目前我正在这样做:
我的文本看起来像:
Hello ${user.name}, this is ....
我这样做:
public string TransformUser(User user, string text)
{
StringBuilder sb = new StringBuilder(text);
sb.Replace("${user.name}", user.Name);
...
...
return sb.ToString();
}
有没有更好的方法,也许使用反射以某种方式循环遍历类的公共属性?
编辑
是否可以使此方法通用,以便我可以将任何对象传递给它?
Currently I am doing this:
I have text that looks like:
Hello ${user.name}, this is ....
And I do this:
public string TransformUser(User user, string text)
{
StringBuilder sb = new StringBuilder(text);
sb.Replace("${user.name}", user.Name);
...
...
return sb.ToString();
}
Is there a better way, maybe using reflection somehow to loop through the classes public properties?
Edit
Would it be possible to make this method Generic, so I can pass any object to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用反射循环遍历所有属性并替换字符串中的键,大致如下所示:
它使用 CanRead 来检查属性是否具有 getter,然后调用 getter 来读取值。只需使用 ToString 即可将值转换为字符串,这可能适用于原始类型(取决于所需的行为)。这是区分大小写的,因此如果用户使用小写字母编写密钥(如您的示例中所示),您可能需要使用
ToLower
。Looping through all properties using reflection and replacing the keys in the string would look roughly like this:
It uses
CanRead
to check if the property has getter and then invokes the getter to read the value. A value is converted to string simply usingToString
which will probably work for primitive types (depending on the required behavior). This is case sensitive, so you may want to useToLower
if the users write keys using lowercase (as in your example).我编写了一个
StringTemplate
类,可以对其进行修改以满足您的需求...它的行为类似于String.Format
,但有一个主要区别:您可以使用占位符的名称,而不是索引。要格式化的值可以指定为IDictionary
或任何对象(在这种情况下,每个占位符将替换为具有相同名称的属性值)。例如:
如果您需要多次使用同一个模板,您可以创建一个 StringTemplate 实例并重用它以获得更好的性能(模板字符串将仅被解析一次)。
您还可以指定格式修饰符,例如
String.Format
中。为了满足您的确切需求,此类需要进行一些调整,但这应该不会太难...
代码如下:
I wrote a
StringTemplate
class which could probably be modified to suit your needs... It behaves likeString.Format
, with a major difference : you can use names for placeholders, rather than indexes. The values to format can be specified as either aIDictionary<string, object>
, or any object (in that case each placeholder will be replaced with the value of the property with the same name).For instance :
If you need to use the same template several times, you can create an instance of
StringTemplate
and reuse it for better performance (the template string will be parsed only once).You can also specify format modifiers, like in
String.Format
.To fit your exact needs, this class will need a few adjustments, but it shouldn't be too hard...
Here's the code :
您可以通过调用 typeof(User).GetProperties() 来循环访问属性。
You can loop through the properties by calling
typeof(User).GetProperties()
.