常用的一些操作方法

简介: 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

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

目录
相关文章
|
并行计算 数据安全/隐私保护
|
3天前
|
弹性计算 运维 搜索推荐
三翼鸟携手阿里云ECS g9i:智慧家庭场景的效能革命与未来生活新范式
三翼鸟是海尔智家旗下全球首个智慧家庭场景品牌,致力于提供覆盖衣、食、住、娱的一站式全场景解决方案。截至2025年,服务近1亿家庭,连接设备超5000万台。面对高并发、低延迟与稳定性挑战,全面升级为阿里云ECS g9i实例,实现连接能力提升40%、故障率下降90%、响应速度提升至120ms以内,成本降低20%,推动智慧家庭体验全面跃迁。
|
4天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
372 91
|
5天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
385 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
4天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
267 156