NClay提供的PropertyHandler
NClay提供基于Emit封装的PropertyHandler来代替反射操作,这样的操作方式比反射操作有着非常优异执行效率。下能通过测试来看一下其使用方式和性能:
class Program
{
static void
{
NClay.PerformanceTest test = new NClay.PerformanceTest(Console.Out);
test.AddPerformance(new PropertyHandlerTest(), new PropertyInfoTest());
test.AddTestName("NClayHandler", "Reflection");
test.Execute(1, false);
test.Execute(10);
test.Execute(100);
test.Execute(1000);
test.Execute(10000);
test.Execute(100000);
Console.Read();
}
}
public class User
{
private string mName;
public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
}
public class PropertyHandlerTest:NClay.Performance
{
static NClay.PropertyHandler mHandler = null;
protected override void OnExecute()
{
if (mHandler == null)
{
PropertyInfo p = typeof(User).GetProperty("Name");
mHandler = new NClay.PropertyHandler(p);
}
User user = new User();
mHandler.Set(user, "henry");
object value = mHandler.Get(user);
}
}
public class PropertyInfoTest:NClay.Performance
{
static PropertyInfo mProperty;
protected override void OnExecute()
{
if (mProperty == null)
{
mProperty = typeof(User).GetProperty("Name");
}
User user = new User();
mProperty.SetValue(user, "henry", null);
object value = mProperty.GetValue(user, null);
}
}