文本在所有对象属性上进行转换,有更好的方法吗?

发布于 2024-08-24 11:56:07 字数 427 浏览 1 评论 0原文

目前我正在这样做:

我的文本看起来像:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一直在等你来 2024-08-31 11:56:07

使用反射循环遍历所有属性并替换字符串中的键,大致如下所示:

var args = new object[0];
foreach(var prop in typeof(User).GetProperties()) {
  if (prop.CanRead) {
    string val = prop.GetGetMethod().Invoke(user, args).ToString();
    sb.Replace("${user." + prop.Name +"}", val);
  }
}

它使用 CanRead 来检查属性是否具有 getter,然后调用 getter 来读取值。只需使用 ToString 即可将值转换为字符串,这可能适用于原始类型(取决于所需的行为)。这是区分大小写的,因此如果用户使用小写字母编写密钥(如您的示例中所示),您可能需要使用 ToLower

Looping through all properties using reflection and replacing the keys in the string would look roughly like this:

var args = new object[0];
foreach(var prop in typeof(User).GetProperties()) {
  if (prop.CanRead) {
    string val = prop.GetGetMethod().Invoke(user, args).ToString();
    sb.Replace("${user." + prop.Name +"}", val);
  }
}

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 using ToString which will probably work for primitive types (depending on the required behavior). This is case sensitive, so you may want to use ToLower if the users write keys using lowercase (as in your example).

別甾虛僞 2024-08-31 11:56:07

我编写了一个 StringTemplate 类,可以对其进行修改以满足您的需求...它的行为类似于 String.Format,但有一个主要区别:您可以使用占位符的名称,而不是索引。要格式化的值可以指定为 IDictionary 或任何对象(在这种情况下,每个占位符将替换为具有相同名称的属性值)。

例如:

// with a dictionary :
var values = new Dictionary<string, object>
{
    { "Title", "Mr." },
    { "LastName", "Smith" }
};
string a = StringTemplate.Format("Hello {Title} {LastName}", values);

// with an anonymous type :
string b = StringTemplate.Format(
    "Hello {Title} {LastName}",
     new { Title = "Mr.", LastName = "Smith" });

如果您需要多次使用同一个模板,您可以创建一个 StringTemplate 实例并重用它以获得更好的性能(模板字符串将仅被解析一次)。

您还可以指定格式修饰符,例如 String.Format 中。

为了满足您的确切需求,此类需要进行一些调整,但这应该不会太难...


代码如下:

public class StringTemplate
{
    private string _template;
    private static Regex _regex = new Regex(@"(?<open>{+)(?<key>\w+)(?<format>:[^}]+)?(?<close>}+)", RegexOptions.Compiled);

    public StringTemplate(string template)
    {
        template.CheckArgumentNull("template");
        this._template = template;
        ParseTemplate();
    }

    private string _templateWithIndexes;
    private List<string> _placeholders;

    private void ParseTemplate()
    {
        _placeholders = new List<string>();
        MatchEvaluator evaluator = (m) =>
        {
            if (m.Success)
            {
                string open = m.Groups["open"].Value;
                string close = m.Groups["close"].Value;
                string key = m.Groups["key"].Value;
                string format = m.Groups["format"].Value;

                if (open.Length % 2 == 0)
                    return m.Value;

                open = RemoveLastChar(open);
                close = RemoveLastChar(close);

                if (!_placeholders.Contains(key))
                {
                    _placeholders.Add(key);
                }

                int index = _placeholders.IndexOf(key);
                return string.Format("{0}{{{1}{2}}}{3}", open, index, format, close);
            }
            return m.Value;
        };
        _templateWithIndexes = _regex.Replace(_template, evaluator);
    }

    private string RemoveLastChar(string str)
    {
        if (str.Length > 1)
            return str.Substring(0, str.Length - 1);
        else
            return string.Empty;
    }

    public static implicit operator StringTemplate(string s)
    {
        return new StringTemplate(s);
    }

    public override string ToString()
    {
        return _template;
    }

    public string Format(IDictionary<string, object> values)
    {
        values.CheckArgumentNull("values");

        object[] array = new object[_placeholders.Count];
        for(int i = 0; i < _placeholders.Count; i++)
        {
            string key = _placeholders[i];
            object value;
            if (!values.TryGetValue(key, out value))
            {
                value = string.Format("{{{0}}}", key);
            }
            array[i] = value;
        }
        return string.Format(_templateWithIndexes, array);
    }

    private IDictionary<string, object> MakeDictionary(object obj)
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        Type type = obj.GetType();
        foreach (string propName in _placeholders)
        {
            var prop = type.GetProperty(propName);
            if (prop != null)
                dict.Add(propName, prop.GetValue(obj, null));
        }
        return dict;
    }

    public string Format(object values)
    {
        return Format(MakeDictionary(values));
    }

    public static string Format(string template, IDictionary<string, object> values)
    {
        return new StringTemplate(template).Format(values);
    }

    public static string Format(string template, object values)
    {
        return new StringTemplate(template).Format(values);
    }
}

I wrote a StringTemplate class which could probably be modified to suit your needs... It behaves like String.Format, with a major difference : you can use names for placeholders, rather than indexes. The values to format can be specified as either a IDictionary<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 :

// with a dictionary :
var values = new Dictionary<string, object>
{
    { "Title", "Mr." },
    { "LastName", "Smith" }
};
string a = StringTemplate.Format("Hello {Title} {LastName}", values);

// with an anonymous type :
string b = StringTemplate.Format(
    "Hello {Title} {LastName}",
     new { Title = "Mr.", LastName = "Smith" });

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 :

public class StringTemplate
{
    private string _template;
    private static Regex _regex = new Regex(@"(?<open>{+)(?<key>\w+)(?<format>:[^}]+)?(?<close>}+)", RegexOptions.Compiled);

    public StringTemplate(string template)
    {
        template.CheckArgumentNull("template");
        this._template = template;
        ParseTemplate();
    }

    private string _templateWithIndexes;
    private List<string> _placeholders;

    private void ParseTemplate()
    {
        _placeholders = new List<string>();
        MatchEvaluator evaluator = (m) =>
        {
            if (m.Success)
            {
                string open = m.Groups["open"].Value;
                string close = m.Groups["close"].Value;
                string key = m.Groups["key"].Value;
                string format = m.Groups["format"].Value;

                if (open.Length % 2 == 0)
                    return m.Value;

                open = RemoveLastChar(open);
                close = RemoveLastChar(close);

                if (!_placeholders.Contains(key))
                {
                    _placeholders.Add(key);
                }

                int index = _placeholders.IndexOf(key);
                return string.Format("{0}{{{1}{2}}}{3}", open, index, format, close);
            }
            return m.Value;
        };
        _templateWithIndexes = _regex.Replace(_template, evaluator);
    }

    private string RemoveLastChar(string str)
    {
        if (str.Length > 1)
            return str.Substring(0, str.Length - 1);
        else
            return string.Empty;
    }

    public static implicit operator StringTemplate(string s)
    {
        return new StringTemplate(s);
    }

    public override string ToString()
    {
        return _template;
    }

    public string Format(IDictionary<string, object> values)
    {
        values.CheckArgumentNull("values");

        object[] array = new object[_placeholders.Count];
        for(int i = 0; i < _placeholders.Count; i++)
        {
            string key = _placeholders[i];
            object value;
            if (!values.TryGetValue(key, out value))
            {
                value = string.Format("{{{0}}}", key);
            }
            array[i] = value;
        }
        return string.Format(_templateWithIndexes, array);
    }

    private IDictionary<string, object> MakeDictionary(object obj)
    {
        Dictionary<string, object> dict = new Dictionary<string, object>();
        Type type = obj.GetType();
        foreach (string propName in _placeholders)
        {
            var prop = type.GetProperty(propName);
            if (prop != null)
                dict.Add(propName, prop.GetValue(obj, null));
        }
        return dict;
    }

    public string Format(object values)
    {
        return Format(MakeDictionary(values));
    }

    public static string Format(string template, IDictionary<string, object> values)
    {
        return new StringTemplate(template).Format(values);
    }

    public static string Format(string template, object values)
    {
        return new StringTemplate(template).Format(values);
    }
}
以歌曲疗慰 2024-08-31 11:56:07

您可以通过调用 typeof(User).GetProperties() 来循环访问属性。

You can loop through the properties by calling typeof(User).GetProperties().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文