//目的、动态切换逻辑
//封闭变化的程序
//动态切换数据库等
//根据字符串、实例化指定程序集下命名空间下的类
Assembly.Load("程序集").CreateInstance("命名空间.类名");
//配合Interface使用
实例.GetType();//获取类型的类型
typeof(Class); //等效和 实例.GetType(); 等效
type.GetProperties(); //属性
type.GetMethods();//方法
type.GetMembers();//成员 字段
type.GetEvents();//事件
Activator.CreateInstance(Type type);//动态实例化一个指定类型的实体类
///反射获取常量的值
var mfield = itype.GetField(常量名);
mb.SortName = mfield.GetValue(对象object).ToString();
//获取方法 不考虑父级成员
//加上BindingFlags.DeclaredOnly 即可
MethodInfo[] MethodItem = itype.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
foreach (var i in Assembly.Load("你的程序集").GetTypes())
{
Console.Write(i.FullName);
Console.Write("|");
Console.WriteLine(i.Name);
}
网上也有很多朋友遇到这种问题
做服务端的盆友、要求给APP端的数据
不要带Null、 APP端处理麻烦
且不是过滤Null属性、需要保留字段、Null给成空字符串。
或者做其他你需要的自定义的处理
比如直接全局Json 序列化 性别 成 “汉子”“妹子”
全局区别数据格式对应不同平台等,
等高级DIV序列化方式
有些朋友、使用在实体类去标记初始值的方式。
有些朋友、使用重写Json.Net/Newtonsoft.Json的方法
还有些朋友、用其他的方式、
但是效率、以及方不方便上
不是很理想和实用
所以还是用反射实现好了、效率上也比较理想,
完美递归复杂实体类之后、再进行Json序列化、
实验证明这样达到目的效率更高、
方式最终精简如下
By 皓月
范例如下:
public static T MySet<T>(T newi)
{
var t1 = newi.GetType();//
//typeof(Class) //获取类型的类型
var t1Properties = t1.GetProperties(); //属性集合
//var t1Members = t1.GetMembers();//成员 字段
//var t1m = t1.GetMethods();//类型的方法的类型
//t1.GetEvents();//类型的事件
foreach (var t in t1Properties)//遍历属性
{
Type t_type = t.GetType();
object t_Value = t.GetValue(newi, null);
if (t_Value == null)//判断是否为空
{
//此处添加任何你需要的自定处理
if (
public static T SetReflectionObject<T>(T t)
{
System.Reflection.PropertyInfo[]
//properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
properties = t.GetType().GetProperties();
if (t.GetType().Name.StartsWith("List"))
{
System.Collections.ICollection MyIlist = t as System.Collections.ICollection;
foreach (var l in MyIlist)
{
SetReflectionObject(l);
}
return t;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
if (item.GetValue(t, null) == null)
{
if (item.PropertyType.Name.StartsWith("String"))
{
item.SetValue(t, "");
}
if (item.PropertyType.Name.StartsWith("List"))
{
item.SetValue(t, Activator.CreateInstance(item.PropertyType));
}
}
else if (item.PropertyType.Name.StartsWith("List"))
{
System.Collections.ICollection Ilist = item.GetValue(t, null) as System.Collections.ICollection;
foreach (var l in Ilist)
{
SetReflectionObject(l);
}
}
}
return t;
}
using System;
using System.Runtime.InteropServices;
namespace CrtAsmOfEmbeded_x86
{
static partial class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool VirtualProtect(byte[] lpAddress, int dwSize, uint flNewProtect, out uint lpflOldProtect);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr CallWindowProc(byte[] lpPrevWndFunc, int hWnd, int Msg, int wParam, int lParam);
private const int NULL = 0;
private const int PAGE_EXECUTE_READWRITE = 64;
}
static partial class Program
{
private static readonly byte[] buf_asm = { 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64, 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171, 184, 0, 0, 0, 0, 51, 210, 15, 162, 137, 85, 252, 137, 69, 248, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 137, 85, 244, 137, 69, 240, 139, 69, 252, 137, 69, 236, 139, 69, 24
http://www.52pojie.cn/thread-289232-1-1.html
这里就需要修复了
看了论坛的修复教程 很麻烦
这里我们只需要CFF Explorer
打开CFF Explorer
拖入dump完成的文件
class A1
{
public A1()
{ Console.WriteLine("1"); }
~A1()
{ Console.WriteLine("1"); }
}
class A2
{
public A2()
{ Console.WriteLine("2"); }
~A2()
{ Console.WriteLine("2"); }
}
class A3
{
public A3()
{ Console.WriteLine("3"); }
~A3()
{ Console.WriteLine("3"); }
}
class Test
{
public void Get23333()
{
new A1();
new A2();
new A3();
GC.Collect();
}
}
[DllImport("NvTemp.dll", EntryPoint = "GetTemperature")]
public static extern bool GetTemperature(ulong[] temp);
//反调试 程序一旦被调试 则跳出
if (System.Diagnostics.Debugger.IsAttached)
{
//也可以添加其他操作
Application.Current.Shutdown();//比如关闭WPF程序
}