常用的一些操作方法

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

 

posted @ 2017-07-01 16:33  YanBigFeg  阅读(242)  评论(0编辑  收藏  举报