7、Const 与 Readonly 区别: 前者静态,后者动态; 前者值替换,后者可以在初始化时赋值
编译结束之后使用Reflector或者IL工具查看代码,是没有I这个常量的,所有使用I这个常量的地方,都被替换成了值100。
基于以上原因,所有public修饰的变量,都不应该使用const修饰,因为,如果外部类引用了这个变量(常量),编译的时候,编译器会自动将该常量替换为值。后果就是,如果改变了该常量的值,就必须重新编译所有引用该常量的代码。
场景:该常量放在一个dll里,现在值变了,就必须更新所有引用了该dll的文件,而不是替换该dll就可以。
6、写入、输出二进制数据(指定地址 与 内存交互数据)
public void WriteSerialize(object data, int offset, int length) { BinaryFormatter formatter = new BinaryFormatter(); byte[] buffer = new byte[length]; MemoryStream serializationStream = new MemoryStream(buffer, 0, length, true, true); formatter.Serialize(serializationStream, data); serializationStream.Flush(); serializationStream.Close(); this.WriteBytes(buffer, offset); } public void WriteBytes(byte[] data, int offset) { for (int i = 0; i < data.Length; i++) { Marshal.WriteByte(this.mappedView, offset + i, data[i]); } } public object ReadDeserialize(int offset, int length) { byte[] data = new byte[length]; this.ReadBytes(data, offset); BinaryFormatter formatter = new BinaryFormatter(); MemoryStream serializationStream = new MemoryStream(data, 0, length, true, true); object obj2 = formatter.Deserialize(serializationStream); serializationStream.Close(); return obj2; } public void ReadBytes(byte[] data, int offset) { for (int i = 0; i < data.Length; i++) { data[i] = Marshal.ReadByte(this.mappedView, offset + i); } }
5、设置、获取域值:
public static void SetDomainValue(string key, object password) { AppDomain.CurrentDomain.SetData(key, password); } public static T GetDomainValue<T>(string key) { object value=AppDomain.CurrentDomain.GetData(key) ; if(value==null)return default(T); return (T)value; }
4、依据类型名称创建委托并执行
public delegate object MethodInvoker(); public delegate Object ParamsConstructorDelegate(params object[] parameters); public static object NewInstance(Type type) { return CreateDelegate(type.GetConstructor(Type.EmptyTypes)).Invoke(); } private static MethodInvoker CreateDelegate(ConstructorInfo target) { var dynamic = new DynamicMethod(string.Empty, typeof (object), new Type[0], target.DeclaringType); ILGenerator il = dynamic.GetILGenerator(); il.DeclareLocal(target.DeclaringType); il.Emit(OpCodes.Newobj, target); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Ret); return (MethodInvoker) dynamic.CreateDelegate(typeof (MethodInvoker)); } public static object NewInstance(Type type, params object[] parameters) { return BuildParamterConstructorDelegate(type).Invoke(parameters); } private static ParamsConstructorDelegate BuildParamterConstructorDelegate(Type type) { var mthd = new DynamicMethod(".ctor", type, new[] {typeof (object[])}); ILGenerator il = mthd.GetILGenerator(); ConstructorInfo ctor = type.GetConstructors()[0]; // not very robust, but meh... ParameterInfo[] ctorParams = ctor.GetParameters(); for (int i = 0; i < ctorParams.Length; i++) { il.Emit(OpCodes.Ldarg_0); switch (i) { case 0: il.Emit(OpCodes.Ldc_I4_0); break; case 1: il.Emit(OpCodes.Ldc_I4_1); break; case 2: il.Emit(OpCodes.Ldc_I4_2); break; case 3: il.Emit(OpCodes.Ldc_I4_3); break; case 4: il.Emit(OpCodes.Ldc_I4_4); break; case 5: il.Emit(OpCodes.Ldc_I4_5); break; case 6: il.Emit(OpCodes.Ldc_I4_6); break; case 7: il.Emit(OpCodes.Ldc_I4_7); break; case 8: il.Emit(OpCodes.Ldc_I4_8); break; default: il.Emit(OpCodes.Ldc_I4, i); break; } il.Emit(OpCodes.Ldelem_Ref); Type paramType = ctorParams[i].ParameterType; il.Emit(paramType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, paramType); } il.Emit(OpCodes.Newobj, ctor); il.Emit(OpCodes.Ret); return (ParamsConstructorDelegate) mthd.CreateDelegate(typeof (ParamsConstructorDelegate)); }
依据反射创建实例:
private static void InitUCBookCommonDll() { string dllItem = "My.Common"; Assembly assembly = null; try { assembly = Assembly.Load(dllItem); } catch (Exception) { PrintLog(string.Format("加载程序集-{0}错误!", dllItem)); } int index = dllItem.IndexOf("My"); if (index != -1) { string typeName = dllItem.Substring(index) + ".LibLoader"; Type type = assembly.GetType(typeName); if (type != null) FastActivator.NewInstance(type); } }
3、注册、发送Windows消息:
[DllImport("user32", SetLastError = true)] public static extern int RegisterWindowMessage(string message);
[DllImport("user32", SetLastError = true)] private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
2、通过类型名称、方法名称,执行方法:
public static void MethodInvoker(object obj, string methodName, params object[] args) { Type type = obj.GetType(); MethodInfo methodInfo = type.GetMethod(methodName); methodInfo.Invoke(obj, args); }
1、通过类型名称,创建实例:
public static object CreateInstance(string typeFullName, params object[] args) { Type type = Type.GetType(typeFullName); return Activator.CreateInstance(type, args); }