C#拾贝
C#拾贝
不积跬步无以至千里,不积小流无以成江河
C#拾贝
一、Linq
1、以...开头 StartsWith
- Repeater1.DataSource=con.Users.Where(r=>r.Nickname.StartsWith("李"));
- Repeater1.DataBind();
2、以...结尾 EndsWith
- Repeater1.DataSource=con.Users.Where(r=>r.Nickname.EndsWith("同"));
- Repeater1.DataBind();
3、模糊差(包含) Contains
- Repeater1.DataSource=con.Users.Where(r=>r.Nickname.Contains("蘇"));
- Repeater1.DataBind();
4、个数 Count()或者Tolist().Count
- Response.Write("总个数:"+con.Users.Count());
- Response.Write("总个数:"+con.Users.Tolist().Count;
5、最大值 Max(r=>r.列名)
- Response.Write("总个数:"+con.Users.Tolist().Max(r=>r.Ids);
6、最小值 Min(r=>r.列名)
- Response.Write("总个数:"+con.Users.Tolist().Min(r=>r.Ids);
7、平均值 Average(r=>r.列名)
- Response.Write("总个数:"+con.Users.Tolist().Average(r=>r.Ids);
8、求和 Sum(r=>r.列名)
- Response.Write("总个数:"+con.Users.Tolist().Sum(r=>r.Ids);
9、升序 OrderBy(r=>r.列名)
- Repeater1.DataSource=con.Users.Tolist().OrderBy(r=>r.Ids);
10、降序 OrderByDescending(r=>r.列名)
- Repeater1.DataSource=con.Users.Tolist().OrderByDescending(r=>r.Ids);
11、分页 Skip()--跳过多少条 Take()--每页取多少条
- 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
键盘键与虚拟键码对照表
- [DllImport("user32.dll", EntryPoint = "keybd_event")]
- public static extern void Keybd_event(
- byte bvk,//虚拟键值 ESC键对应的是27 16进制为0x1B
- byte bScan,//0
- int dwFlags,//0为按下,1按住,2释放
- int dwExtraInfo//0
- );
- void Start()
- {
- Keybd_event(27,0,0,0);
- Keybd_event(27, 0, 1, 0);
- Keybd_event(27, 0, 2, 0);
- }
三、WPF另类投屏方案
有时希望WPF中可以将某些一个页面不同窗口投到不同的屏幕上去,类似仿真操控台上多个屏幕分别输出到不同的显示器。这时候可以通过窗口抓屏的方式,一秒抓30次来模拟录屏,在将抓的图替换到需要的窗口,不同屏幕放不同窗口。
API: RenderTargetBitmap
四、WPF白板实现
API:inkcanvas
DrawingAttributes 可以设置笔触大小、颜色、平滑等
ColorDialog 作为调色盘
PreviewMouseWheel 事件滚轮控制笔触大小
五、Attribute使用
1、为枚举添加描述
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace demo
- {
- /// <summary>
- /// 枚举类
- /// </summary>
- public enum TypeEnum
- {
- [EnumText("活跃型")]
- Active = 1,
- [EnumText("健康型")]
- Healthy = 2,
- [EnumText("稳健型")]
- Steady = 3
- }
- /// <summary>
- /// 自定义Attribute
- /// </summary>
- public class EnumText : Attribute
- {
- public EnumText(String text)
- {
- this.Text = text;
- }
- public String Text { get; set; }
- }
- /// <summary>
- /// 帮助类
- /// </summary>
- public class EnumHelper
- {
- /// <summary>
- /// 获取自定义attribute
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="enumObj"></param>
- /// <returns></returns>
- public static T GetAttribute<T>(Enum enumObj)where T:Attribute
- {
- Type type = enumObj.GetType();
- Attribute attr = null;
- try
- {
- String enumName = Enum.GetName(type, enumObj); //获取对应的枚举名
- FieldInfo field = type.GetField(enumName);
- attr = field.GetCustomAttribute(typeof(T), false);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- // throw ex;
- }
- return (T)attr;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="args"></param>
- public static void Main(String[] args)
- {
- var enum1 = TypeEnum.Active;
- Console.WriteLine(GetAttribute<EnumText>(enum1).Text);
- Console.WriteLine("输入任意键结束");
- Console.ReadKey();
- }
- }
2、为协议号添加描述
2.1、继承同一接口的协议号类
- public interface IProtocol { }
- /// <summary>
- /// 协议号
- /// </summary>
- public class Protocol:IProtocol
- {
- //... ...
- /// <summary>
- /// 登录
- /// </summary>
- [IProtocolDesc("登录")]
- public const int Login = 2501;
- //... ...
- }
2.2、通过反射来得到协议对应值的描述
- [AttributeUsage(AttributeTargets.Field)]
- public sealed class IProtocolDescAttribute : Attribute
- {
- public string Descripiton { get; }
- public IProtocolDescAttribute(string des) : base()
- {
- Descripiton = des;
- }
- }
- public static class IProtocolDesHelper
- {
- public static string GetDes(int value)
- {
- string des = value.ToString();
- BindingFlags binding = BindingFlags.Static | BindingFlags.Public;//const值为静态类型,这里作为限定
- Assembly ass=Assembly.GetAssembly(typeof(IProtocol));
- Type[] types = ass.GetTypes();
- List<Type> protocalTypes=new List<Type>();
- foreach (var type in types)
- {
- if (type.IsInterface) continue;
- Type[] ins = type.GetInterfaces();
- foreach (var item in ins)
- {
- if(item==typeof(IProtocol)&&!protocalTypes.Contains(type))
- protocalTypes.Add(type);//挑选出实现IProtocol类型的实例
- }
- }
- foreach (var protocalType in protocalTypes)
- {
- var fieldInfos = protocalType.GetFields(binding);
- foreach (var fieldInfo in fieldInfos)
- {
- var feildvalue = fieldInfo.GetValue(protocalType);
- int rel;
- if (int.TryParse(feildvalue.ToString(), out rel))
- {
- if (rel == value)
- {
- var att = fieldInfo.GetCustomAttributes(typeof(IProtocolDescAttribute), false) as IProtocolDescAttribute[];
- if (att != null && att.Length > 0)
- return att[0].Descripiton;
- }
- }
- }
- }
- return des;
- }
- }
六、反射使用
1、反射与泛型
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEngine;
- public class TestGneric : MonoBehaviour
- {
- // Start is called before the first frame update
- void Start()
- {
- GetTypeOfGeneric();
- }
- //获取泛型和已构造的Type对象的各种方式
- public void GetTypeOfGeneric()
- {
- string listTypeName = "System.Collections.Generic.List`1";
- Type defByName = Type.GetType(listTypeName);
- Type closedByName = Type.GetType(listTypeName + "[System.String]");
- Type closedByMethod = defByName.MakeGenericType(typeof(string));
- Type closedByTypeof = typeof(List<string>);
- Debug.Log(closedByMethod==closedByName);//True
- Debug.Log(closedByTypeof==closedByName);//True
- Type defByTypeof = typeof(List<>);
- Type defByMethod = closedByName.GetGenericTypeDefinition();
- Debug.Log(defByMethod==defByName);//True
- Debug.Log(defByTypeof==defByName);//True
- }
- public static void PrintTypeParameter<T>()
- {
- Debug.Log(typeof(T));
- }
- public void InvokeGenericByRf()
- {
- Type type = typeof(TestGneric);
- //从泛型类型定义获取的方法不能直接调用,
- MethodInfo definition = type.GetMethod("PrintTypeParameter");
- //必须从一个已构造的类型获取方法
- MethodInfo constructed = definition.MakeGenericMethod(typeof(string));
- constructed.Invoke(null, null);
- }
- }
C#拾贝的更多相关文章
- iOS多线程拾贝------操作巨人编程
iOS多线程拾贝------操作巨人编程 多线程 基本 实现方案:pthread - NSThread - GCD - NSOperation Pthread 多平台,可移植 c语言,要程序员管理生命 ...
- AngularJS进阶(三十二)书海拾贝之特殊的ng-src和ng-href
书海拾贝之特殊的ng-src和ng-href 在说明这两个指令的特殊之前,需要先了解一下ng的启动及执行过程,如下: 1) 浏览器加载静态HTML文件并解析为DOM: 2) 浏览器加载angular. ...
- python: 爬取[博海拾贝]图片脚本
练手代码,聊作备忘: # encoding: utf-8 # from __future__ import unicode_literals import urllib import urllib2 ...
- python 拾贝
1. 内建的 type() 函数带三个参数时, 将作为强悍的动态类构造器. 如下: type(name, bases, dict) 返回一个新的type对象. 基本上是 class 语句的动态形式 ...
- 技海拾贝 - Android
1. 前台Service - 介绍: http://blog.csdn.net/think_soft/article/details/7299438 - 代码实例: http://blog.csdn ...
- 技海拾贝 - Java
1. Java中的多线程 http://blog.csdn.net/luoweifu/article/details/46673975 Java中继承thread类与实现Runnable接口的区别 h ...
- DDD:《实现领域驱动》拾贝(待续)
Design is not just what it looks like and feels like. Design is how it works.
- AngularJS进阶(三十三)书海拾贝之简介AngularJS中使用factory和service的方法
简介AngularJS中使用factory和service的方法 AngularJS支持使用服务的体系结构"关注点分离"的概念.服务是JavaScript函数,并负责只做一个特定的 ...
- .Net Discovery 系列之七--深入理解.Net垃圾收集机制(拾贝篇)
关于.Net垃圾收集器(Garbage Collection),Aicken已经在“.Net Discovery 系列”文章中有2篇的涉及,这一篇文章是对上2篇文章的补充,关于“.Net Discov ...
- [码海拾贝 之Perl]在字符串数组中查找特定的字符串是否存在
前言 检索一个字符串是否存在于一个数组中, 最主要的想法应该就是对数组进行循环, 逐个推断数组的每一个元素值和给定的值是否相等. (在Java语言还能够把数组转成 List , 在 list 中直接有 ...
随机推荐
- [Caddy2] URL访问路径的重定向和重写规则 (redir/rewrite 指令)
当我们在规划网站路径时,为了保留搜索引擎收录 避免404的同时做到升级,常用到重定向跳转和URL重写. 重定向(redirect) 在 Caddy 中为 redir 指令. https://caddy ...
- async 与 promise 的区别
async函数会引式返回一个promise,而promise的resolve值就是函数return的值 使用async和await明显节约了不少代码,不需要.then,不需要写匿名函数处理promis ...
- ABAP 7.58 中支持任意精度算术的新类
1. 引言 通常,有两种对编程语言的改进.第一种是让困难的事情变得简单,第二种是让不可能的事情变为可能.本文介绍的是任意精度算术,它属于第二类:使在ABAP中原本不可能的事情成为可能. 过去已经可以在 ...
- rails角本启动和停止
start.sh #!/bin/bash nohup rails s Puma -d >> run_log.log 2>&1 & stop.sh #!/bin/bas ...
- 网络协议分析与抓包 TCP/IP UDP等
学习地址: https://www.bilibili.com/video/BV1hV411U74y?p=4 https://www.bilibili.com/video/BV1S7411R7kF?p= ...
- vue3.4中defineModel中默认值是复杂数据类型 (注意!!!)
const drillFields = defineModel<string[]>('drillFields', { get(val) { return reactive(val || [ ...
- mongodb的replication与shard分片结合使用详解
部署脚本 #!/bin/bash #复制集配置 IP='10.0.0.12' #主机ip NA='rs3' #复制集名称 if [ "$1" = "reset" ...
- centos7源码编译安装nginx1.19并调优,向已安装的nginx添加新模块
目录 一.关于nginx 二.nginx的安装方式 三.源码编译安装nginx 3.1 下载nginx源码并解压 3.2 创建nginx用户和组 3.3 安装nginx编译环境(解决依赖问题) 3.4 ...
- 【winform】【Socket】实现你画我猜一:核心功能开发
我认为得核心:是把客户端画的图画实时传递给其他的客户端. 我的思路是: 1.GDI+绘图加鼠标事件,实现客户端绘图. 2.每记录鼠标有效绘图10个点,就把这10个点通过socket传递给服务器. ...
- group_concat 和 case when 的坑
SELECT size,instrument_id, (CASE side WHEN "sell" THEN group_concat(id ORDER BY id) END )a ...
注