Skip to content

Commit

Permalink
+ win rt compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
prabirshrestha committed Nov 5, 2011
1 parent 48e7985 commit e483725
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ReflectionUtils-WinRT/App.xaml
@@ -1,3 +1,3 @@
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ReflectionUtils_WinRT.App" /> x:Class="ReflectionUtils_WinRT.App" />
2 changes: 1 addition & 1 deletion src/ReflectionUtils-WinRT/MainPage.xaml
Expand Up @@ -7,7 +7,7 @@
d:DesignHeight="768" d:DesignWidth="1366"> d:DesignHeight="768" d:DesignWidth="1366">


<Grid x:Name="LayoutRoot" Background="#FF0C0C0C"> <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">

<TextBox AcceptsReturn="True" x:Name="result"></TextBox>
</Grid> </Grid>


</UserControl> </UserControl>
178 changes: 178 additions & 0 deletions src/ReflectionUtils-WinRT/MainPage.xaml.cs
Expand Up @@ -6,14 +6,192 @@
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Data;
using System.Reflection;
using ReflectionUtils;


namespace ReflectionUtils_WinRT namespace ReflectionUtils_WinRT
{ {
partial class MainPage 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() public MainPage()
{ {
InitializeComponent(); 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("");
} }
} }
} }
10 changes: 8 additions & 2 deletions src/ReflectionUtils-WinRT/ReflectionUtils-WinRT.csproj
Expand Up @@ -18,7 +18,7 @@
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>TRACE;DEBUG;REFLECTION_UTILS_WINRT</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
Expand All @@ -27,14 +27,20 @@
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE;REFLECTION_UTILS_WINRT</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included --> <!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
</ItemGroup> </ItemGroup>
<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"> <Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
</Compile> </Compile>
Expand Down
10 changes: 10 additions & 0 deletions src/ReflectionUtils/Program.cs
Expand Up @@ -18,6 +18,16 @@ static void Main(string[] args)
//int loops = int.Parse(Console.ReadLine()); //int loops = int.Parse(Console.ReadLine());


int loops = 10000; 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); CreateObjectUsingReflection(loops);
CreateObjectUsingDynamicMethodCall(loops); CreateObjectUsingDynamicMethodCall(loops);
Expand Down
45 changes: 43 additions & 2 deletions src/ReflectionUtils/ReflectionUtils.cs
Expand Up @@ -17,18 +17,29 @@ class ReflectionUtils
{ {
public static Attribute GetAttribute(MemberInfo info, Type type) 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)) if (info == null || type == null || !Attribute.IsDefined(info, type))
return null; return null;

return Attribute.GetCustomAttribute(info, type); return Attribute.GetCustomAttribute(info, type);
#endif
} }


public static Attribute GetAttribute(Type objectType, Type attributeType) 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)) if (objectType == null || attributeType == null || !Attribute.IsDefined(objectType, attributeType))
return null; return null;

return Attribute.GetCustomAttribute(objectType, attributeType); return Attribute.GetCustomAttribute(objectType, attributeType);
#endif
} }


public static bool IsTypeGenericeCollectionInterface(Type type) public static bool IsTypeGenericeCollectionInterface(Type type)
Expand All @@ -43,8 +54,13 @@ public static bool IsTypeGenericeCollectionInterface(Type type)


public static bool IsTypeDictionary(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)) if (typeof(IDictionary).IsAssignableFrom(type))
return true; return true;
#endif


if (!type.IsGenericType) if (!type.IsGenericType)
return false; return false;
Expand Down Expand Up @@ -119,8 +135,21 @@ public static object GetNewInstance(Type type)
c = (CtorDelegate)dynamicMethod.CreateDelegate(typeof(CtorDelegate)); c = (CtorDelegate)dynamicMethod.CreateDelegate(typeof(CtorDelegate));
ConstructorCache.Add(type, c); ConstructorCache.Add(type, c);
return 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 #else
ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
#endif
c = delegate { return constructorInfo.Invoke(null); }; c = delegate { return constructorInfo.Invoke(null); };
ConstructorCache.Add(type, c); ConstructorCache.Add(type, c);
return c(); return c();
Expand Down Expand Up @@ -194,7 +223,11 @@ static SetHandler CreateSetHandler(FieldInfo fieldInfo)


static GetHandler CreateGetHandler(PropertyInfo propertyInfo) static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
{ {
#if REFLECTION_UTILS_WINRT
MethodInfo getMethodInfo = propertyInfo.GetMethod;
#else
MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true); MethodInfo getMethodInfo = propertyInfo.GetGetMethod(true);
#endif
if (getMethodInfo == null) if (getMethodInfo == null)
return null; return null;
#if REFLECTION_UTILS_REFLECTIONEMIT #if REFLECTION_UTILS_REFLECTIONEMIT
Expand All @@ -209,14 +242,22 @@ static GetHandler CreateGetHandler(PropertyInfo propertyInfo)
getGenerator.Emit(OpCodes.Ret); getGenerator.Emit(OpCodes.Ret);


return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler)); return (GetHandler)dynamicGet.CreateDelegate(typeof(GetHandler));
#else
#if REFLECTION_UTILS_WINRT
return delegate(object instance) { return getMethodInfo.Invoke(instance, new Type[] { }); };
#else #else
return delegate(object instance) { return getMethodInfo.Invoke(instance, Type.EmptyTypes); }; return delegate(object instance) { return getMethodInfo.Invoke(instance, Type.EmptyTypes); };
#endif
#endif #endif
} }


static SetHandler CreateSetHandler(PropertyInfo propertyInfo) static SetHandler CreateSetHandler(PropertyInfo propertyInfo)
{ {
#if REFLECTION_UTILS_WINRT
MethodInfo setMethodInfo = propertyInfo.SetMethod;
#else
MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true); MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true);
#endif
if (setMethodInfo == null) if (setMethodInfo == null)
return null; return null;
#if REFLECTION_UTILS_REFLECTIONEMIT #if REFLECTION_UTILS_REFLECTIONEMIT
Expand Down

0 comments on commit e483725

Please sign in to comment.