Added Server.Reflection for new Property, Method and Field for setting and getting or invoking methods for more speed.

This commit is contained in:
ravenal 2010-07-15 18:57:45 +00:00
parent 7c20337781
commit fbdf46d493
7 changed files with 608 additions and 0 deletions

View file

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;
namespace Server.Reflection
{
public static class BaseFieldAccessor
{
public static FieldSetInvokeHandler<V> SetFieldInvoker<V>( Type type, string fieldName )
{
FieldInfo field = type.GetField( fieldName );
if ( field == null )
return null;
return SetFieldInvoker<V>( type, field );
}
public static FieldGetInvokeHandler<V> GetFieldInvoker<V>( Type type, string fieldName )
{
FieldInfo field = type.GetField( fieldName );
if ( field == null )
return null;
return GetFieldInvoker<V>( type, field );
}
public static FieldSetInvokeHandler<V> SetFieldInvoker<V>( Type t, FieldInfo field )
{
DynamicMethod dm = new DynamicMethod( "Set" + field.Name, null, new Type[] { t, typeof( V ) }, t );
ILGenerator il = dm.GetILGenerator();
// Load the instance of the object (argument 0) onto the stack
il.Emit( OpCodes.Ldarg_0 );
il.Emit( OpCodes.Ldarg_1 );
// Load the value of the object's field (fi) onto the stack
il.Emit( OpCodes.Stfld, field );
// return the value on the top of the stack
il.Emit( OpCodes.Ret );
return (FieldSetInvokeHandler<V>)dm.CreateDelegate( typeof( FieldSetInvokeHandler<V> ) );
}
public static FieldGetInvokeHandler<V> GetFieldInvoker<V>( Type t, FieldInfo field )
{
// Member is a Field...
DynamicMethod dm = new DynamicMethod( "Get" + field.Name, typeof( V ), new Type[] { t }, t );
ILGenerator il = dm.GetILGenerator();
// Load the instance of the object (argument 0) onto the stack
il.Emit( OpCodes.Ldarg_0 );
// Load the value of the object's field (fi) onto the stack
il.Emit( OpCodes.Ldfld, field );
// return the value on the top of the stack
il.Emit( OpCodes.Ret );
return (FieldGetInvokeHandler<V>)dm.CreateDelegate( typeof( FieldGetInvokeHandler<V> ) );
}
}
}

View file

@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace Server.Reflection
{
public static class BaseMethodInvoker
{
public static InvokeHandler GetMethodInvoker( MethodInfo method )
{
DynamicMethod dynamicMethod = new DynamicMethod( string.Empty,
typeof( object ), new Type[] { typeof( object ), typeof( object[] ) }, method.DeclaringType.Module );
ILGenerator il = dynamicMethod.GetILGenerator();
ParameterInfo[] ps = method.GetParameters();
Type[] paramTypes = new Type[ps.Length];
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
paramTypes[i] = ps[i].ParameterType.GetElementType();
else
paramTypes[i] = ps[i].ParameterType;
}
LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];
for ( int i = 0; i < paramTypes.Length; i++ )
locals[i] = il.DeclareLocal( paramTypes[i], true );
for ( int i = 0; i < paramTypes.Length; i++ )
{
il.Emit( OpCodes.Ldarg_1 );
EmitFastInt( il, i );
il.Emit( OpCodes.Ldelem_Ref );
EmitCastToReference( il, paramTypes[i] );
il.Emit( OpCodes.Stloc, locals[i] );
}
if ( !method.IsStatic )
il.Emit( OpCodes.Ldarg_0 );
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
il.Emit( OpCodes.Ldloca_S, locals[i] );
else
il.Emit( OpCodes.Ldloc, locals[i] );
}
if ( method.IsStatic )
il.EmitCall( OpCodes.Call, method, null );
else
il.EmitCall( OpCodes.Callvirt, method, null );
if ( method.ReturnType == typeof( void ) )
il.Emit( OpCodes.Ldnull );
else
EmitBoxIfNeeded( il, method.ReturnType );
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
{
il.Emit( OpCodes.Ldarg_1 );
EmitFastInt( il, i );
il.Emit( OpCodes.Ldloc, locals[i] );
if ( locals[i].LocalType.IsValueType )
il.Emit( OpCodes.Box, locals[i].LocalType );
il.Emit( OpCodes.Stelem_Ref );
}
}
il.Emit( OpCodes.Ret );
InvokeHandler invoder = (InvokeHandler)dynamicMethod.CreateDelegate( typeof( InvokeHandler ) );
return invoder;
}
public static InvokeHandler GetMethodInvoker( Type type, string methodName )
{
MethodInfo methodInfo = type.GetMethod( methodName );
return GetMethodInvoker( methodInfo );
}
private static void EmitCastToReference( ILGenerator il, System.Type type )
{
if ( type.IsValueType )
{
il.Emit( OpCodes.Unbox_Any, type );
}
else
{
il.Emit( OpCodes.Castclass, type );
}
}
private static void EmitBoxIfNeeded( ILGenerator il, System.Type type )
{
if ( type.IsValueType )
{
il.Emit( OpCodes.Box, type );
}
}
private static void EmitFastInt( ILGenerator il, int value )
{
switch ( value )
{
case -1:
il.Emit( OpCodes.Ldc_I4_M1 );
return;
case 0:
il.Emit( OpCodes.Ldc_I4_0 );
return;
case 1:
il.Emit( OpCodes.Ldc_I4_1 );
return;
case 2:
il.Emit( OpCodes.Ldc_I4_2 );
return;
case 3:
il.Emit( OpCodes.Ldc_I4_3 );
return;
case 4:
il.Emit( OpCodes.Ldc_I4_4 );
return;
case 5:
il.Emit( OpCodes.Ldc_I4_5 );
return;
case 6:
il.Emit( OpCodes.Ldc_I4_6 );
return;
case 7:
il.Emit( OpCodes.Ldc_I4_7 );
return;
case 8:
il.Emit( OpCodes.Ldc_I4_8 );
return;
}
if ( value > -129 && value < 128 )
{
il.Emit( OpCodes.Ldc_I4_S, (SByte)value );
}
else
{
il.Emit( OpCodes.Ldc_I4, value );
}
}
}
}

View file

@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
namespace Server.Reflection
{
public static class BasePropertyAccessor
{
public static InvokeHandler GetPropertyInvoker( Type type, string propertyName )
{
MethodInfo mi = type.GetMethod( "get_" + propertyName );
if ( mi == null )
return null;
return BaseMethodInvoker.GetMethodInvoker( mi );
}
public static InvokeHandler SetPropertyInvoker( Type type, string propertyName )
{
PropertyInfo pi = type.GetProperty( propertyName );
if ( pi == null )
return null;
MethodInfo mi = type.GetMethod( "set_" + pi.Name );
if ( mi == null )
return null;
return GetMethodInvoker4Set( mi );
}
public static InvokeHandler GetPropertyInvoker( Type type, PropertyInfo property )
{
if ( property == null )
return null;
MethodInfo mi = type.GetMethod( "get_" + property.Name );
if ( mi == null )
return null;
return BaseMethodInvoker.GetMethodInvoker( mi );
}
public static InvokeHandler SetPropertyInvoker( Type type, PropertyInfo property )
{
if ( property == null )
return null;
MethodInfo mi = type.GetMethod( "set_" + property.Name );
if ( mi == null )
return null;
return GetMethodInvoker4Set( mi );
}
private static InvokeHandler GetMethodInvoker4Set( MethodInfo methodInfo )
{
DynamicMethod dynamicMethod = new DynamicMethod( string.Empty,
typeof( object ), new Type[] { typeof( object ), typeof( object[] ) }, methodInfo.DeclaringType.Module );
ILGenerator il = dynamicMethod.GetILGenerator();
ParameterInfo[] ps = methodInfo.GetParameters();
Type[] paramTypes = new Type[ps.Length];
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
paramTypes[i] = ps[i].ParameterType.GetElementType();
else
paramTypes[i] = ps[i].ParameterType;
}
LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];
for ( int i = 0; i < paramTypes.Length; i++ )
locals[i] = il.DeclareLocal( paramTypes[i], true );
for ( int i = 0; i < paramTypes.Length; i++ )
{
il.Emit( OpCodes.Ldarg_1 );
EmitFastInt( il, i );
il.Emit( OpCodes.Ldelem_Ref );
EmitCastToReference( il, paramTypes[i] );
il.Emit( OpCodes.Stloc, locals[i] );
}
if ( !methodInfo.IsStatic )
il.Emit( OpCodes.Ldarg_0 );
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
il.Emit( OpCodes.Ldloca_S, locals[i] );
else
il.Emit( OpCodes.Ldloc, locals[i] );
}
if ( methodInfo.IsStatic )
il.EmitCall( OpCodes.Call, methodInfo, null );
else
il.EmitCall( OpCodes.Callvirt, methodInfo, null );
if ( methodInfo.ReturnType == typeof( void ) )
il.Emit( OpCodes.Ldnull );
else
EmitBoxIfNeeded( il, methodInfo.ReturnType );
for ( int i = 0; i < paramTypes.Length; i++ )
{
if ( ps[i].ParameterType.IsByRef )
{
il.Emit( OpCodes.Ldarg_1 );
EmitFastInt( il, i );
il.Emit( OpCodes.Ldloc, locals[i] );
if ( locals[i].LocalType.IsValueType )
il.Emit( OpCodes.Box, locals[i].LocalType );
il.Emit( OpCodes.Stelem_Ref );
}
}
il.Emit( OpCodes.Ret );
InvokeHandler invoder = (InvokeHandler)dynamicMethod.CreateDelegate( typeof( InvokeHandler ) );
return invoder;
}
private static void EmitCastToReference( ILGenerator il, System.Type type )
{
if ( type.IsValueType )
{
il.Emit( OpCodes.Unbox_Any, type );
}
else
{
il.Emit( OpCodes.Castclass, type );
}
}
private static void EmitBoxIfNeeded( ILGenerator il, System.Type type )
{
if ( type.IsValueType )
{
il.Emit( OpCodes.Box, type );
}
}
private static void EmitFastInt( ILGenerator il, int value )
{
switch ( value )
{
case -1:
il.Emit( OpCodes.Ldc_I4_M1 );
return;
case 0:
il.Emit( OpCodes.Ldc_I4_0 );
return;
case 1:
il.Emit( OpCodes.Ldc_I4_1 );
return;
case 2:
il.Emit( OpCodes.Ldc_I4_2 );
return;
case 3:
il.Emit( OpCodes.Ldc_I4_3 );
return;
case 4:
il.Emit( OpCodes.Ldc_I4_4 );
return;
case 5:
il.Emit( OpCodes.Ldc_I4_5 );
return;
case 6:
il.Emit( OpCodes.Ldc_I4_6 );
return;
case 7:
il.Emit( OpCodes.Ldc_I4_7 );
return;
case 8:
il.Emit( OpCodes.Ldc_I4_8 );
return;
}
if ( value > -129 && value < 128 )
{
il.Emit( OpCodes.Ldc_I4_S, (SByte)value );
}
else
{
il.Emit( OpCodes.Ldc_I4, value );
}
}
}
}

View file

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Server.Reflection
{
public sealed class FieldAccessor<V>
{
private Type m_Type;
private FieldInfo m_Field;
private bool m_FieldEmpty;
public Type Type { get { return m_Type; } }
public FieldInfo Field { get { return m_Field; } }
public bool IsFieldEmpty { get { return m_FieldEmpty; } }
private FieldGetInvokeHandler<V> m_GetMethodHandler;
private FieldSetInvokeHandler<V> m_SetMethodHandler;
public FieldAccessor( Type type, FieldInfo field )
{
m_Type = type;
m_Field = field;
if ( field == null )
{
m_FieldEmpty = true;
return;
}
m_FieldEmpty = false;
m_GetMethodHandler = BaseFieldAccessor.GetFieldInvoker<V>( m_Type, field );
m_SetMethodHandler = BaseFieldAccessor.SetFieldInvoker<V>( m_Type, field );
}
public V GetValue( object target )
{
if ( m_FieldEmpty )
return default( V );
return m_GetMethodHandler( target );
}
public void SetValue( object target, V value )
{
if ( m_FieldEmpty )
return;
m_SetMethodHandler( target, value );
}
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Server.Reflection
{
public delegate object InvokeHandler( object target, params object[] paramters );
public delegate T InvokeHandler<T>( object target, params object[] paramters );
public delegate V FieldGetInvokeHandler<V>( object target );
public delegate void FieldSetInvokeHandler<V>( object target, V value );
}

View file

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Server.Reflection
{
public sealed class MethodInvoker
{
private MethodInfo m_Method;
private bool m_MethodEmpty;
public MethodInfo Method { get { return m_Method; } }
public bool IsMethodEmpty { get { return m_MethodEmpty; } }
private InvokeHandler m_MethodHandler;
public MethodInvoker( Type type, string methodname )
{
MethodInfo mi = type.GetMethod( methodname );
if ( mi != null )
m_MethodHandler = BaseMethodInvoker.GetMethodInvoker( mi );
m_MethodEmpty = (mi == null) | (m_MethodHandler == null);
}
public MethodInvoker( MethodInfo method )
{
m_Method = method;
if ( method != null )
m_MethodHandler = BaseMethodInvoker.GetMethodInvoker( method );
m_MethodEmpty = (method == null) | (m_MethodHandler == null);
}
public object Invoke( object target, params object[] parameters )
{
if ( m_MethodEmpty )
return null;
return m_MethodHandler.Invoke( target, parameters );
}
public IAsyncResult BeginInvoke( object target, object[] parameters, AsyncCallback callback, object obj )
{
if ( m_MethodEmpty )
return null;
return m_MethodHandler.BeginInvoke( target, parameters, callback, obj );
}
public object EndInvoke( IAsyncResult asyncResult )
{
if ( m_MethodEmpty )
return null;
return m_MethodHandler.EndInvoke( asyncResult );
}
}
}

View file

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Server.Reflection
{
public sealed class PropertyAccessor
{
private Type m_Type;
private PropertyInfo m_Property;
private bool m_PropertyEmpty;
public Type Type { get { return m_Type; } }
public PropertyInfo PropertyInfo { get { return m_Property; } }
public bool IsPropertyEmpty { get { return m_PropertyEmpty; } }
private InvokeHandler m_GetMethodHandler;
private InvokeHandler m_SetMethodHandler;
public PropertyAccessor( Type type, string propertyName )
{
m_Type = type;
m_GetMethodHandler = BasePropertyAccessor.GetPropertyInvoker( m_Type, propertyName );
m_SetMethodHandler = BasePropertyAccessor.SetPropertyInvoker( m_Type, propertyName );
if ( m_GetMethodHandler == null || m_SetMethodHandler == null )
m_PropertyEmpty = true;
}
public PropertyAccessor( Type type, PropertyInfo property )
{
m_Type = type;
m_Property = property;
if ( property == null )
{
m_PropertyEmpty = true;
return;
}
m_GetMethodHandler = BasePropertyAccessor.GetPropertyInvoker( m_Type, property );
m_SetMethodHandler = BasePropertyAccessor.SetPropertyInvoker( m_Type, property );
if ( m_GetMethodHandler == null || m_SetMethodHandler == null )
m_PropertyEmpty = true;
}
public object GetValue( object target, params object[] parameters )
{
if ( m_PropertyEmpty )
return null;
return m_GetMethodHandler( target, parameters );
}
public void SetValue( object target, params object[] parameters )
{
if ( m_PropertyEmpty )
return;
m_SetMethodHandler( target, parameters );
}
}
}