public class Templatetransferconfig
{
Public Type ClassType {get; set;}
<summary>
If ClassType is not empty, the field represents the field name. If empty, the variable placeholder
</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>
User ID
</summary>
public int CustomerId {get; set;}
<summary>
Repayment amount
</summary>
Public decimal Amount {get; set;}
}
Public class Templatetransfer
{
static readonly dictionary<string, templatetransferconfig> _dict = new D Ictionary<string, templatetransferconfig> ();
Static Templatetransfer ()
{
var a = new Templatetransferconfig ()
{
ClassType = typeof (Treaderecord),
FieldName = "Amount",
Format = "N2"
};
_dict. ADD ("Transaction Amount", "a");
A = new Templatetransferconfig ()
{
ClassType = typeof (Treaderecord),
FieldName = "Amount",
Format = "N0"
};
_dict. ADD ("Transaction amount (integer)" ", a);
A = new Templatetransferconfig ()
{
ClassType = NULL,
FieldName = "{0}",
Format = null
ZZ};
_dict. ADD ("Repayment Time", "a");
A = new Templatetransferconfig ()
{
ClassType = typeof (Customer),
FieldName = "Name",
Format = null
};
_dict. ADD ("Customer Name", "a");
A = new Templatetransferconfig ()
{
ClassType = typeof (Customer),
FieldName = "Phone",
Format = null
};
_dict. ADD ("Customer Phone", "a");
A = new Templatetransferconfig ()
{
ClassType = NULL,
FieldName = "{1}",
Format = null
};
_dict. ADD ("Company Signature", "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 = "Dear Customer Name" sir/Madam, your account received "Transaction amount (whole number)" in "Repayment Time", log in to Bank of China to view account balance. "Company Signature" ";
var paymentresult = new Treaderecord () {Amount = 34341};
var customer=new customer () {Name = "Zhang San"};
String Data =templatetransfer. Formatdata (RawData, new object[] {paymentresult, customer}, DateTime.Now.ToString ("Yyyy-mm-dd"), "Bank of China");
Console.WriteLine (data);
Console.read ();
}
}
(original) Template string (sms, mail ...) A simple solution to replace a class instance match