常用的一些操作方法

简介: 1.反射创建类型 //3 列排序 Type t = data.ElementType; //var pcs = Activator.

1.反射创建类型

          //3 列排序
                Type t = data.ElementType;
                //var pcs = Activator.CreateInstance(t, new object[] {
                //    "addTime",
                //    Common.ListSortDirection.Ascending
                //});

                Type type = typeof(PropertySortCondition<>);
                type = type.MakeGenericType(t);
                var pcs = Activator.CreateInstance(type, new object[] {
                    "addTime",
                    Common.ListSortDirection.Ascending
                });
View Code

 2.写入TXT文件

        #region 写文件(.txt-添加)
        /// <summary>  
        /// 写文件  
        /// </summary>  
        /// <param name="FileName">文件路径,相对路径,不需要写扩展名</param>  
        /// <param name="Strings">文件内容</param>  
        private void WriteFile(string FileName, string Strings)
        {
            string Path = System.Web.HttpContext.Current.Server.MapPath("~/" + FileName + ".txt");
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }
        #endregion
View Code

3.参数对比与赋值

System.Threading.Interlocked.CompareExchange(ref "参数1","参数3","参数2");


参数1与参数2对比,一样就把参数3复制给参数1.不一样就把参数2赋值给参数1.这个方法返回的是原始的参数1,即返回值在此方法没有赋值前已经确认。

4.操作xml文件

//创建xml文件
string mPath = Application.StartupPath + "/record.xml";//创建位置
 if (!File.Exists(mPath))
                {
                    CreateXMLDoc();
                    mXmlDoc.Load(mPath);
                }
//添加节点
        public void CreateXMLDoc()
        {
            if (!File.Exists(mPath))
            {
                XmlDocument xmldoc = new XmlDocument();
                //加入XML的声明段落
                xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null));
                XmlElement rMainNode = xmldoc.CreateElement("", "settings", "");
                rMainNode.IsEmpty = false;
                xmldoc.AppendChild(rMainNode);

                XmlElement rIp = xmldoc.CreateElement("", "ips", "");
                rIp.IsEmpty = false;
                rMainNode.AppendChild(rIp);

                XmlElement rPort = xmldoc.CreateElement("", "ports", "");
                rPort.IsEmpty = false;
                rMainNode.AppendChild(rPort);

                XmlElement rName = xmldoc.CreateElement("", "names", "");
                rName.IsEmpty = false;
                rMainNode.AppendChild(rName);


                XmlElement rPreviousRecord = xmldoc.CreateElement("", "previousreocrd", "");
                rPreviousRecord.IsEmpty = false;
                rMainNode.AppendChild(rPreviousRecord);

                xmldoc.Save(mPath);
            }
        }
/////删除节点
 XmlNode rMainNode = mXmlDoc.SelectSingleNode("settings");

                    XmlNode rNode = rMainNode.SelectSingleNode("names");
                    XmlNodeList rList = rNode.ChildNodes;
                    List<string> list = new List<string>();
                    foreach (XmlNode r in rList)
                    {
                        XmlElement rElem = (XmlElement)r;
                        list.Add(rElem.GetAttribute("value").ToString().Split(',')[0]);
                        if (rElem.GetAttribute("value").ToString().Split(',')[0] == tb_name.Text) 
                        {
                            rNode.RemoveChild(rElem);
                        }

                    }
View Code

5.读写ini文件

#region 读Ini文件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Section">节名</param>
        /// <param name="Key">键名</param>
        /// <param name="NoText">读取失败返回值</param>
        /// <param name="iniFilePath">路径</param>
        /// <returns>读取值</returns>
        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        #endregion

        #region 写Ini文件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Section">节名</param>
        /// <param name="Key">键名</param>
        /// <param name="Value"></param>
        /// <param name="iniFilePath">路径</param>
        /// <returns></returns>
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion
View Code

6.读写web.config文件

//定义对象 
 static Configuration config =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
//读取
  string access_token = config.AppSettings.Settings["access_token"].Value;
//写入
 AppSettingsSection app = config.AppSettings;
                    app.Settings["access_token"].Value = token.access_token;
                    config.Save(ConfigurationSaveMode.Modified);
View Code

 

7.拼接json字符串

 //参数拼接              
            string param = "";//这里是开始位置
            param += "{";
            param += "\"" + "touser" + "\":\"" + openid + "\",";
            param += "\"" + "template_id" + "\":\"" + "xa-8MK5i0UI3rv-icn_7WTaE3mytLvti0bfoLW1hJFo" + "\",";
            param += "\"" + "url" + "\":\"" + JumpUrl + "\",";
            param += "\"" + "data" + "\"";
            param += ":";
            param += "{";
            param += "\"" + "content" + "\"";
            param += ":";
            param += "{";
            param += "\"" + "value" + "\":\"" + msg + "\",";
            param += "\"" + "color" + "\":\"" + "#173177" + "\"";
            param += "}";

            param += "},";
            param = param.TrimEnd(',');
            param += "}";
View Code

8.反射创建类并运行其中一个方法

 //Type[] mytypes = Assembly.GetExecutingAssembly().GetTypes();
                    Type type= typeof(User);//user 是类
                    var methom = Activator.CreateInstance(type);//反射创建类
                    MethodInfo methodinfo = type.GetMethod(_mess);//获取方法 _mess参数是自定义字符串为方法名称
                    methodinfo.Invoke(methom, null);//运行方法

 

9.多级linq关联存在关联数据不存在的情况

                           (from a in childData     //主关联文件
                            join bj in bjCostData    //副关联文件数据可能存在数据没有对应关联
                            on a.id equals bj.childId  //关联键
                            into bj                       
                            from bje in bj.DefaultIfEmpty()   //存在没有关系对应关联是的处理
                            join bjt in bjRefundData
                            on a.id equals bjt.childId
                            into bjt
                            from bjte in bjt.DefaultIfEmpty()select new H_ChildStatistics
                            {
                                id = a.id,
                                parkId = a.parkId,
                                parkName = a.parkName,
                                childName = a.childName,
                                childNameEng = a.childNameEng,
                                gradeNo = a.gradeNo,
                                classNo = a.classNo,
                                modifyTime = a.modifyTime,
                                bjfTotalReceive = bje == null ? 0 : bje.payTotalMoney,
                                bjfTotalRefund = bjte == null ? 0 : bjte.payTotalMoney,  
                                childTotalReceive =0 ,
                                childTotalRefund =0 ,
                            });

 

作者:YanBigFeg —— 颜秉锋

出处:http://www.cnblogs.com/yanbigfeg

本文版权归作者和博客园共有,欢迎转载,转载请标明出处。如果您觉得本篇博文对您有所收获,觉得小弟还算用心,请点击右下角的 [推荐],谢谢!

目录
相关文章
|
并行计算 数据安全/隐私保护
|
4天前
|
云安全 监控 安全
|
2天前
|
存储 机器学习/深度学习 人工智能
打破硬件壁垒!煎饺App:强悍AI语音工具,为何是豆包AI手机平替?
直接上干货!3000 字以上长文,细节拉满,把核心功能、使用技巧和实测结论全给大家摆明白,读完你就知道这款 “安卓机通用 AI 语音工具"——煎饺App它为何能打破硬件壁垒?它接下来,咱们就深度拆解煎饺 App—— 先给大家扒清楚它的使用逻辑,附上“操作演示”和“🚀快速上手不踩坑 : 4 条核心操作干货(必看)”,跟着走零基础也能快速上手;后续再用真实实测数据,正面硬刚煎饺 App的语音助手口令效果——创建京东「牛奶自动下单神器」口令 ,从修改口令、识别准确率到场景实用性,逐一测试不掺水,最后,再和豆包 AI 手机语音助手的普通版——豆包App对比测试下,简单地谈谈煎饺App的能力边界在哪?
|
9天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1128 7
|
11天前
|
机器学习/深度学习 人工智能 数据可视化
1秒生图!6B参数如何“以小博大”生成超真实图像?
Z-Image是6B参数开源图像生成模型,仅需16GB显存即可生成媲美百亿级模型的超真实图像,支持中英双语文本渲染与智能编辑,登顶Hugging Face趋势榜,首日下载破50万。
727 42
|
15天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
1169 41
|
15天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
934 77
大厂CIO独家分享:AI如何重塑开发者未来十年
|
3天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
|
1天前
|
人工智能 JSON 前端开发
为什么你的API文档总是被吐槽?用这份"契约指令"终结前后端战争
本文针对前后端协作中"文档过时、不准确"的痛点,提供了一套实战验证的AI指令。通过强制结构化输入和自检机制,让AI自动生成包含完整参数、JSON示例和多语言代码的标准API契约文档,彻底解决接口沟通难题。
171 112

热门文章

最新文章