欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

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

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复