asp.net reflection Reduced code writing _ Practical Tips

Source: Internet
Author: User

Copy Code code as follows:

public bool Add (Liuyan Refmodel)
{
String sql = "INSERT into Liuyan (name,phone,zhiwei,gongsi,addr,country,dianyou,content,adddate) VALUES (@name, @phone, @zhiwei, @gongsi, @addr, @country, @dianyou, @content, @adddate) ";
oledbparameter[] Parameters ={
New OleDbParameter ("@name", OleDbType.VarChar),
New OleDbParameter ("@tel", OleDbType.VarChar),
New OleDbParameter ("@zhiwei", OleDbType.VarChar),
New OleDbParameter ("@gongsi", OleDbType.VarChar),
New OleDbParameter ("@addr", OleDbType.VarChar),
New OleDbParameter ("@country", OleDbType.VarChar),
New OleDbParameter ("@dianyou", OleDbType.VarChar),
New OleDbParameter ("@content", OleDbType.VarChar),
New OleDbParameter ("@adddate", OleDbType.VarChar)
};
Parameters[0]. Value=model. xingming;
PARAMETERS[1]. Value=model. Tel;
PARAMETERS[2]. Value=model. Zhiwei;
PARAMETERS[3]. Value=model. Gongsi;
PARAMETERS[4]. Value=model. ADDR;
PARAMETERS[5]. Value=model. Country;
PARAMETERS[6]. Value=model. Dianyou;
PARAMETERS[7]. Value=model. Content;
PARAMETERS[8]. Value=model. Adddate;
return Sqlhelper.executecommand (SQL, parameters);
}

This article speaks of reflection now say where in this piece of code the use of reflection is only in my coding process in order to Jingxiang whim
Copy Code code as follows:

Parameters[0]. Value=model. xingming;
PARAMETERS[1]. Value=model. Tel;
PARAMETERS[2]. Value=model. Zhiwei;
PARAMETERS[3]. Value=model. Gongsi;
PARAMETERS[4]. Value=model. ADDR;
PARAMETERS[5]. Value=model. Country;
PARAMETERS[6]. Value=model. Dianyou;
PARAMETERS[7]. Value=model. Content;
PARAMETERS[8]. Value=model. Adddate;

This code can use reflection to assign values when there are many attributes in the class.
  

 
Copy Code code as follows:
    
Use reflection to remember to reference a using System.Reflection namespace

Type t = Refmodel. GetType ();
for (int i = 0; i < t.getproperties (). Length; i++)
{
Parameters[i]. Value= t.getproperties () [i]. GetValue (Refmodel, NULL);
}


This uses reflection to get all the properties of the class
It then traverses the attribute and assigns the property value to the parameter
Of course here is the added method without model. Id
So the For loop starts at 1.
Then Parameters[i]. When value is assigned I want to subtract a parameters[i-1]. Value
So that when you write the data layer in the future, assign a value to the parameter without forcing a copy and paste
What about personal tips and sharing?
Using the Code generation tool can be ignored.

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.