基本信息
源码名称:C# 通过反射修改私有字段
源码大小:0.03M
文件格式:.zip
开发语言:C#
更新时间:2016-04-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

通过反射修改私有字段


using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //输出未修改前本地化设置的ToString结果
            Console.WriteLine(DateTime.Now);

            //还真没测出DateTimeFormatInfo.CurrentInfo是NULL的情况,但是预防一下
            if (DateTimeFormatInfo.CurrentInfo != null)
            {
                var type = DateTimeFormatInfo.CurrentInfo.GetType();
                var field = type.GetField("generalLongTimePattern", BindingFlags.NonPublic | BindingFlags.Instance);
                //我怂!!
                if (field != null)
                    field.SetValue(DateTimeFormatInfo.CurrentInfo, "yyyy-MM-dd HH:mm:ss");
            }

            //输出修改后的结果
            Console.WriteLine(DateTime.Now);


            //修改默认时间ToString格式的的另一种方式 [多线程失效]
            //Thread.CurrentThread.CurrentCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            //DateTimeFormatInfo.CurrentInfo.ShortDatePattern = "yyyy-MM-dd";
            //DateTimeFormatInfo.CurrentInfo.LongTimePattern = "HH:mm:ss";

            //Console.WriteLine(DateTime.Now.ToString());

            //var thread = new Thread(() => Console.WriteLine(DateTime.Now));
            //thread.Start();
            ////同上
            //var action = new Action(() => Console.WriteLine(DateTime.Now));
            //action.BeginInvoke(null, null);


            Console.ReadLine();
        }
    }
}