• (原创)模板字符串(短信,邮件..) 与 类实例 匹配替换的简单解决方案


    public class TemplateTransferConfig
    {
    public Type ClassType { get; set; }

    /// <summary>
    /// 如果ClassType不为空,则该字段代表字段名.如果为空,则为变量占位符
    /// </summary>
    public String FieldName { get; set; }
    public String Format { get; set; }


    }

    public class Customer
    {
    public int CustomerId { get; set; }
    public String Name { get; set; }
    public string Phone { get; set; }
    }
    public class TreadeRecord
    {
    /// <summary>
    /// 用户Id
    /// </summary>
    public int CustomerId { get; set; }

    /// <summary>
    /// 还款金额
    /// </summary>
    public decimal Amount { get; set; }


    }

    public class TemplateTransfer
    {
    static readonly Dictionary<string, TemplateTransferConfig> _dict = new Dictionary<string, TemplateTransferConfig>();

    static TemplateTransfer()
    {
    var a = new TemplateTransferConfig()
    {
    ClassType = typeof(TreadeRecord),
    FieldName = "Amount",
    Format = "N2"
    };
    _dict.Add("【交易金额】", a);
    a = new TemplateTransferConfig()
    {
    ClassType = typeof(TreadeRecord),
    FieldName = "Amount",
    Format = "N0"
    };
    _dict.Add("【交易金额(整数)】", a);
    a = new TemplateTransferConfig()
    {
    ClassType = null,
    FieldName = "{0}",
    Format = null
    zz };
    _dict.Add("【还款时间】", a);
    a = new TemplateTransferConfig()
    {
    ClassType = typeof(Customer),
    FieldName = "Name",
    Format = null
    };
    _dict.Add("【客户名称】", a);
    a = new TemplateTransferConfig()
    {
    ClassType = typeof(Customer),
    FieldName = "Phone",
    Format = null
    };
    _dict.Add("【客户电话】", a);

    a = new TemplateTransferConfig()
    {
    ClassType = null,
    FieldName = "{1}",
    Format = null
    };
    _dict.Add("【公司签名】", a);


    }
    public static String FormatData(string data, object[] instanceArr, params string[] args)
    {

    foreach (var kv in _dict)
    {
    var key = kv.Key;
    if (data.Contains(key))
    {
    var a = kv.Value;
    if (a.ClassType != null)
    {
    object instance = getInstanceByType(a.ClassType, instanceArr);
    PropertyInfo propertyInfo = a.ClassType.GetProperty(a.FieldName);
    var v = propertyInfo.GetValue(instance);
    MethodInfo method;
    if (String.IsNullOrEmpty(a.Format))
    method = propertyInfo.PropertyType.GetMethod("ToString", new Type[] { });
    else
    method = propertyInfo.PropertyType.GetMethod("ToString", new Type[] { typeof(string) });
    object r;
    if (String.IsNullOrEmpty(a.Format))
    r = method.Invoke(v, new object[] { });
    else
    r = method.Invoke(v, new object[] { a.Format });
    data = data.Replace(key, r.ToString());
    }
    else
    {
    data = data.Replace(key, a.FieldName);
    }

    }

    }
    data = string.Format(data, args);
    return data;
    }

    static object getInstanceByType(Type type, object[] instanceArr)
    {
    foreach (var item in instanceArr)
    {
    if (item.GetType().FullName == type.FullName)
    {
    return item;
    }
    }
    return null;
    }
    }

    class Program
    {



    static void Main(string[] args)
    {
    string rawData = "尊敬的【客户名称】先生/女士,您的账户于【还款时间】收到【交易金额(整数)】元,登录中国银行即可查看账户余额。【【公司签名】】";
    var paymentResult = new TreadeRecord() { Amount = 34341 };
    var customer=new Customer(){Name = "张三哥"};
    string data =TemplateTransfer. FormatData(rawData, new object[] { paymentResult, customer }, DateTime.Now.ToString("yyyy-MM-dd"),"中国银行");
    Console.WriteLine(data);
    Console.Read();
    }
    }

  • 相关阅读:
    sql基础资料
    monkeyrunner自动化测试
    monkey命令
    加密类
    程序生成SiteMapPath文件
    Asp.net 后台调用js方法(转)
    C# 文件粉碎
    【乱搞】【AOJ-149】简易版最长序列
    【乱搞】【AOJ-59】09年3月选拔赛第4题
    关于java 线程池 ThreadPoolExceutor 之 TestDemo
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5485890.html
Copyright © 2020-2023  润新知