Skip to content
Browse files

+ CreateGetHandler for field

  • Loading branch information...
1 parent a7f208b commit 2d359b65eab97675ac7adcc6056d58c3b2e77c86 @prabirshrestha prabirshrestha committed Apr 21, 2011
View
12 src/ReflectionUtils.sln
@@ -1,7 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReflectionUtils", "ReflectionUtils\ReflectionUtils.csproj", "{E9512755-2E8A-4FEE-A21A-AF246B10BFAE}"
+EndProject
Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {E9512755-2E8A-4FEE-A21A-AF246B10BFAE}.Debug|x86.ActiveCfg = Debug|x86
+ {E9512755-2E8A-4FEE-A21A-AF246B10BFAE}.Debug|x86.Build.0 = Debug|x86
+ {E9512755-2E8A-4FEE-A21A-AF246B10BFAE}.Release|x86.ActiveCfg = Release|x86
+ {E9512755-2E8A-4FEE-A21A-AF246B10BFAE}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
View
86 src/ReflectionUtils/Program.cs
@@ -1,14 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
using System.Text;
namespace ReflectionUtils
{
class Program
{
+ const BindingFlags BINDING_FLAGS = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
+ private static DateTime lastTestStartTime;
+ private static Type type = typeof(SimpleClass);
+ private static SimpleClass simpleClass = SimpleClass.CreateInstance();
+ private static FieldInfo fieldInfo = type.GetField("stringField", BINDING_FLAGS);
+
static void Main(string[] args)
{
+ Console.WriteLine("How Many Test Iterations Would You Like To Run?");
+ //int loops = int.Parse(Console.ReadLine());
+
+ int loops = 10000;
+
+ CreateObjectUsingReflection(loops);
+ CreateObjectUsingDynamicMethodCall(loops);
+
+ GetValueUsingReflection(loops);
+ GetValueUsingDynamicMethodCall(loops);
+ }
+
+ private static void CreateObjectUsingReflection(int loops)
+ {
+ StartTest("Begin CreateObjectUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ object result = Activator.CreateInstance(type);
+ }
+
+ EndTest("End CreateObjectUsingReflection");
+ }
+
+ // CreateObjectUsingDynamicMethodCall
+ private static void CreateObjectUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin CreateObjectUsingDynamicMethodCall");
+
+
+ for (int i = 0; i < loops; i++)
+ {
+ object result = ReflectionUtils.Instance.GetNewInstance(type);
+ }
+
+ EndTest("End CreateObjectUsingDynamicMethodCall");
+ }
+
+ // GetValueUsingReflection
+ private static void GetValueUsingReflection(int loops)
+ {
+ StartTest("Begin GetValueUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)fieldInfo.GetValue(simpleClass);
+ }
+
+ EndTest("End GetValueUsingReflection");
+ }
+
+ // GetValueUsingDynamicMethodCall
+ private static void GetValueUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin GetValueUsingDynamicMethodCall");
+
+ GetHandler getHandler = ReflectionUtils.CreateGetHandler(type, fieldInfo);
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)getHandler(simpleClass);
+ }
+
+ EndTest("End GetValueUsingDynamicMethodCall");
+ }
+
+ // StartTest
+ private static void StartTest(string message)
+ {
+ lastTestStartTime = DateTime.Now;
+ Console.WriteLine(message);
+ }
+
+ // EndTest
+ private static void EndTest(string message)
+ {
+ Console.WriteLine(message);
+ Console.WriteLine(DateTime.Now - lastTestStartTime);
+ Console.WriteLine("");
}
}
}
View
60 src/ReflectionUtils/ReflectionUtils.cs
@@ -0,0 +1,60 @@
+#define REFLECTION_EMIT
+
+using System;
+#if REFLECTION_EMIT
+using System.Reflection;
+using System.Reflection.Emit;
+#endif
+
+namespace ReflectionUtils
+{
+#if REFLECTION_EMIT
+ delegate object CtorDelegate();
+#endif
+
+ public delegate object GetHandler(object source);
+
+ public delegate void SetHandler(object source, object value);
+
+ public class ReflectionUtils
+ {
+ public readonly static ReflectionUtils Instance = new ReflectionUtils();
+
+#if REFLECTION_EMIT
+ readonly SafeDictionary<Type, CtorDelegate> _constructorCache = new SafeDictionary<Type, CtorDelegate>();
+#endif
+
+ public object GetNewInstance(Type type)
+ {
+#if REFLECTION_EMIT
+ CtorDelegate c;
+ if (_constructorCache.TryGetValue(type, out c))
+ return c();
+ DynamicMethod dynMethod = new DynamicMethod("_", type, null);
+ ILGenerator ilGen = dynMethod.GetILGenerator();
+
+ ilGen.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
+ ilGen.Emit(OpCodes.Ret);
+ c = (CtorDelegate)dynMethod.CreateDelegate(typeof(CtorDelegate));
+ _constructorCache.Add(type, c);
+ return c();
+#else
+ return Activator.CreateInstance(type);
+#endif
+ }
+
+ internal static GetHandler CreateGetHandler(Type type, FieldInfo fieldInfo)
+ {
+ DynamicMethod dynamicGet = new DynamicMethod("DynamicGet", typeof(object), new [] { typeof(object) }, type, true);
+ ILGenerator getGenerator = dynamicGet.GetILGenerator();
+
+ getGenerator.Emit(OpCodes.Ldarg_0);
+ getGenerator.Emit(OpCodes.Ldfld, fieldInfo);
+ if (type.IsValueType)
+ getGenerator.Emit(OpCodes.Box, type);
+ getGenerator.Emit(OpCodes.Ret);
+
+ return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler));
+ }
+ }
+}
View
5 src/ReflectionUtils/ReflectionUtils.csproj
@@ -5,7 +5,7 @@
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>54768422-1465-47ce-b806-a892a0b309d8</ProjectGuid>
+ <ProjectGuid>{E9512755-2E8A-4FEE-A21A-AF246B10BFAE}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ReflectionUtils</RootNamespace>
@@ -45,6 +45,9 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="ReflectionUtils.cs" />
+ <Compile Include="SafeDictionary.cs" />
+ <Compile Include="SimpleClass.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
37 src/ReflectionUtils/SafeDictionary.cs
@@ -0,0 +1,37 @@
+using System.Collections.Generic;
+
+namespace ReflectionUtils
+{
+ internal class SafeDictionary<TKey, TValue>
+ {
+ private readonly object _Padlock = new object();
+ private readonly Dictionary<TKey, TValue> _Dictionary = new Dictionary<TKey, TValue>();
+
+
+ public bool TryGetValue(TKey key, out TValue value)
+ {
+ return _Dictionary.TryGetValue(key, out value);
+ }
+
+ public TValue this[TKey key]
+ {
+ get
+ {
+ return _Dictionary[key];
+ }
+ }
+ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
+ {
+ return ((ICollection<KeyValuePair<TKey, TValue>>)_Dictionary).GetEnumerator();
+ }
+
+ public void Add(TKey key, TValue value)
+ {
+ lock (_Padlock)
+ {
+ if (_Dictionary.ContainsKey(key) == false)
+ _Dictionary.Add(key, value);
+ }
+ }
+ }
+}
View
19 src/ReflectionUtils/SimpleClass.cs
@@ -0,0 +1,19 @@
+namespace ReflectionUtils
+{
+ public class SimpleClass
+ {
+ // SimpleClass
+ public SimpleClass()
+ {
+ }
+
+ // StringField
+ private string stringField;
+
+ // CreateInstance
+ internal static SimpleClass CreateInstance()
+ {
+ return new SimpleClass();
+ }
+ }
+}

0 comments on commit 2d359b6

Please sign in to comment.
Something went wrong with that request. Please try again.