• 工作中小知识点汇总(c#)


    1、OOP 实体与数据库字段转换(注意 此时实体字段必须和数据库中查询的字段列名相同)

    list = ModelExtend.GetByDataTablePart<EZRate>(ds.Tables[0]);
      /// <summary>
            /// 实体和DataTable都有的数据才回给实体赋值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="data"></param>
            /// <returns></returns>
            public static List<T> GetByDataTablePart<T>(this DataTable data) where T : class, new()
            {
                if (data != null)
                {
                    List<T> list = new List<T>();
    
                    foreach (DataRow row in data.Rows)
                    {
                        T t = row.GetByDataRowPart<T>();
                        if (t != null)
                        {
                            list.Add(t);
                        }
                    }
                    return list;
                }
                else
                    return null;
            }
     /// <summary>
            /// 实体和DataRow都有的数据才回给实体赋值
            /// </summary>
            /// <typeparam name="T">实体类型</typeparam>
            /// <param name="data">数据</param>
            /// <returns></returns>
            public static T GetByDataRowPart<T>(this DataRow data) where T : class, new()
            {
                if (data != null)
                {
                    T t = new T();
                    PropertyInfo[] properties = GetProperties(typeof(T));
    
                    foreach (PropertyInfo p in properties)
                    {
                        if (data.Table.Columns.Contains(p.Name))
                        {
                            object value = data[p.Name] == DBNull.Value ? p.PropertyType.Name.ToLower().Equals("string") ? string.Empty : null : data[p.Name];
                            p.SetValue(t, value, null);
                        }
                    }
                    return t;
                }
                else
                    return null;
    
            }

    2、重载可以用这种方式,不用写两个方法

     /// <summary>
            /// 构造产品订单映射
            /// </summary>
            /// <param name="identify">产品订单标识</param>
            /// <param name="supplier">供应商标识</param>
            /// <param name="commodity">产品标识</param>
            /// <param name="items">产品的具体项目(例如:机票)</param>
            public CommodityOrderMapping(string identify, string supplier, string commodity, IEnumerable<string> items = null, String isEmergency="0")
            {
                this.Identify = identify;
                this.Supplier = supplier;
                this.Commodity = commodity;
                this.IsEmergency = isEmergency;
                if (items != null) this._Items.AddRange(items);
            }
    //订单映射
                    orderMap = new OrderMapping(eco.TOrderId, eco.PurchaseCode,
                               new CommodityOrderMapping[] { new CommodityOrderMapping(eco.FOrderId, eco.SupplierCode, "C1",items: sublist, isEmergency: IsEmagenrcy) });//子订单映射

    在方法中赋默认值,例如 items ,isEmergency 在调用时 用 items:sublist isEmergency:IsEmagenrcy .也可以只写 items:sublist。
    总结后边两个参数可写可不写,但是必须前面加上名称例如items:sublist 必须加items。

     3、c#生成代理类

    1、

    2、wsdl /n:THPolicyWebService /out:E:THPolicyWebService.cs http://b2b.test.itour.com.cn:5000/THPolicyWebService.asmx?wsdl

    4、解析xml

    XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
    xnm.AddNamespace("rs", "http://www.opentravel.org/OTA/2003/05");
    string xPath = @"/rs:OTA_AirAvailRS/rs:OriginDestinationOptions/rs:OriginDestinationOption/rs:FlightSegment[contains(@FlightNumber,'CA1861')]";
    XmlNode isSuccess = xmlDoc.SelectSingleNode(xPath,xnm);
    string s = isSuccess.OuterXml;
     
    5、Common类
    // c#弹出框
            public static void MessageBox(System.Web.UI.Page page, string msg)
            {
                msg = msg.Replace(""", "");
                msg = msg.Replace("'", "");
                msg = msg.Replace("
    ", "");
                msg = msg.Replace("
    ", "");
    
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), Guid.NewGuid().ToString(),
                    "alert('" + msg + "');", true);
    
            }
            // c#后台写js
            public static void WriteScript(System.Web.UI.Page page, string script)
            {
    
    
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), Guid.NewGuid().ToString(),
                    string.Format("{0};", script), true);
    
            }
            // c#md5加密
            public static string Md5Compute(string txt)
            {
    
                MD5 md5 = MD5.Create();
                //把txt转成byte数组
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(txt);
                byte[] returnBytes = md5.ComputeHash(bytes);
                md5.Clear();
                string result = "";
                for (int i = 0; i < returnBytes.Length; i++)
                {
                    //ToString中的X表示转成16进制后再转string类型,2表示两位显示,不足两位前面补0
                    result += returnBytes[i].ToString("X2");
    
                }
                return result;
    
    
    
            }

     6、 单击文本框变色
            $(function () {
                $("input[type=text]").focus(function () {
                    $(this).addClass("myFocus");
                }).blur(function () {
                    $(this).removeClass("myFocus");
                });
            });

     7、windows服务时间控件使用
      private System.Timers.Timer timer1 = new System.Timers.Timer();
            protected override void OnStart(string[] args)
            {
              
                this.timer1.Enabled = true;
                this.timer1.Interval = 120000;
                this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Tick);
            }
    
            protected override void OnStop()
            {
                this.timer1.Enabled = false;
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
           // 填写实际的方法
         }
  • 相关阅读:
    【转】VS2013编译libjpeg库
    玩转百度地图(二)之画圆,高德地图、搜搜地图、搜狗地图等稍微修改即可
    JAVA自动生成正则表达式工具类
    S2SH商用后台权限系统第三讲
    自定义表单验证指令
    关于input/textarea提交内容空格回车转换问题,以及ng-model去除空格问题
    angular ui-router 缓存问题
    ionic 发送请求返回一直都是404
    ionic中获取坐标方法
    ionic的scroll的使用出现的问题
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3171405.html
Copyright © 2020-2023  润新知