Skip to content

Commit d315cc3

Browse files
authored
Copy reflection code to json lib (V3) (#553)
* Copy reflection code into JSON lib Remove dependency on internal code in UnitsNet, which broke in v4 branch trying to strong name sign the UnitsNet lib. Can also not remove InternalsVisibleTo for backwards compatibility reasons. * Remove commented code in ReflectionBridgeExtensions * JsonNet: 1.3.1
1 parent e7921a1 commit d315cc3

File tree

4 files changed

+47
-179
lines changed

4 files changed

+47
-179
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
5+
namespace UnitsNet.Serialization.JsonNet.Internal
6+
{
7+
/// <summary>
8+
/// Helper for dealing with reflection, abstracting API differences between old and new .NET framework.
9+
/// </summary>
10+
internal static class ReflectionHelper
11+
{
12+
internal static PropertyInfo GetProperty(this Type type, string name)
13+
{
14+
#if (NET40 || NET35 || NET20 || SILVERLIGHT)
15+
return type.GetProperty(name);
16+
17+
#else
18+
return type.GetTypeInfo().GetDeclaredProperty(name);
19+
#endif
20+
}
21+
22+
// Ambiguous method conflict with GetMethods() name WindowsRuntimeComponent, so use GetDeclaredMethods() instead
23+
internal static IEnumerable<MethodInfo> GetDeclaredMethods(this Type someType)
24+
{
25+
Type t = someType;
26+
while (t != null)
27+
{
28+
#if (NET40 || NET35 || NET20 || SILVERLIGHT)
29+
foreach (var m in t.GetMethods())
30+
yield return m;
31+
t = t.BaseType;
32+
#else
33+
TypeInfo ti = t.GetTypeInfo();
34+
foreach (MethodInfo m in ti.DeclaredMethods)
35+
yield return m;
36+
t = ti.BaseType;
37+
#endif
38+
}
39+
}
40+
}
41+
}

UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.Common.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- NuGet properties -->
33
<PropertyGroup>
44
<PackageId>UnitsNet.Serialization.JsonNet</PackageId>
5-
<Version>1.3.0</Version>
5+
<Version>1.3.1</Version>
66
<Authors>Andreas Gullberg Larsen</Authors>
77
<Title>Units.NET Serialization with Json.NET</Title>
88
<Description>A helper library for serializing and deserializing types in Units.NET using Json.NET.</Description>

UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
using JetBrains.Annotations;
2626
using Newtonsoft.Json;
2727
using Newtonsoft.Json.Linq;
28-
using UnitsNet.InternalHelpers;
29-
using UnitsNet.Units;
28+
using UnitsNet.Serialization.JsonNet.Internal;
3029

3130
namespace UnitsNet.Serialization.JsonNet
3231
{
@@ -222,7 +221,7 @@ public override void WriteJson(JsonWriter writer, object obj, JsonSerializer ser
222221
private static string GetUnitFullNameOfQuantity(object obj, Type quantityType)
223222
{
224223
// Get value of Unit property
225-
PropertyInfo unitProperty = quantityType.GetPropety("Unit");
224+
PropertyInfo unitProperty = quantityType.GetProperty("Unit");
226225
Enum quantityUnit = (Enum) unitProperty.GetValue(obj, null); // MassUnit.Kilogram
227226

228227
Type unitType = quantityUnit.GetType(); // MassUnit
@@ -289,7 +288,7 @@ private class ValueUnit
289288
public double Value { get; [UsedImplicitly] set; }
290289
}
291290

292-
#region Can Convert
291+
#region Can Convert
293292

294293
/// <summary>
295294
/// Determines whether this instance can convert the specified object type.
@@ -332,6 +331,7 @@ protected virtual bool CanConvertNullable(Type objectType)
332331
return objectType.FullName != null && objectType.FullName.Contains(nameof(UnitsNet) + ".");
333332
}
334333

335-
#endregion
334+
#endregion
335+
336336
}
337337
}

UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,6 @@ internal static Assembly GetAssembly(this Type type)
4646
#endif
4747
}
4848

49-
// internal static bool IsSealed(this Type type)
50-
// {
51-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
52-
// return type.GetTypeInfo().IsSealed;
53-
//#else
54-
// return type.IsSealed;
55-
//#endif
56-
// }
57-
//
58-
// internal static bool IsAbstract(this Type type)
59-
// {
60-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
61-
// return type.GetTypeInfo().IsAbstract;
62-
//#else
63-
// return type.IsAbstract;
64-
//#endif
65-
// }
66-
6749
internal static bool IsEnum(this Type type)
6850
{
6951
#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
@@ -73,92 +55,6 @@ internal static bool IsEnum(this Type type)
7355
#endif
7456
}
7557

76-
// internal static bool IsClass(this Type type)
77-
// {
78-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
79-
// return type.GetTypeInfo().IsClass;
80-
//#else
81-
// return type.IsClass;
82-
//#endif
83-
// }
84-
//
85-
// internal static bool IsPrimitive(this Type type)
86-
// {
87-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
88-
// return type.GetTypeInfo().IsPrimitive;
89-
//#else
90-
// return type.IsPrimitive;
91-
//#endif
92-
// }
93-
//
94-
// internal static bool IsPublic(this Type type)
95-
// {
96-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
97-
// return type.GetTypeInfo().IsPublic;
98-
//#else
99-
// return type.IsPublic;
100-
//#endif
101-
// }
102-
//
103-
// internal static bool IsNestedPublic(this Type type)
104-
// {
105-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
106-
// return type.GetTypeInfo().IsNestedPublic;
107-
//#else
108-
// return type.IsNestedPublic;
109-
//#endif
110-
// }
111-
//
112-
// internal static bool IsFromLocalAssembly(this Type type)
113-
// {
114-
//#if SILVERLIGHT
115-
// string assemblyName = type.GetAssembly().FullName;
116-
//#else
117-
// string assemblyName = type.GetAssembly().GetName().Name;
118-
//#endif
119-
//
120-
// try
121-
// {
122-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
123-
// Assembly.Load(new AssemblyName {Name = assemblyName});
124-
//#else
125-
// Assembly.Load(assemblyName);
126-
//#endif
127-
// return true;
128-
// }
129-
// catch
130-
// {
131-
// return false;
132-
// }
133-
// }
134-
//
135-
// internal static bool IsGenericType(this Type type)
136-
// {
137-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
138-
// return type.GetTypeInfo().IsGenericType;
139-
//#else
140-
// return type.IsGenericType;
141-
//#endif
142-
// }
143-
//
144-
// internal static bool IsInterface(this Type type)
145-
// {
146-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
147-
// return type.GetTypeInfo().IsInterface;
148-
//#else
149-
// return type.IsInterface;
150-
//#endif
151-
// }
152-
//
153-
// internal static Type BaseType(this Type type)
154-
// {
155-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
156-
// return type.GetTypeInfo().BaseType;
157-
//#else
158-
// return type.BaseType;
159-
//#endif
160-
// }
161-
16258
internal static bool IsValueType(this Type type)
16359
{
16460
#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
@@ -168,49 +64,6 @@ internal static bool IsValueType(this Type type)
16864
#endif
16965
}
17066

171-
// internal static T GetPropertyValue<T>(this Type type, string propertyName, object target)
172-
// {
173-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
174-
// PropertyInfo property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
175-
// return (T) property.GetValue(target);
176-
//#else
177-
// return (T) type.InvokeMember(propertyName, BindingFlags.GetProperty, null, target, null);
178-
//#endif
179-
// }
180-
//
181-
// internal static void SetPropertyValue(this Type type, string propertyName, object target, object value)
182-
// {
183-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
184-
// PropertyInfo property = type.GetTypeInfo().GetDeclaredProperty(propertyName);
185-
// property.SetValue(target, value);
186-
//#else
187-
// type.InvokeMember(propertyName, BindingFlags.SetProperty, null, target, new object[] {value});
188-
//#endif
189-
// }
190-
//
191-
// internal static void SetFieldValue(this Type type, string fieldName, object target, object value)
192-
// {
193-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
194-
// FieldInfo field = type.GetTypeInfo().GetDeclaredField(fieldName);
195-
// if (field != null)
196-
// field.SetValue(target, value);
197-
// else
198-
// type.SetPropertyValue(fieldName, target, value);
199-
//#else
200-
// type.InvokeMember(fieldName, BindingFlags.SetField | BindingFlags.SetProperty, null, target, new object[] {value});
201-
//#endif
202-
// }
203-
//
204-
// internal static void InvokeMethod<T>(this Type type, string methodName, object target, T value)
205-
// {
206-
//#if !(NET40 || NET35 || NET20 || SILVERLIGHT)
207-
// MethodInfo method = type.GetTypeInfo().GetDeclaredMethod(methodName);
208-
// method.Invoke(target, new object[] {value});
209-
//#else
210-
// type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, new object[] {value});
211-
//#endif
212-
// }
213-
21467
internal static PropertyInfo GetPropety(this Type type, string name)
21568
{
21669
#if (NET40 || NET35 || NET20 || SILVERLIGHT)
@@ -234,32 +87,6 @@ internal static IEnumerable<MethodInfo> GetDeclaredMethods(this Type someType)
23487
t = ti.BaseType;
23588
}
23689
}
237-
238-
// internal static Type[] GetGenericArguments(this Type type)
239-
// {
240-
// return type.GetTypeInfo().GenericTypeArguments;
241-
// }
242-
//
243-
// /*
244-
// internal static bool IsAssignableFrom(this Type type, Type otherType)
245-
// {
246-
// return type.GetTypeInfo().IsAssignableFrom(otherType.GetTypeInfo());
247-
// }*/
248-
//
249-
// internal static bool IsSubclassOf(this Type type, Type c)
250-
// {
251-
// return type.GetTypeInfo().IsSubclassOf(c);
252-
// }
253-
//
254-
// internal static Attribute[] GetCustomAttributes(this Type type)
255-
// {
256-
// return type.GetTypeInfo().GetCustomAttributes().ToArray();
257-
// }
258-
//
259-
// internal static Attribute[] GetCustomAttributes(this Type type, Type attributeType, bool inherit)
260-
// {
261-
// return type.GetTypeInfo().GetCustomAttributes(attributeType, inherit).Cast<Attribute>().ToArray();
262-
// }
26390
#else
26491
// Ambiguous method conflict with GetMethods() name WindowsRuntimeComponent, so use GetDeclaredMethods() instead
26592
internal static IEnumerable<MethodInfo> GetDeclaredMethods(this Type someType)

0 commit comments

Comments
 (0)