显示DateTimeFormat所有属性的代码

一段显示所有 DateTimeFormat 属性值的代码,方便察看

 

C#代码:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List valueList = new List();

        private string GetLevelSpace(int level)
        {
            string result = string.Empty;
            for (int i = 0; i < level; i++)
            {
                result += "  ";
            }
            return result;
        }

        private void ShowPropertyValue(int level, object obj)
        {
            if (valueList.Contains(obj)) return;
            valueList.Add(obj);

            if (obj == null)
            {
                return;
            }
            Type type = obj.GetType();
            PropertyInfo[] pis = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo pi in pis)
            {
                object value = null;
                try
                {
                    value = pi.GetValue(obj, null);
                }
                catch
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": 出错");
                    this.richTextBox1.AppendText("/r/n");
                    continue;
                }
                if (pi.PropertyType.IsValueType || pi.PropertyType == typeof(string))
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": " + value);
                    richTextBox1.AppendText("/r/n");
                }
                else if (pi.PropertyType == typeof(string[]))
                {
                    string[] values = (string[])pi.GetValue(obj, null);
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ": " + values[0]);
                    for (int i = 1; i < values.Length; i++)
                        richTextBox1.AppendText(", " + values[i]);
                    richTextBox1.AppendText("/r/n");
                }
                else
                {
                    richTextBox1.AppendText(GetLevelSpace(level) + pi.Name + ":/r/n");
                    ShowPropertyValue(++level, value);
                }
            }           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";
            valueList.Clear();

            ShowPropertyValue(0, CultureInfo.CurrentCulture.DateTimeFormat);   

            DateTime dt = DateTime.Now;
            richTextBox1.AppendText("/r/n默认日期格式的输出:");
            richTextBox1.AppendText(dt.ToString() + "/r/n");

            CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            Thread.CurrentThread.CurrentCulture = ci;
            if (!CultureInfo.CurrentCulture.DateTimeFormat.IsReadOnly)
            {
                CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy|MM|dd";
                CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern = "H+mm+ss";
                richTextBox1.AppendText("/r/n自定义日期格式的输出:");
                richTextBox1.AppendText(dt.ToString() + "/r/n");
            }
        }
    }
}

 

窗体文件:

namespace WindowsFormsApplication2
{
    partial class Form1
    {
        ///


        /// 必需的设计器变量。
        ///

        private System.ComponentModel.IContainer components = null;

        ///
        /// 清理所有正在使用的资源。
        ///

        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        ///
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        ///

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Dock = System.Windows.Forms.DockStyle.Top;
            this.button1.Location = new System.Drawing.Point(0, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(465, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // richTextBox1
            //
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 23);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(465, 334);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = "";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(465, 357);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.RichTextBox richTextBox1;
    }
}

 

我的显示结果:

AMDesignator: 上午
Calendar:
  MinSupportedDateTime: 0001-01-01 0:00:00
  MaxSupportedDateTime: 9999-12-31 23:59:59
  AlgorithmType: SolarCalendar
  CalendarType: Localized
  Eras:
    Length: 1
    LongLength: 1
    Rank: 1
    SyncRoot:
      IsReadOnly: False
      IsFixedSize: True
      IsSynchronized: False
    TwoDigitYearMax: 2029
    IsReadOnly: True
  DateSeparator: -
  FirstDayOfWeek: Sunday
  CalendarWeekRule: FirstDay
  FullDateTimePattern: yyyy'年'M'月'd'日' H:mm:ss
  LongDatePattern: yyyy'年'M'月'd'日'
  LongTimePattern: H:mm:ss
  MonthDayPattern: M'月'd'日'
  PMDesignator: 下午
  RFC1123Pattern: ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  ShortDatePattern: yyyy-MM-dd
  ShortTimePattern: H:mm
  SortableDateTimePattern: yyyy'-'MM'-'dd'T'HH':'mm':'ss
  TimeSeparator: :
  UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z'
  YearMonthPattern: yyyy'年'M'月'
  AbbreviatedDayNames: 日, 一, 二, 三, 四, 五, 六
  ShortestDayNames: 日, 一, 二, 三, 四, 五, 六
  DayNames: 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六
  AbbreviatedMonthNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  MonthNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  IsReadOnly: True
  NativeCalendarName: 公历
  AbbreviatedMonthGenitiveNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,
  MonthGenitiveNames: 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月,

 

默认日期格式的输出:2010-02-08 14:37:39

自定义日期格式的输出:2010|02|08 14+37+39

你可能感兴趣的:(C#技术)