Skip to content
Browse files

+ win rt compatibility

  • Loading branch information...
1 parent 48e7985 commit e483725e36d859d63e03c6595d7cca6096592f8c @prabirshrestha prabirshrestha committed Nov 5, 2011
View
2 src/ReflectionUtils-WinRT/App.xaml
@@ -1,3 +1,3 @@
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- x:Class="ReflectionUtils_WinRT.App" />
+ x:Class="ReflectionUtils_WinRT.App" />
View
2 src/ReflectionUtils-WinRT/MainPage.xaml
@@ -7,7 +7,7 @@
d:DesignHeight="768" d:DesignWidth="1366">
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
-
+ <TextBox AcceptsReturn="True" x:Name="result"></TextBox>
</Grid>
</UserControl>
View
178 src/ReflectionUtils-WinRT/MainPage.xaml.cs
@@ -6,14 +6,192 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
+using System.Reflection;
+using ReflectionUtils;
namespace ReflectionUtils_WinRT
{
partial class MainPage
{
+ //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.GetTypeInfo().GetDeclaredField("stringField");
+ private static PropertyInfo propertyInfo = type.GetTypeInfo().GetDeclaredProperty("stringProperty");
+
public MainPage()
{
InitializeComponent();
+
+ int loops = 10000;
+
+ CreateObjectUsingReflection(loops);
+ CreateObjectUsingDynamicMethodCall(loops);
+
+ SetFieldValueUsingReflection(loops);
+ SetFieldValueUsingDynamicMethodCall(loops);
+
+ SetFieldValueUsingReflection(loops);
+ SetFieldValueUsingDynamicMethodCall(loops);
+
+ GetFieldValueUsingReflection(loops);
+ GetFieldValueUsingDynamicMethodCall(loops);
+
+ GetPropertyValueUsingReflection(loops);
+ GetPropertyValueUsingDynamicMethodCall(loops);
+ }
+
+ private void CreateObjectUsingReflection(int loops)
+ {
+ StartTest("Begin CreateObjectUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ object result = Activator.CreateInstance(type);
+ }
+
+ EndTest("End CreateObjectUsingReflection");
+ }
+
+ // CreateObjectUsingDynamicMethodCall
+ private void CreateObjectUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin CreateObjectUsingDynamicMethodCall");
+
+
+ for (int i = 0; i < loops; i++)
+ {
+ object result = CacheResolver.GetNewInstance(type);
+ }
+
+ EndTest("End CreateObjectUsingDynamicMethodCall");
+ }
+
+ private void SetFieldValueUsingReflection(int loops)
+ {
+ StartTest("Begin SetFieldValueUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ fieldInfo.SetValue(simpleClass, "test");
+ }
+
+ EndTest("End SetFieldValueUsingReflection");
+ }
+
+ // SetValueUsingDynamicMethodCall
+ private void SetFieldValueUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin SetFieldValueUsingDynamicMethodCall");
+ SetHandler setHandler = new CacheResolver.MemberMap(fieldInfo).Setter;
+
+ for (int i = 0; i < loops; i++)
+ {
+ setHandler(simpleClass, "test");
+ }
+
+ EndTest("End SetFieldValueUsingDynamicMethodCall");
+ }
+
+ private void SetPropertyValueUsingReflection(int loops)
+ {
+ StartTest("Begin SetPropertyValueUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ propertyInfo.SetValue(simpleClass, "test", null);
+ }
+
+ EndTest("End SetPropertyValueUsingReflection");
+ }
+
+ // SetValueUsingDynamicMethodCall
+ private void SetPropertyValueUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin SetPropertyValueUsingDynamicMethodCall");
+ SetHandler setHandler = new CacheResolver.MemberMap(propertyInfo).Setter;
+
+ for (int i = 0; i < loops; i++)
+ {
+
+ setHandler(simpleClass, "test");
+ }
+
+ EndTest("End SetPropertyValueUsingDynamicMethodCall");
+ }
+
+ // GetValueUsingReflection
+ private void GetFieldValueUsingReflection(int loops)
+ {
+ StartTest("Begin GetFieldValueUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)fieldInfo.GetValue(simpleClass);
+ }
+
+ EndTest("End GetFieldValueUsingReflection");
+ }
+
+ // GetValueUsingDynamicMethodCall
+ private void GetFieldValueUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin GetFieldValueUsingDynamicMethodCall");
+ GetHandler getHandler = new CacheResolver.MemberMap(fieldInfo).Getter;
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)getHandler(simpleClass);
+ }
+
+ EndTest("End GetFieldValueUsingDynamicMethodCall");
+ }
+
+ // GetValueUsingReflection
+ private void GetPropertyValueUsingReflection(int loops)
+ {
+ StartTest("Begin GetPropertyValueUsingReflection");
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)propertyInfo.GetValue(simpleClass, null);
+ }
+
+ EndTest("End GetPropertyValueUsingReflection");
+ }
+
+ // GetValueUsingDynamicMethodCall
+ private void GetPropertyValueUsingDynamicMethodCall(int loops)
+ {
+ StartTest("Begin GetPropertyValueUsingDynamicMethodCall");
+ GetHandler getHandler = new CacheResolver.MemberMap(propertyInfo).Getter;
+
+ for (int i = 0; i < loops; i++)
+ {
+ string value = (string)getHandler(simpleClass);
+ }
+
+ EndTest("End GetPropertyValueUsingDynamicMethodCall");
+ }
+
+ // StartTest
+ private void StartTest(string message)
+ {
+ lastTestStartTime = DateTime.Now;
+ result.Text += message + Environment.NewLine;
+ //Console.WriteLine(message);
+ }
+
+ // EndTest
+ private void EndTest(string message)
+ {
+ result.Text += message + Environment.NewLine;
+ result.Text += (DateTime.Now - lastTestStartTime) + Environment.NewLine;
+ result.Text += Environment.NewLine;
+ //Console.WriteLine(message);
+ //Console.WriteLine(DateTime.Now - lastTestStartTime);
+ //Console.WriteLine("");
}
}
}
View
10 src/ReflectionUtils-WinRT/ReflectionUtils-WinRT.csproj
@@ -18,7 +18,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
- <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <DefineConstants>TRACE;DEBUG;REFLECTION_UTILS_WINRT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
@@ -27,14 +27,20 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
- <DefineConstants>TRACE</DefineConstants>
+ <DefineConstants>TRACE;REFLECTION_UTILS_WINRT</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
</ItemGroup>
<ItemGroup>
+ <Compile Include="..\ReflectionUtils\ReflectionUtils.cs">
+ <Link>ReflectionUtils.cs</Link>
+ </Compile>
+ <Compile Include="..\ReflectionUtils\SimpleClass.cs">
+ <Link>SimpleClass.cs</Link>
+ </Compile>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
View
10 src/ReflectionUtils/Program.cs
@@ -18,6 +18,16 @@ static void Main(string[] args)
//int loops = int.Parse(Console.ReadLine());
int loops = 10000;
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(int)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(long)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(double)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(byte)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(float)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(short)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(decimal)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(bool)));
+ Console.WriteLine(typeof(IConvertible).IsAssignableFrom(typeof(Program)));
+ return;
CreateObjectUsingReflection(loops);
CreateObjectUsingDynamicMethodCall(loops);
View
45 src/ReflectionUtils/ReflectionUtils.cs
@@ -17,18 +17,29 @@ class ReflectionUtils
{
public static Attribute GetAttribute(MemberInfo info, Type type)
{
+#if REFLECTION_UTILS_WINRT
+ if (info == null || type == null || !info.IsDefined(type))
+ return null;
+ return info.GetCustomAttribute(type);
+#else
if (info == null || type == null || !Attribute.IsDefined(info, type))
return null;
-
return Attribute.GetCustomAttribute(info, type);
+#endif
}
public static Attribute GetAttribute(Type objectType, Type attributeType)
{
+
+#if REFLECTION_UTILS_WINRT
+ if (objectType == null || attributeType == null || !objectType.GetTypeInfo().IsDefined(attributeType))
+ return null;
+ return objectType.GetTypeInfo().GetCustomAttribute(attributeType);
+#else
if (objectType == null || attributeType == null || !Attribute.IsDefined(objectType, attributeType))
return null;
-
return Attribute.GetCustomAttribute(objectType, attributeType);
+#endif
}
public static bool IsTypeGenericeCollectionInterface(Type type)
@@ -43,8 +54,13 @@ public static bool IsTypeGenericeCollectionInterface(Type type)
public static bool IsTypeDictionary(Type type)
{
+#if REFLECTION_UTILS_WINRT
+ if (typeof(IDictionary<,>).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
+ return true;
+#else
if (typeof(IDictionary).IsAssignableFrom(type))
return true;
+#endif
if (!type.IsGenericType)
return false;
@@ -120,7 +136,20 @@ public static object GetNewInstance(Type type)
ConstructorCache.Add(type, c);
return c();
#else
+#if REFLECTION_UTILS_WINRT
+ IEnumerable<ConstructorInfo> constructorInfos = type.GetTypeInfo().DeclaredConstructors;
+ ConstructorInfo constructorInfo = null;
+ foreach (ConstructorInfo item in constructorInfos) // FirstOrDefault()
+ {
+ if (item.GetParameters().Length == 0) // Default ctor - make sure it doesn't contain any parameters
+ {
+ constructorInfo = item;
+ break;
+ }
+ }
+#else
ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
+#endif
c = delegate { return constructorInfo.Invoke(null); };
ConstructorCache.Add(type, c);
return c();
@@ -194,7 +223,11 @@ static SetHandler CreateSetHandler(FieldInfo fieldInfo)
static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
{
+#if REFLECTION_UTILS_WINRT
+ MethodInfo getMethodInfo = propertyInfo.GetMethod;
+#else
MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true);
+#endif
if (getMethodInfo == null)
return null;
#if REFLECTION_UTILS_REFLECTIONEMIT
@@ -210,13 +243,21 @@ static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler));
#else
+#if REFLECTION_UTILS_WINRT
+ return delegate(object instance) { return getMethodInfo.Invoke(instance, new Type[] { }); };
+#else
return delegate(object instance) { return getMethodInfo.Invoke(instance, Type.EmptyTypes); };
#endif
+#endif
}
static SetHandler CreateSetHandler(PropertyInfo propertyInfo)
{
+#if REFLECTION_UTILS_WINRT
+ MethodInfo setMethodInfo = propertyInfo.SetMethod;
+#else
MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true);
+#endif
if (setMethodInfo == null)
return null;
#if REFLECTION_UTILS_REFLECTIONEMIT

0 comments on commit e483725

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