Permalink
Browse files

Initial "make it compile on DNXCORE50" changes

  • Loading branch information...
1 parent c1c9dd5 commit 31e96d9dd794de23d1ae28c4bfc64c1e09f52db0 @mgravell committed Nov 24, 2015
Showing with 130 additions and 45 deletions.
  1. +8 −7 FastMember/MemberSet.cs
  2. +4 −2 FastMember/ObjectReader.cs
  3. +44 −36 FastMember/TypeAccessor.cs
  4. +60 −0 FastMember/TypeHelpers.cs
  5. +14 −0 FastMember/project.json
@@ -13,7 +13,7 @@ public sealed class MemberSet : IEnumerable<Member>, IList<Member>
Member[] members;
internal MemberSet(Type type)
{
- members = type.GetProperties().Cast<MemberInfo>().Concat(type.GetFields().Cast<MemberInfo>()).OrderBy(x => x.Name, StringComparer.InvariantCulture)
+ members = type.GetProperties().Cast<MemberInfo>().Concat(type.GetFields().Cast<MemberInfo>()).OrderBy(x => x.Name, StringComparer.Ordinal)
.Select(member => new Member(member)).ToArray();
}
/// <summary>
@@ -74,12 +74,9 @@ public Type Type
{
get
{
- switch (member.MemberType)
- {
- case MemberTypes.Field: return ((FieldInfo)member).FieldType;
- case MemberTypes.Property: return ((PropertyInfo)member).PropertyType;
- default: throw new NotSupportedException(member.MemberType.ToString());
- }
+ if(member is FieldInfo) return ((FieldInfo)member).FieldType;
+ if (member is PropertyInfo) return ((PropertyInfo)member).PropertyType;
+ throw new NotSupportedException(member.GetType().Name);
}
}
@@ -89,7 +86,11 @@ public Type Type
public bool IsDefined(Type attributeType)
{
if (attributeType == null) throw new ArgumentNullException("attributeType");
+#if DNXCORE50
+ return member.IsDefined(attributeType);
+#else
return Attribute.IsDefined(member, attributeType);
+#endif
}
@@ -1,4 +1,5 @@
-using System;
+#if !DNXCORE50
+using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
@@ -71,7 +72,7 @@ public ObjectReader(Type type, IEnumerable source, params string[] members)
var tmp = member.Type;
memberType = Nullable.GetUnderlyingType(tmp) ?? tmp;
- allowNull = !(memberType.IsValueType && memberType == tmp);
+ allowNull = !(memberType._IsValueType() && memberType == tmp);
// but keep checking, in case of duplicates
}
@@ -320,3 +321,4 @@ bool IDataRecord.IsDBNull(int i)
}
}
}
+#endif
@@ -127,48 +127,47 @@ private static void WriteMapImpl(ILGenerator il, Type type, List<MemberInfo> mem
il.MarkLabel(labels[i]);
var member = members[i];
bool isFail = true;
- switch (member.MemberType)
+ FieldInfo field;
+ PropertyInfo prop;
+ if((field = member as FieldInfo) != null)
{
- case MemberTypes.Field:
- var field = (FieldInfo)member;
+ il.Emit(obj);
+ Cast(il, type, true);
+ if (isGet)
+ {
+ il.Emit(OpCodes.Ldfld, field);
+ if (field.FieldType._IsValueType()) il.Emit(OpCodes.Box, field.FieldType);
+ }
+ else
+ {
+ il.Emit(value);
+ Cast(il, field.FieldType, false);
+ il.Emit(OpCodes.Stfld, field);
+ }
+ il.Emit(OpCodes.Ret);
+ isFail = false;
+ }
+ else if ((prop = member as PropertyInfo) != null)
+ {
+ MethodInfo accessor;
+ if (prop.CanRead && (accessor = isGet ? prop.GetGetMethod(allowNonPublicAccessors) : prop.GetSetMethod(allowNonPublicAccessors)) != null)
+ {
il.Emit(obj);
Cast(il, type, true);
if (isGet)
{
- il.Emit(OpCodes.Ldfld, field);
- if (field.FieldType.IsValueType) il.Emit(OpCodes.Box, field.FieldType);
+ il.EmitCall(type._IsValueType() ? OpCodes.Call : OpCodes.Callvirt, accessor, null);
+ if (prop.PropertyType._IsValueType()) il.Emit(OpCodes.Box, prop.PropertyType);
}
else
{
il.Emit(value);
- Cast(il, field.FieldType, false);
- il.Emit(OpCodes.Stfld, field);
+ Cast(il, prop.PropertyType, false);
+ il.EmitCall(type._IsValueType() ? OpCodes.Call : OpCodes.Callvirt, accessor, null);
}
il.Emit(OpCodes.Ret);
isFail = false;
- break;
- case MemberTypes.Property:
- var prop = (PropertyInfo)member;
- MethodInfo accessor;
- if (prop.CanRead && (accessor = isGet ? prop.GetGetMethod(allowNonPublicAccessors) : prop.GetSetMethod(allowNonPublicAccessors)) != null)
- {
- il.Emit(obj);
- Cast(il, type, true);
- if (isGet)
- {
- il.EmitCall(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, accessor, null);
- if (prop.PropertyType.IsValueType) il.Emit(OpCodes.Box, prop.PropertyType);
- }
- else
- {
- il.Emit(value);
- Cast(il, prop.PropertyType, false);
- il.EmitCall(type.IsValueType ? OpCodes.Call : OpCodes.Callvirt, accessor, null);
- }
- il.Emit(OpCodes.Ret);
- isFail = false;
- }
- break;
+ }
}
if (isFail) il.Emit(OpCodes.Br, fail);
}
@@ -241,8 +240,8 @@ public override object CreateNew()
}
private static bool IsFullyPublic(Type type, PropertyInfo[] props, bool allowNonPublicAccessors)
{
- while (type.IsNestedPublic) type = type.DeclaringType;
- if (!type.IsPublic) return false;
+ while (type._IsNestedPublic()) type = type.DeclaringType;
+ if (!type._IsPublic()) return false;
if (allowNonPublicAccessors)
{
@@ -280,7 +279,7 @@ static TypeAccessor CreateNew(Type type, bool allowNonPublicAccessors)
foreach (var field in fields) if (!map.ContainsKey(field.Name)) { map.Add(field.Name, i++); members.Add(field); }
ConstructorInfo ctor = null;
- if (type.IsClass && !type.IsAbstract)
+ if (type._IsClass() && !type._IsAbstract())
{
ctor = type.GetConstructor(Type.EmptyTypes);
}
@@ -310,11 +309,20 @@ static TypeAccessor CreateNew(Type type, bool allowNonPublicAccessors)
if (assembly == null)
{
AssemblyName name = new AssemblyName("FastMember_dynamic");
+#if DNXCORE50
+ assembly = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
+#else
assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
+#endif
module = assembly.DefineDynamicModule(name.Name);
}
+#if DNXCORE50
+ TypeAttributes attribs = typeof(TypeAccessor).GetTypeInfo().Attributes;
+#else
+ TypeAttributes attribs = typeof(TypeAccessor).Attributes;
+#endif
TypeBuilder tb = module.DefineType("FastMember_dynamic." + type.Name + "_" + Interlocked.Increment(ref counter),
- (typeof(TypeAccessor).Attributes | TypeAttributes.Sealed | TypeAttributes.Public) & ~(TypeAttributes.Abstract | TypeAttributes.NotPublic), typeof(RuntimeTypeAccessor));
+ (attribs | TypeAttributes.Sealed | TypeAttributes.Public) & ~(TypeAttributes.Abstract | TypeAttributes.NotPublic), typeof(RuntimeTypeAccessor));
il = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] {
typeof(Dictionary<string,int>)
@@ -364,14 +372,14 @@ static TypeAccessor CreateNew(Type type, bool allowNonPublicAccessors)
il.Emit(OpCodes.Ret);
tb.DefineMethodOverride(body, baseMethod);
- var accessor = (TypeAccessor)Activator.CreateInstance(tb.CreateType(), map);
+ var accessor = (TypeAccessor)Activator.CreateInstance(tb._CreateType(), map);
return accessor;
}
private static void Cast(ILGenerator il, Type type, bool valueAsPointer)
{
if (type == typeof(object)) { }
- else if (type.IsValueType)
+ else if (type._IsValueType())
{
if (valueAsPointer)
{
@@ -0,0 +1,60 @@
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+
+namespace FastMember
+{
+ internal static class TypeHelpers
+ {
+ public static bool _IsValueType(this Type type)
+ {
+#if DNXCORE50
+ return type.GetTypeInfo().IsValueType;
+#else
+ return type.IsValueType;
+#endif
+ }
+ public static bool _IsPublic(this Type type)
+ {
+#if DNXCORE50
+ return type.GetTypeInfo().IsPublic;
+#else
+ return type.IsPublic;
+#endif
+ }
+
+ public static bool _IsNestedPublic(this Type type)
+ {
+#if DNXCORE50
+ return type.GetTypeInfo().IsNestedPublic;
+#else
+ return type.IsNestedPublic;
+#endif
+ }
+ public static bool _IsClass(this Type type)
+ {
+#if DNXCORE50
+ return type.GetTypeInfo().IsClass;
+#else
+ return type.IsClass;
+#endif
+ }
+
+ public static bool _IsAbstract(this Type type)
+ {
+#if DNXCORE50
+ return type.GetTypeInfo().IsAbstract;
+#else
+ return type.IsAbstract;
+#endif
+ }
+ public static Type _CreateType(this TypeBuilder type)
+ {
+#if DNXCORE50
+ return type.CreateTypeInfo().AsType();
+#else
+ return type.CreateType();
+#endif
+ }
+ }
+}
@@ -12,6 +12,20 @@
"frameworkAssemblies": {
"System.Data": "4.0.0.0"
}
+ },
+ "dnxcore50": {
+ "dependencies": {
+ "Microsoft.CSharp": "4.0.1-*",
+ "System.Collections.NonGeneric": "4.0.1-*",
+ "System.Data.Common": "4.0.1-*",
+ "System.Linq": "4.0.1-*",
+ "System.Reflection": "4.1.0-*",
+ "System.Reflection.Emit": "4.0.1-*",
+ "System.Reflection.Emit.ILGeneration": "4.0.1-*",
+ "System.Reflection.Emit.Lightweight": "4.0.1-*",
+ "System.Reflection.TypeExtensions":"4.1.0-*",
+ "System.Runtime": "4.0.21-*"
+ }
}
}
}

0 comments on commit 31e96d9

Please sign in to comment.