zoukankan      html  css  js  c++  java
  • c#构造函数对string类型赋初值

     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类型则赋初值

  • 相关阅读:
    CUDA编程学习(一)
    数据挖掘领域的十大经典算法
    MATLAB代码加密生成.p文件
    如何读入文件下的图像序列
    什么是co-training
    Addthis使用
    html5新增及删除标签
    html5语法
    设置SecureCRT配色和解决乱码问题
    html5新增及废除属性
  • 原文地址:https://www.cnblogs.com/nlh774/p/7940459.html
Copyright © 2011-2022 走看看