调试新模版

Literacy
构造函数

初始化对象属性,字段访问组件,建立大小写敏感的访问实例

var user = new { Name = "blqw", Sex = "" };
Literacy lit = new Literacy(user.GetType());
string name = (string)lit.Property["Name"].GetValue(user);
//name = "blqw"
View Code

初始化对象属性,字段访问组件,参数2指示是否需要区分大小写

默认初始化仅加载公共的实例属性
需要支持其他属性或字段需使用Load中的相关方法
var user = new { Name = "blqw", Sex = "" };
Literacy lit = new Literacy(user.GetType(),true);
string name = (string)lit.Property["Name"].GetValue(user);
//name = "blqw"
string name2 = (string)lit.Property["name"].GetValue(user);//不区分大小写
//name2 = "blqw"
View Code
方法

调用对象的无参构造函数,新建对象

Literacy lit = new Literacy(typeof(Guid));
Guid g = (Guid)lit.NewObject();
//g = 00000000-0000-0000-0000-000000000000
View Code
属性

加载更多属性或字段

默认初始化仅加载公共的实例属性
需要支持其他属性或字段需使用Load中的相关方法
string str = "abc";
Literacy lit = new Literacy(str.GetType());
lit.Load.NonPublicField();  //加载非公共的实例字段
Char c = (char)lit.Field["m_firstChar"].GetValue(str);
//c = 'a';
View Code

属性集合

默认初始化仅加载公共的实例属性
需要加载其他属性需使用Load中的相关方法
var user = new { Name = "blqw", Sex = "" };
Literacy lit = new Literacy(user.GetType());
string name = (string)lit.Property["Name"].GetValue(user);
//name = "blqw"
bool b = lit.Property["Sex"].TrySetValue(user, "");
//b = false user.Sex = "男"
lit.Property["Sex"].SetValue(user, "");
//抛出异常: throw new Exception("属性没有公开的Set访问器");
View Code

字段集合

默认值为null
加载字段需使用Load中的相关方法
string str = "abc";
Literacy lit = new Literacy(str.GetType());
lit.Load.NonPublicField();  //加载非公共的实例字段
Char c = (char)lit.Field["m_firstChar"].GetValue(str);
//c = 'a';
lit.Field["m_firstChar"].SetValue(str, 'A');
//str = "Abc";
View Code
静态方法

从缓存中获取一个Literacy对象,如果没有则新建缓存2013.08.30新

参数2指示,该Literacy对象是否需要区分大小写
这个方法是线程安全的
var user = new { Name = "blqw", Sex = "" };
Literacy lit1 = Literacy.Cache(user.GetType(), true);
Literacy lit2 = Literacy.Cache(user.GetType(), false);
Literacy lit3 = Literacy.Cache(user.GetType(), true);
bool b1 = lit1 == lit2;
bool b2 = lit1 == lit3;
View Code

IL构造一个用于调用对象构造函数的委托

参数1:获取构造函数的对象
参数2:构造函数的参数,默认null
LiteracyNewObject nb = Literacy.CreateNewObject(typeof(Guid));
Guid g = (Guid)nb();
//g = 00000000-0000-0000-0000-000000000000
View Code

IL构造一个用于获取对象属性值的委托

DataTable table = new DataTable();
table.TableName = "User";
LiteracyGetter get = Literacy.CreateGetter(table.GetType().GetProperty("TableName"));
var tableName = get(table);
//tableName = "User"
View Code

IL构造一个用于获取对象字段值的委托

LiteracyGetter get = Literacy.CreateGetter(typeof(string).GetField("m_firstChar", BindingFlags.NonPublic | BindingFlags.Instance));
char c = (char)get("123456789");
//c == '1'
View Code

IL构造一个用于设置对象属性值的委托

DataTable table = new DataTable();
table.TableName = "User";
LiteracySetter set = Literacy.CreateSetter(table.GetType().GetProperty("TableName"));
set(table,"NewTableName");
//table.TableName = "NewTableName"
View Code

IL构造一个用于设置对象字段值的委托

LiteracySetter set = Literacy.CreateSetter(typeof(string).GetField("m_firstChar", BindingFlags.NonPublic | BindingFlags.Instance));
string s = "123456789";
set(s,'a');
//s = "a23456789"
View Code

IL构造一个用于执行方法的委托

LiteracyCaller met = Literacy.CreateCaller(typeof(Guid).GetMethod("ToString", new[] { typeof(string) }));
string s = (string)met(Guid.NewGuid(), "N");
//s = "4b857b9807f900009e37726b10187d4c"
s = (string)met(Guid.NewGuid(), "");
//s = "4b857b98-07f9-0000-b18a-872db2409a43"
View Code
posted @ 2013-08-30 15:57  冰麟轻武  阅读(387)  评论(0编辑  收藏  举报