(original) Template string (sms, mail ...) A simple solution to replace a class instance match

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.