一、几个静态方法
1.获取本机所有服务列表 ,返回System.ServiceProcess.ServiceController的数组。
System.ServiceProcess.ServiceController.GetServices()
2.获取执行的当前方法MethodBase对象。可以调用MethodBase对象的DeclaringType获取当前对象的Type
System.Reflection.MethodBase.GetCurrentMethod()
3.获取一个类型是否带无参公共构造函数
type.GetConstructor(new Type[]{}); 返回null则不存在,反之存在。
4.Interlocked.Increment(ref loaction)
线程安全的自增方法
5.System.Threading.ReaderWriterLock
读写锁的类,多个线程可以同时读,但不能一读一写,也不能同时写
二、特性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
该特性可以标记给特性类
public class MyAttribute : Attribute { public MyAttribute() { } }
标记了之后代表My特性只可以标记给Class和Interface。
三、Lazy
LazyThreadSafetyMode 枚举参数
LazyThreadSafetyMode.None 线程不安全 当多个线程创建时实例时会报错
LazyThreadSafetyMode.PublicationOnly 线程不安全 当多个线程创建时不会报错,不推荐使用
LazyThreadSafetyMode.ExecutionAndPublication 线程安全 当多个线程创建时会加锁
四、枚举
判断多位枚举的包含关系
enum oop { a = 1, b = 2, c = 4, d = 8, e = 16 } oop o = oop.a | oop.b | oop.c; bool asd= (o & oop.a) != 0;
删除一个枚举值
o=o & (~oop.a);
五、Graphics
测量字符串所占的长宽用TextRenderer.MeasureText()或者Graphics.MeasureString();前者精确度高
六、implicit 关键字
public static implicit operator string(CommandBuilder<T> command);
隐身类型转换
将CommandBuilder<T>转换成string类型
七、[ThreadStatic]特性 ,非常实用的技巧。
[ThreadStatic] private static Db instance; public new static Db Instance { get { if (instance == null) { instance = GetNewInstance(true); } return instance; } }
ThreadStatic使每个线程只有一个实例(单例)。