• 大小: 0.03M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-02-25
  • 语言: C#
  • 标签: 反射  

资源简介

通过反射修改私有字段


【核心代码】

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();
        }
    }
}

资源截图

代码片段和文件信息

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格式的的另一种方式 [多线

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-01-06 11:11  ConsoleApplication1\
     目录           0  2016-01-05 17:45  ConsoleApplication1\bin\
     目录           0  2016-01-05 17:45  ConsoleApplication1\bin\Debug\
     文件        5120  2016-01-06 09:27  ConsoleApplication1\bin\Debug\ConsoleApplication1.exe
     文件       13824  2016-01-06 09:27  ConsoleApplication1\bin\Debug\ConsoleApplication1.pdb
     文件       22704  2016-01-06 11:07  ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe
     文件         490  2015-10-30 15:19  ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.manifest
     文件        2521  2015-12-25 13:28  ConsoleApplication1\ConsoleApplication1.csproj
     目录           0  2016-01-05 17:45  ConsoleApplication1\obj\
     目录           0  2016-01-06 09:27  ConsoleApplication1\obj\Debug\
     文件        1141  2016-01-06 11:07  ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.FileListAbsolute.txt
     文件        1414  2015-12-25 13:23  ConsoleApplication1\obj\Debug\ConsoleApplication1.csprojResolveAssemblyReference.cache
     文件        5120  2016-01-06 09:27  ConsoleApplication1\obj\Debug\ConsoleApplication1.exe
     文件       13824  2016-01-06 09:27  ConsoleApplication1\obj\Debug\ConsoleApplication1.pdb
     文件        6594  2016-01-06 09:01  ConsoleApplication1\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2015-12-25 13:18  ConsoleApplication1\obj\Debug\TempPE\
     文件        1771  2016-01-06 11:11  ConsoleApplication1\Program.cs
     目录           0  2016-01-05 17:45  ConsoleApplication1\Properties\
     文件        1362  2015-12-25 13:18  ConsoleApplication1\Properties\AssemblyInfo.cs
     文件        1026  2015-12-25 13:18  ConsoleApplication1.sln
     文件       48640  2016-01-06 11:16  ConsoleApplication1.v12.suo

评论

共有 条评论