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.  
  2. [DllImport("user32.dll", EntryPoint = "keybd_event")] 
  3.  
  4. public static extern void Keybd_event( 
  5.  
  6. byte bvk,//虚拟键值 ESC键对应的是27 16进制为0x1B 
  7.  
  8. byte bScan,//0 
  9.  
  10. int dwFlags,//0为按下,1按住,2释放 
  11.  
  12. int dwExtraInfo//0 
  13.  
  14. ); 
  15.  
  16.  
  17.  
  18. void Start() 
  19.  
  20. { 
  21.  
  22. Keybd_event(27,0,0,0); 
  23.  
  24. Keybd_event(27, 0, 1, 0); 
  25.  
  26. Keybd_event(27, 0, 2, 0); 
  27.  
  28. } 

三、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.  
  9. namespace demo 
  10. { 
  11. /// <summary> 
  12. /// 枚举类 
  13. /// </summary> 
  14. public enum TypeEnum 
  15. { 
  16. [EnumText("活跃型")] 
  17. Active = 1, 
  18.  
  19. [EnumText("健康型")] 
  20. Healthy = 2, 
  21.  
  22. [EnumText("稳健型")] 
  23. Steady = 3 
  24. } 
  25. /// <summary> 
  26. /// 自定义Attribute 
  27. /// </summary> 
  28. public class EnumText : Attribute 
  29. { 
  30. public EnumText(String text) 
  31. { 
  32. this.Text = text; 
  33. } 
  34.  
  35. public String Text { get; set; } 
  36. } 
  37. /// <summary> 
  38. /// 帮助类 
  39. /// </summary> 
  40. public class EnumHelper 
  41. { 
  42. /// <summary> 
  43. /// 获取自定义attribute  
  44. /// </summary> 
  45. /// <typeparam name="T"></typeparam> 
  46. /// <param name="enumObj"></param> 
  47. /// <returns></returns> 
  48. public static T GetAttribute<T>(Enum enumObj)where T:Attribute 
  49. { 
  50. Type type = enumObj.GetType(); 
  51. Attribute attr = null; 
  52. try 
  53. { 
  54. String enumName = Enum.GetName(type, enumObj); //获取对应的枚举名 
  55. FieldInfo field = type.GetField(enumName);  
  56. attr = field.GetCustomAttribute(typeof(T), false); 
  57. } 
  58. catch (Exception ex) 
  59. { 
  60. Console.WriteLine(ex); 
  61. // throw ex; 
  62. } 
  63.  
  64. return (T)attr; 
  65. } 
  66.  
  67. /// <summary> 
  68. ///  
  69. /// </summary> 
  70. /// <param name="args"></param> 
  71. public static void Main(String[] args) 
  72. { 
  73. var enum1 = TypeEnum.Active; 
  74. Console.WriteLine(GetAttribute<EnumText>(enum1).Text); 
  75.  
  76. Console.WriteLine("输入任意键结束"); 
  77. Console.ReadKey(); 
  78.  
  79. } 
  80. } 

2、为协议号添加描述

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

  1. public interface IProtocol { } 
  2.  
  3. /// <summary> 
  4. /// 协议号 
  5. /// </summary> 
  6. public class Protocol:IProtocol 
  7. { 
  8. //... ... 
  9.  
  10. /// <summary> 
  11. /// 登录 
  12. /// </summary> 
  13. [IProtocolDesc("登录")]  
  14. public const int Login = 2501; 
  15.  
  16. //... ...  
  17. } 

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

  1. [AttributeUsage(AttributeTargets.Field)] 
  2. public sealed class IProtocolDescAttribute : Attribute 
  3. { 
  4. public string Descripiton { get; } 
  5.  
  6. public IProtocolDescAttribute(string des) : base() 
  7. { 
  8. Descripiton = des; 
  9. } 
  10. } 
  11.  
  12. public static class IProtocolDesHelper 
  13. { 
  14. public static string GetDes(int value) 
  15. { 
  16. string des = value.ToString(); 
  17. BindingFlags binding = BindingFlags.Static | BindingFlags.Public;//const值为静态类型,这里作为限定 
  18.  
  19. Assembly ass=Assembly.GetAssembly(typeof(IProtocol)); 
  20. Type[] types = ass.GetTypes(); 
  21. List<Type> protocalTypes=new List<Type>(); 
  22. foreach (var type in types) 
  23. { 
  24. if (type.IsInterface) continue; 
  25. Type[] ins = type.GetInterfaces(); 
  26. foreach (var item in ins) 
  27. { 
  28. if(item==typeof(IProtocol)&&!protocalTypes.Contains(type)) 
  29. protocalTypes.Add(type);//挑选出实现IProtocol类型的实例 
  30. } 
  31. } 
  32.  
  33. foreach (var protocalType in protocalTypes) 
  34. { 
  35. var fieldInfos = protocalType.GetFields(binding); 
  36. foreach (var fieldInfo in fieldInfos) 
  37. { 
  38. var feildvalue = fieldInfo.GetValue(protocalType); 
  39. int rel; 
  40. if (int.TryParse(feildvalue.ToString(), out rel)) 
  41. { 
  42. if (rel == value) 
  43. { 
  44. var att = fieldInfo.GetCustomAttributes(typeof(IProtocolDescAttribute), false) as IProtocolDescAttribute[]; 
  45. if (att != null && att.Length > 0) 
  46. return att[0].Descripiton; 
  47. } 
  48. } 
  49.  
  50. } 
  51. } 
  52.  
  53. return des; 
  54. } 
  55. } 
原文地址:https://www.cnblogs.com/Firepad-magic/p/14091389.html