Teaq.FastReflection is designed to mimic the .NET Reflection API's closely, so usage will be familiar in most cases. To access fast reflection, you simply do as follows:

var typeDescription = typeof(MyType).TypeHandle.GetTypeDescription();

From this entry point, you can get property, field and method accessors that are similar in use to PropertyInfo, FieldInfo, and MethodInfo classes. In addition, you can reflect and use constructors.

A common use is to get or set a property value on an instance of some type at runtime. The Teaq.FastReflection API looks similar to System.Reflection, as follows:

// assume "myInstance" is "MyType" per above, and you wish to enumerate and get the property values:
var allPublicProps = typeDescription.GetProperties();
foreach (var prop in allPublicProps)
{
    // value is an object, and value types will be boxed.    
    var value = prop.GetValue(myInstance);
    // do something useful with value...
}

All reflection methods that acquire handles on properties, methods or constructors take parameters that affect what is exposed via the API, using the System.Reflection.BindingFlags enumeration.

Another common use for performance sensitive code is to instantiate an instance by reflecting the constructor. Oddly, when constraining a generic type to require a default constructor (e.g. T where T: new()), the compiled IL results in an equivalent to Activator.CreateInstance() (at least as of .NET 4.5 and CSharp 5.0). For hot path code, this isn't great as you are paying full reflection cost. Instead, you can do the following and pay the cost of a dictionary lookup and a cast operation:

// The following gets the constructors and properties, but in this case we care about the constructors:
var description = t.TypeHandle.GetTypeDescription(MemberTypes.Constructor | MemberTypes.Property);

// Presuming your type 'T' is constrained by new(), this is compiler validated, 
// and it will be faster than Activator.CreateInstance(). 
// Also assuming T: class, for the pedantic reader:
var entity = description.CreateInstance() as T;

There is also the ability to call constructors with arguments, and to invoke methods by name. The use of these capabilities requires some knowledge of the type you are working with. In some cases it is useful to discover property types and apply cross-cutting policies, as you might do with plain old reflection:

// assume "myInstance" is "MyType" per above, and you wish to enumerate and get the property values:
var allPublicProps = typeDescription.GetProperties();
foreach (var prop in allPublicProps)
{
    var typeOfProperty = prop.PropertyType;
    var nameOfPropery = prop.MemberName;
    // do something useful 
    // something useful? how about null checking, and setting a default?
    if (typeOfProperty == typeof(string))
    {
       var value = prop.GetValue(myInstance) as string;
       if (value == null)
       {
          prop.SetValue(myInstance, string.Empty);
       }
    }
}

Invoking a method on a type is very similar; ensure you include Methods when specifying desired member types, and you can invoke methods late bound by name. To do this, you "GetMethods" in a manner similar to how you "GetProperties". The resulting type is a MethodDescription, which will present an API similar to System.Reflection.MethodInfo.

Last edited Apr 4, 2016 at 4:08 AM by bkjuice, version 8