c#构造函数对string类型赋初值
2017-12-01 09:41
253 查看
1 public class Stu 2 { 3 public Stu() 4 { 5 //当成员属性非常多难以一一赋值时,采用本方法。当然写代码逐一成员直接赋值效率更高。 6 AssignEmptyStringMemberProperties(); 7 } 8 9 /// <summary> 10 /// string类型成员赋空值(string.Empty) 11 /// 类似还可以写出:对int、datetime等处理 12 /// </summary> 13 public void AssignEmptyStringMemberProperties() 14 { 15 foreach (var property in this.GetType().GetProperties()) 16 { 17 if (property.PropertyType == typeof(string)) 18 { 19 property.SetValue(this, string.Empty, null); 20 } 21 } 22 } 23 24 public long Id { get; set; } 25 26 public string Name { get; set; } 27 28 public string Sno { get; set; } 29 30 public string Mobile { get; set; } 31 32 public string IdCard { get; set; } 33 34 public string Remark1 { get; set; } 35 public string Remark2 { get; set; } 36 public string Remark3 { get; set; } 37 public string Remark4 { get; set; } 38 public string Remark5 { get; set; } 39 }
主要就是利用反射获取本类中的所有属性,若是string类型则赋初值
相关文章推荐
- String类型赋初值问题
- Java中String类型变量初值为null时的连接问题
- string类型的初值不是随便赋的,记一次奇怪的访问冲突事件
- Java中String类型变量初值为null时的连接问题
- C++中 int、string等类型转换方法
- Java 之 String 类型
- Swift的String类型转换如何实现?
- C++中各种基本数据类型转换为string类型
- java中数据类型转换 Integer String Long Float Double Date
- JAVA中int、String的类型转换
- C# 中WebService返回类型(string,int,bool,DataSet,class实体类)示例
- Swift- String和Int类型互换
- 三探String类型
- MyBatis: SQL语句中传入单个String类型的参数的获取
- Redis数据类型--string
- 二十一、标准库类型string的使用、string类介绍、string对象的构造和初始化、string常用成员函数
- string类型的查找
- iOS中String 转为char类型
- 从键盘输入任意一个String类型的字符串 将其进行字符串反串
- 如何将 String类型 转换为日期格式 Date类型