C#拾贝

C#C#技巧C#进阶

不积跬步无以至千里,不积小流无以成江河

C#拾贝

一、Linq

1、以...开头 StartsWith

  1. Repeater1.DataSource=con.Users.Where(r=>r.Nickname.StartsWith("李")); 


  2. Repeater1.DataBind(); 


2、以...结尾 EndsWith

  1. Repeater1.DataSource=con.Users.Where(r=>r.Nickname.EndsWith("同")); 


  2. Repeater1.DataBind(); 


3、模糊差(包含) Contains

  1. Repeater1.DataSource=con.Users.Where(r=>r.Nickname.Contains("蘇")); 


  2. Repeater1.DataBind(); 


4、个数 Count()或者Tolist().Count

  1. Response.Write("总个数:"+con.Users.Count()); 


  2. Response.Write("总个数:"+con.Users.Tolist().Count; 


5、最大值 Max(r=>r.列名)

  1. Response.Write("总个数:"+con.Users.Tolist().Max(r=>r.Ids); 


6、最小值 Min(r=>r.列名)

  1. Response.Write("总个数:"+con.Users.Tolist().Min(r=>r.Ids); 


7、平均值 Average(r=>r.列名)

  1. Response.Write("总个数:"+con.Users.Tolist().Average(r=>r.Ids); 


8、求和 Sum(r=>r.列名)

  1. Response.Write("总个数:"+con.Users.Tolist().Sum(r=>r.Ids); 


9、升序 OrderBy(r=>r.列名)

  1. Repeater1.DataSource=con.Users.Tolist().OrderBy(r=>r.Ids); 


10、降序 OrderByDescending(r=>r.列名)

  1. Repeater1.DataSource=con.Users.Tolist().OrderByDescending(r=>r.Ids); 


11、分页 Skip()--跳过多少条 Take()--每页取多少条

  1. Repeater1.DataSource=con.Users.Tolist().Skip(0).Take(PageCount) 表示第一页跳过0条,每页取PageCount条 


二、模拟键盘按键

通过键盘按键可以调用一些软件的快捷键,比如录屏、截图、语言

键位对照表:https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes 十六进制转十进制即可

如虚拟键值 ESC键对应十六进制为0x1B十进制为27


键盘键与虚拟键码对照表


  1. [DllImport("user32.dll", EntryPoint = "keybd_event")] 



  2. public static extern void Keybd_event( 



  3. byte bvk,//虚拟键值 ESC键对应的是27 16进制为0x1B 



  4. byte bScan,//0 



  5. int dwFlags,//0为按下,1按住,2释放 



  6. int dwExtraInfo//0 



  7. ); 





  8. void Start() 







  9. Keybd_event(27,0,0,0); 



  10. Keybd_event(27, 0, 1, 0); 



  11. Keybd_event(27, 0, 2, 0); 






三、WPF另类投屏方案

有时希望WPF中可以将某些一个页面不同窗口投到不同的屏幕上去,类似仿真操控台上多个屏幕分别输出到不同的显示器。这时候可以通过窗口抓屏的方式,一秒抓30次来模拟录屏,在将抓的图替换到需要的窗口,不同屏幕放不同窗口。

API: RenderTargetBitmap

四、WPF白板实现

API:inkcanvas

DrawingAttributes 可以设置笔触大小、颜色、平滑等

ColorDialog 作为调色盘

PreviewMouseWheel 事件滚轮控制笔触大小

五、Attribute使用

1、为枚举添加描述

  1. using System; 


  2. using System.Collections.Generic; 


  3. using System.ComponentModel; 


  4. using System.Linq; 


  5. using System.Reflection; 


  6. using System.Text; 


  7. using System.Threading.Tasks; 



  8. namespace demo 





  9. /// <summary> 


  10. /// 枚举类 


  11. /// </summary> 


  12. public enum TypeEnum 





  13. [EnumText("活跃型")] 


  14. Active = 1, 



  15. [EnumText("健康型")] 


  16. Healthy = 2, 



  17. [EnumText("稳健型")] 


  18. Steady = 3 





  19. /// <summary> 


  20. /// 自定义Attribute 


  21. /// </summary> 


  22. public class EnumText : Attribute 





  23. public EnumText(String text) 





  24. this.Text = text; 






  25. public String Text { get; set; } 





  26. /// <summary> 


  27. /// 帮助类 


  28. /// </summary> 


  29. public class EnumHelper 





  30. /// <summary> 


  31. /// 获取自定义attribute  


  32. /// </summary> 


  33. /// <typeparam name="T"></typeparam> 


  34. /// <param name="enumObj"></param> 


  35. /// <returns></returns> 


  36. public static T GetAttribute<T>(Enum enumObj)where T:Attribute 





  37. Type type = enumObj.GetType(); 


  38. Attribute attr = null; 


  39. try 





  40. String enumName = Enum.GetName(type, enumObj); //获取对应的枚举名 


  41. FieldInfo field = type.GetField(enumName);  


  42. attr = field.GetCustomAttribute(typeof(T), false); 





  43. catch (Exception ex) 





  44. Console.WriteLine(ex); 


  45. // throw ex; 






  46. return (T)attr; 






  47. /// <summary> 


  48. ///  


  49. /// </summary> 


  50. /// <param name="args"></param> 


  51. public static void Main(String[] args) 





  52. var enum1 = TypeEnum.Active; 


  53. Console.WriteLine(GetAttribute<EnumText>(enum1).Text); 



  54. Console.WriteLine("输入任意键结束"); 


  55. Console.ReadKey(); 









2、为协议号添加描述

2.1、继承同一接口的协议号类

  1. public interface IProtocol { } 



  2. /// <summary> 


  3. /// 协议号 


  4. /// </summary> 


  5. public class Protocol:IProtocol 





  6. //... ... 



  7. /// <summary> 


  8. /// 登录 


  9. /// </summary> 


  10. [IProtocolDesc("登录")]  


  11. public const int Login = 2501; 



  12. //... ...  





2.2、通过反射来得到协议对应值的描述

  1. [AttributeUsage(AttributeTargets.Field)] 


  2. public sealed class IProtocolDescAttribute : Attribute 





  3. public string Descripiton { get; } 



  4. public IProtocolDescAttribute(string des) : base() 





  5. Descripiton = des; 









  6. public static class IProtocolDesHelper 





  7. public static string GetDes(int value) 





  8. string des = value.ToString(); 


  9. BindingFlags binding = BindingFlags.Static | BindingFlags.Public;//const值为静态类型,这里作为限定 



  10. Assembly ass=Assembly.GetAssembly(typeof(IProtocol)); 


  11. Type[] types = ass.GetTypes(); 


  12. List<Type> protocalTypes=new List<Type>(); 


  13. foreach (var type in types) 





  14. if (type.IsInterface) continue; 


  15. Type[] ins = type.GetInterfaces(); 


  16. foreach (var item in ins) 





  17. if(item==typeof(IProtocol)&&!protocalTypes.Contains(type)) 


  18. protocalTypes.Add(type);//挑选出实现IProtocol类型的实例 









  19. foreach (var protocalType in protocalTypes) 





  20. var fieldInfos = protocalType.GetFields(binding); 


  21. foreach (var fieldInfo in fieldInfos) 





  22. var feildvalue = fieldInfo.GetValue(protocalType); 


  23. int rel; 


  24. if (int.TryParse(feildvalue.ToString(), out rel)) 





  25. if (rel == value) 





  26. var att = fieldInfo.GetCustomAttributes(typeof(IProtocolDescAttribute), false) as IProtocolDescAttribute[]; 


  27. if (att != null && att.Length > 0) 


  28. return att[0].Descripiton; 
















  29. return des; 








六、反射使用

1、反射与泛型

  1. using System; 


  2. using System.Collections.Generic; 


  3. using System.Reflection; 


  4. using UnityEngine; 



  5. public class TestGneric : MonoBehaviour 





  6. // Start is called before the first frame update 


  7. void Start() 





  8. GetTypeOfGeneric(); 






  9. //获取泛型和已构造的Type对象的各种方式 


  10. public void GetTypeOfGeneric() 





  11. string listTypeName = "System.Collections.Generic.List`1"; 


  12. Type defByName = Type.GetType(listTypeName); 


  13. Type closedByName = Type.GetType(listTypeName + "[System.String]"); 


  14. Type closedByMethod = defByName.MakeGenericType(typeof(string)); 


  15. Type closedByTypeof = typeof(List<string>); 



  16. Debug.Log(closedByMethod==closedByName);//True 


  17. Debug.Log(closedByTypeof==closedByName);//True 



  18. Type defByTypeof = typeof(List<>); 


  19. Type defByMethod = closedByName.GetGenericTypeDefinition(); 



  20. Debug.Log(defByMethod==defByName);//True 


  21. Debug.Log(defByTypeof==defByName);//True 






  22. public static void PrintTypeParameter<T>() 





  23. Debug.Log(typeof(T)); 






  24. public void InvokeGenericByRf() 





  25. Type type = typeof(TestGneric); 


  26. //从泛型类型定义获取的方法不能直接调用, 


  27. MethodInfo definition = type.GetMethod("PrintTypeParameter"); 


  28. //必须从一个已构造的类型获取方法 


  29. MethodInfo constructed = definition.MakeGenericMethod(typeof(string)); 


  30. constructed.Invoke(null, null); 









C#拾贝的更多相关文章

  1. iOS多线程拾贝------操作巨人编程

    iOS多线程拾贝------操作巨人编程 多线程 基本 实现方案:pthread - NSThread - GCD - NSOperation Pthread 多平台,可移植 c语言,要程序员管理生命 ...

  2. AngularJS进阶(三十二)书海拾贝之特殊的ng-src和ng-href

    书海拾贝之特殊的ng-src和ng-href 在说明这两个指令的特殊之前,需要先了解一下ng的启动及执行过程,如下: 1) 浏览器加载静态HTML文件并解析为DOM: 2) 浏览器加载angular. ...

  3. python: 爬取[博海拾贝]图片脚本

    练手代码,聊作备忘: # encoding: utf-8 # from __future__ import unicode_literals import urllib import urllib2 ...

  4. python 拾贝

    1. 内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下:   type(name, bases, dict) 返回一个新的type对象. 基本上是 class 语句的动态形式 ...

  5. 技海拾贝 - Android

    1. 前台Service - 介绍: http://blog.csdn.net/think_soft/article/details/7299438 - 代码实例:  http://blog.csdn ...

  6. 技海拾贝 - Java

    1. Java中的多线程 http://blog.csdn.net/luoweifu/article/details/46673975 Java中继承thread类与实现Runnable接口的区别 h ...

  7. DDD:《实现领域驱动》拾贝(待续)

    Design is not just what it looks like and feels like. Design is how it works.

  8. AngularJS进阶(三十三)书海拾贝之简介AngularJS中使用factory和service的方法

    简介AngularJS中使用factory和service的方法 AngularJS支持使用服务的体系结构"关注点分离"的概念.服务是JavaScript函数,并负责只做一个特定的 ...

  9. .Net Discovery 系列之七--深入理解.Net垃圾收集机制(拾贝篇)

    关于.Net垃圾收集器(Garbage Collection),Aicken已经在“.Net Discovery 系列”文章中有2篇的涉及,这一篇文章是对上2篇文章的补充,关于“.Net Discov ...

  10. [码海拾贝 之Perl]在字符串数组中查找特定的字符串是否存在

    前言 检索一个字符串是否存在于一个数组中, 最主要的想法应该就是对数组进行循环, 逐个推断数组的每一个元素值和给定的值是否相等. (在Java语言还能够把数组转成 List , 在 list 中直接有 ...

随机推荐

  1. 一文详解Redis中BigKey、HotKey的发现与处理

    简介: 在Redis的使用过程中,我们经常会遇到BigKey(下文将其称为"大key")及HotKey(下文将其称为"热key").大Key与热Key如果未能及 ...

  2. 日志审计携手DDoS防护助力云上安全

    ​简介: 本文主要介绍日志审计结合DDoS防护保障云上业务安全的新实践. 日志审计携手DDoS防护助力云上安全 1 背景介绍 设想一下,此时你正在高速公路上开车去上班,路上还有其他汽车,总体而言,大家 ...

  3. Cube 技术解读 | 支付宝新一代动态化技术架构与选型综述

    ​简介: 支付宝客户端的动态化技术经历三个阶段:现阶段也就是第三阶段是实体组件+部分光栅化的hybrid模式,Cube 就是该模式下的产物. ​ 如标题所述,笔者将持续更新<Cube 技术解读& ...

  4. [Contract] web3.eth.getAccounts, web3.eth.getCoinbase 使用场景区别

    web3.eth.getAccounts() 返回节点控制的账号列表(Promise returns Array) web3.eth.getCoinbase() 返回挖矿奖励所归集的地址(Promis ...

  5. 每天5分钟复习OpenStack(十三)存储缓存技术Bcache

    Ceph作为一个分布式存储,在项目中常见的形态有两者,一种是采用 SSD 或NVME 磁盘做Ceph的日志盘,使用SATA磁盘来做数据盘.这样的好处是比较经济实惠.另一种则是全部采用 SSD 或NVM ...

  6. 使用 DISM 安全清理 C 盘 WinSxS 文件夹空间

    本文将介绍如何使用系统内置 DISM 工具进行安全清理 C 盘空间,清理 WinSxS 文件夹里面的可回收删除的程序包空间 开始之前,先使用管理员权限打开 CMD 或 PowerShell 命令行窗口 ...

  7. dotnet 使用 Newtonsoft.Json 输出枚举首字符小写

    本文告诉大家如何使用 Newtonsoft.Json 输出枚举首字符小写 实现方法是加上 JsonConverterAttribute 特性,传入 StringEnumConverter 转换器,再加 ...

  8. 01 Xpath简明教程(十分钟入门)

    目录 Xpath简明教程(十分钟入门) Xpath表达式 Xpath节点 节点关系 Xpath基本语法 1) 基本语法使用 2) xpath通配符 3) 多路径匹配 Xpath内建函数 Xpath简明 ...

  9. Git/SourceTree版本管理

    目录 视频课程: 工作区: 文件状态: 回退版本: 合并分支 合并提交 冲突 删除分支 忽略文件 汉英对照表 多端同步 添加远程仓库 推送代码到远程仓库 拉取代码 视频课程: https://www. ...

  10. 3 个好玩的前端开源项目「GitHub 热点速览」

    单休的周末总是短暂的,还没缓过神新的一周就又开始了.如果你和我一样状态还没有完全恢复,那就让上周好玩的开源项目唤醒你吧! 每周 GitHub 上总是有一些让人眼前一亮的开源项目,上周好玩的前端项目特别 ...