Recently while browsing Microsoft Codeplex projects I’ve found another good thing. They call it ‘Fast Reflection Library’, it is written in C# and its purpose is to provide the same as part of the reflection features like executing method dynamically but give simple and faster implementations.
Example of usage:
using System;
using System.Reflection;
using FastReflectionLib;
namespace SimpleConsole
{
class Program
{
static void Main(string[] args)
{
PropertyInfo propertyInfo = typeof(string).GetProperty("Length");
MethodInfo methodInfo = typeof(string).GetMethod("Contains");
string s = "Hello World!";
// get value by normal reflection
int length1 = (int)propertyInfo.GetValue(s, null);
// get value by the extension method from FastReflectionLib,
// which is much faster
int length2 = (int)propertyInfo.FastGetValue(s);
// invoke by normal reflection
bool result1 = (bool)methodInfo.Invoke(s, new object[] { "Hello" });
// invoke by the extension method from FastReflectionLib,
// which is much faster
bool result2 = (bool)methodInfo.FastInvoke(s, new object[] { "Hello" });
}
}
}
Although DataObjects.Net contains its own implementation of fast reflection (see: Xtensive.Core.Reflection), I like in this one the following aspects: clear & minimalistic design, simple and obvious API & ease of use. It took several seconds to understand the general idea and start using it. I think that this might be a good example of how library should look like: it should solve 1 concrete problem in most efficient way and do nothing more in order not to turn into another dinosaur.
No comments:
Post a Comment