Skip to content

Added ability to map private props to columns #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
os: Visual Studio 2015

build:
project: SQLite.Net.OSS.sln

Expand Down
33 changes: 33 additions & 0 deletions src/ReflectionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform {
public abstract class ReflectionService : IReflectionService {
protected internal ReflectionService() { }

public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType) {
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public IEnumerable<PropertyInfo> GetDecoratedPrivateInstanceProperties(Type mappedType, Type attributeType) {
return mappedType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty)
.Where(x => x.GetSetMethod(false) != null || x.GetCustomAttribute(attributeType) != null);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member) {
if (member.MemberType == MemberTypes.Property) {
var m = (PropertyInfo)member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field) {
var m = (FieldInfo)member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
}
24 changes: 2 additions & 22 deletions src/SQLite.Net.Platform.Generic/ReflectionServiceGeneric.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform.Generic
{
public class ReflectionServiceGeneric : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
public class ReflectionServiceGeneric : ReflectionService { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\ReflectionService.cs">
<Link>ReflectionService.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionServiceGeneric.cs" />
<Compile Include="SQLiteApiGeneric.cs" />
Expand Down
31 changes: 2 additions & 29 deletions src/SQLite.Net.Platform.OSX/ReflectionServiceOSX.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform.OSX
namespace SQLite.Net.Platform.OSX
{
public class ReflectionServiceOSX : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
public class ReflectionServiceOSX : ReflectionService { }
}
13 changes: 8 additions & 5 deletions src/SQLite.Net.Platform.OSX/SQLite.Net.Platform.OSX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<Reference Include="Microsoft.Build.Utilities.v4.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\ReflectionService.cs">
<Link>ReflectionService.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionServiceOSX.cs" />
<Compile Include="SQLiteApiOSX.cs" />
Expand All @@ -60,10 +63,10 @@
<Target Name="AfterBuild">
</Target>
-->
<ItemGroup>
<Folder Include="i386\" />
<Folder Include="x86_64\" />
</ItemGroup>
<ItemGroup>
<Folder Include="i386\" />
<Folder Include="x86_64\" />
</ItemGroup>
<ItemGroup>
<None Include="i386\libsqlite3_for_net.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -81,4 +84,4 @@
<Name>SQLite.Net</Name>
</ProjectReference>
</ItemGroup>
</Project>
</Project>
31 changes: 2 additions & 29 deletions src/SQLite.Net.Platform.Win32/ReflectionServiceWin32.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform.Win32
namespace SQLite.Net.Platform.Win32
{
public class ReflectionServiceWin32 : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
public class ReflectionServiceWin32 : ReflectionService { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\ReflectionService.cs">
<Link>ReflectionService.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionServiceWin32.cs" />
<Compile Include="SQLiteApiWin32.cs" />
Expand Down
42 changes: 27 additions & 15 deletions src/SQLite.Net.Platform.WinRT/ReflectionServiceWinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,39 @@ namespace SQLite.Net.Platform.WinRT
public class ReflectionServiceWinRT : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
if (mappedType == null) throw new ArgumentNullException(nameof(mappedType));

return from p in mappedType.GetRuntimeProperties()
where
((p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic) ||
(p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic))
select p;
}

public IEnumerable<PropertyInfo> GetDecoratedPrivateInstanceProperties(Type mappedType, Type attributeType)
{
return from p in mappedType.GetRuntimeProperties()
where
((p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic) ||
(p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic))
select p;
where (
(p.GetMethod != null && p.GetMethod.IsPrivate) || (p.SetMethod != null && p.SetMethod.IsPrivate) ||
p.GetCustomAttribute(attributeType) != null
)
select p;
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member is PropertyInfo)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member is FieldInfo)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.DeclaringType);
if (member is PropertyInfo)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member is FieldInfo)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.DeclaringType);
}
}
}
47 changes: 28 additions & 19 deletions src/SQLite.Net.Platform.WindowsPhone8/ReflectionServiceWP8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@
namespace SQLite.Net.Platform.WindowsPhone8
{
public class ReflectionServiceWP8 : IReflectionService
{
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
if (mappedType == null)
{
throw new ArgumentNullException("mappedType");
}
if (mappedType == null) throw new ArgumentNullException(nameof(mappedType));

return from p in mappedType.GetRuntimeProperties()
where
((p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic) ||
(p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic))
select p;
}

public IEnumerable<PropertyInfo> GetDecoratedPrivateInstanceProperties(Type mappedType, Type attributeType)
{
return from p in mappedType.GetRuntimeProperties()
where
((p.GetMethod != null && p.GetMethod.IsPublic) || (p.SetMethod != null && p.SetMethod.IsPublic) ||
(p.GetMethod != null && p.GetMethod.IsStatic) || (p.SetMethod != null && p.SetMethod.IsStatic))
select p;
where (
(p.GetMethod != null && p.GetMethod.IsPrivate) || (p.SetMethod != null && p.SetMethod.IsPrivate) ||
p.GetCustomAttribute(attributeType) != null
)
select p;
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
return Expression.Lambda(expr).Compile().DynamicInvoke();
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
if (member is PropertyInfo)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member is FieldInfo)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.DeclaringType);
}
}
}
31 changes: 2 additions & 29 deletions src/SQLite.Net.Platform.XamarinAndroid/ReflectionServiceAndroid.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform.XamarinAndroid
namespace SQLite.Net.Platform.XamarinAndroid
{
public class ReflectionServiceAndroid : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
public class ReflectionServiceAndroid : ReflectionService { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\ReflectionService.cs">
<Link>ReflectionService.cs</Link>
</Compile>
<Compile Include="ReflectionServiceAndroid.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
31 changes: 2 additions & 29 deletions src/SQLite.Net.Platform.XamarinIOS.Unified/ReflectionServiceIOS.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using SQLite.Net.Interop;

namespace SQLite.Net.Platform.XamarinIOS
namespace SQLite.Net.Platform.XamarinIOS
{
public class ReflectionServiceIOS : IReflectionService
{
public IEnumerable<PropertyInfo> GetPublicInstanceProperties(Type mappedType)
{
return mappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
}

public object GetMemberValue(object obj, Expression expr, MemberInfo member)
{
if (member.MemberType == MemberTypes.Property)
{
var m = (PropertyInfo) member;
return m.GetValue(obj, null);
}
if (member.MemberType == MemberTypes.Field)
{
var m = (FieldInfo) member;
return m.GetValue(obj);
}
throw new NotSupportedException("MemberExpr: " + member.MemberType);
}
}
public class ReflectionServiceIOS : ReflectionService { }
}
Loading