Skip to content

Commit be0b6c9

Browse files
committed
Fix for UAP (remove dependency on System.Reflection.Emit)
Linked to #33
1 parent 229ffce commit be0b6c9

18 files changed

+362
-74
lines changed

README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ The following frameworks are supported:
1515
- net40
1616
- net45x
1717
- net46x
18-
- dnx451
1918
- netstandard1.3
2019
- netcore45
2120
- netcore451
21+
- netcore50
2222
- uap10.0
2323
- SilverLight 5.0
2424

@@ -29,10 +29,6 @@ This fork takes the basic library to a new level. Contains XML Documentation and
2929
Some background:
3030
I forked from https://github.com/NArnott/System.Linq.Dynamic and added some more functionality there.<br>My fork is still visible on github [https://github.com/StefH/System.Linq.Dynamic], however I decided to start a new project + nuget to avoid confusion and create the project according to the new VS2015 + dotnet Core standards.
3131

32-
33-
34-
35-
3632
However, currently there are multiple nuget packages and project available:
3733

3834
| Project | NuGet | Author | Comment |

src/System.Linq.Dynamic.Core/AssemblyBuilderFactory.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
#if !UAP10_0
2+
using System.Reflection;
23
using System.Reflection.Emit;
34

45
namespace System.Linq.Dynamic.Core
@@ -20,4 +21,5 @@ public static AssemblyBuilder DefineDynamicAssembly(AssemblyName name, AssemblyB
2021
#endif
2122
}
2223
}
23-
}
24+
}
25+
#endif

src/System.Linq.Dynamic.Core/Compatibility/CustomTypeBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace System.Reflection
55
{
66
internal static class CustomTypeBuilderExtensions
77
{
8-
#if !(NET35 || NET40 || SILVERLIGHT || WPSL)
8+
#if !(NET35 || NET40 || SILVERLIGHT || WPSL || UAP10_0)
99
public static Type CreateType(this TypeBuilder tb)
1010
{
1111
return tb.CreateTypeInfo().AsType();

src/System.Linq.Dynamic.Core/DynamicClass.cs

+196-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,199 @@
1-
#if WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD
1+
#if UAP10_0
2+
using System.Collections.Generic;
3+
using System.Dynamic;
4+
5+
namespace System.Linq.Dynamic.Core
6+
{
7+
/// <summary>
8+
/// Provides a base class for dynamic objects for UAP10_0.
9+
/// </summary>
10+
public class DynamicClass : DynamicObject
11+
{
12+
readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
13+
14+
public DynamicClass(
15+
KeyValuePair<string, object> _1
16+
)
17+
{
18+
_properties.Add(_1.Key, _1.Value);
19+
}
20+
21+
public DynamicClass(
22+
KeyValuePair<string, object> _1,
23+
KeyValuePair<string, object> _2
24+
)
25+
{
26+
_properties.Add(_1.Key, _1.Value);
27+
_properties.Add(_2.Key, _2.Value);
28+
}
29+
30+
public DynamicClass(
31+
KeyValuePair<string, object> _1,
32+
KeyValuePair<string, object> _2,
33+
KeyValuePair<string, object> _3
34+
)
35+
{
36+
_properties.Add(_1.Key, _1.Value);
37+
_properties.Add(_2.Key, _2.Value);
38+
_properties.Add(_3.Key, _3.Value);
39+
}
40+
41+
public DynamicClass(
42+
KeyValuePair<string, object> _1,
43+
KeyValuePair<string, object> _2,
44+
KeyValuePair<string, object> _3,
45+
KeyValuePair<string, object> _4
46+
)
47+
{
48+
_properties.Add(_1.Key, _1.Value);
49+
_properties.Add(_2.Key, _2.Value);
50+
_properties.Add(_3.Key, _3.Value);
51+
_properties.Add(_4.Key, _4.Value);
52+
}
53+
54+
public DynamicClass(
55+
KeyValuePair<string, object> _1,
56+
KeyValuePair<string, object> _2,
57+
KeyValuePair<string, object> _3,
58+
KeyValuePair<string, object> _4,
59+
KeyValuePair<string, object> _5
60+
)
61+
{
62+
_properties.Add(_1.Key, _1.Value);
63+
_properties.Add(_2.Key, _2.Value);
64+
_properties.Add(_3.Key, _3.Value);
65+
_properties.Add(_4.Key, _4.Value);
66+
_properties.Add(_5.Key, _5.Value);
67+
}
68+
69+
public DynamicClass(
70+
KeyValuePair<string, object> _1,
71+
KeyValuePair<string, object> _2,
72+
KeyValuePair<string, object> _3,
73+
KeyValuePair<string, object> _4,
74+
KeyValuePair<string, object> _5,
75+
KeyValuePair<string, object> _6
76+
)
77+
{
78+
_properties.Add(_1.Key, _1.Value);
79+
_properties.Add(_2.Key, _2.Value);
80+
_properties.Add(_3.Key, _3.Value);
81+
_properties.Add(_4.Key, _4.Value);
82+
_properties.Add(_5.Key, _5.Value);
83+
_properties.Add(_6.Key, _6.Value);
84+
}
85+
86+
public DynamicClass(
87+
KeyValuePair<string, object> _1,
88+
KeyValuePair<string, object> _2,
89+
KeyValuePair<string, object> _3,
90+
KeyValuePair<string, object> _4,
91+
KeyValuePair<string, object> _5,
92+
KeyValuePair<string, object> _6,
93+
KeyValuePair<string, object> _7
94+
)
95+
{
96+
_properties.Add(_1.Key, _1.Value);
97+
_properties.Add(_2.Key, _2.Value);
98+
_properties.Add(_3.Key, _3.Value);
99+
_properties.Add(_4.Key, _4.Value);
100+
_properties.Add(_5.Key, _5.Value);
101+
_properties.Add(_6.Key, _6.Value);
102+
_properties.Add(_7.Key, _7.Value);
103+
}
104+
105+
public DynamicClass(
106+
KeyValuePair<string, object> _1,
107+
KeyValuePair<string, object> _2,
108+
KeyValuePair<string, object> _3,
109+
KeyValuePair<string, object> _4,
110+
KeyValuePair<string, object> _5,
111+
KeyValuePair<string, object> _6,
112+
KeyValuePair<string, object> _7,
113+
KeyValuePair<string, object> _8
114+
)
115+
{
116+
_properties.Add(_1.Key, _1.Value);
117+
_properties.Add(_2.Key, _2.Value);
118+
_properties.Add(_3.Key, _3.Value);
119+
_properties.Add(_4.Key, _4.Value);
120+
_properties.Add(_5.Key, _5.Value);
121+
_properties.Add(_6.Key, _6.Value);
122+
_properties.Add(_7.Key, _7.Value);
123+
_properties.Add(_8.Key, _8.Value);
124+
}
125+
126+
public DynamicClass(
127+
KeyValuePair<string, object> _1,
128+
KeyValuePair<string, object> _2,
129+
KeyValuePair<string, object> _3,
130+
KeyValuePair<string, object> _4,
131+
KeyValuePair<string, object> _5,
132+
KeyValuePair<string, object> _6,
133+
KeyValuePair<string, object> _7,
134+
KeyValuePair<string, object> _8,
135+
KeyValuePair<string, object> _9
136+
)
137+
{
138+
_properties.Add(_1.Key, _1.Value);
139+
_properties.Add(_2.Key, _2.Value);
140+
_properties.Add(_3.Key, _3.Value);
141+
_properties.Add(_4.Key, _4.Value);
142+
_properties.Add(_5.Key, _5.Value);
143+
_properties.Add(_6.Key, _6.Value);
144+
_properties.Add(_7.Key, _7.Value);
145+
_properties.Add(_8.Key, _8.Value);
146+
_properties.Add(_9.Key, _9.Value);
147+
}
148+
149+
public object this[string name]
150+
{
151+
get
152+
{
153+
object result;
154+
if (_properties.TryGetValue(name, out result))
155+
return result;
156+
157+
return null;
158+
}
159+
set
160+
{
161+
if (_properties.ContainsKey(name))
162+
_properties[name] = value;
163+
else
164+
_properties.Add(name, value);
165+
}
166+
}
167+
168+
public override IEnumerable<string> GetDynamicMemberNames()
169+
{
170+
return _properties.Keys;
171+
}
172+
173+
public override bool TryGetMember(GetMemberBinder binder, out object result)
174+
{
175+
var name = binder.Name;
176+
_properties.TryGetValue(name, out result);
177+
178+
return true;
179+
}
180+
181+
public override bool TrySetMember(SetMemberBinder binder, object value)
182+
{
183+
var name = binder.Name;
184+
if (_properties.ContainsKey(name))
185+
_properties[name] = value;
186+
else
187+
_properties.Add(name, value);
188+
189+
return true;
190+
}
191+
}
192+
}
193+
#else
194+
#if WINDOWS_APP || DOTNET5_1 || NETSTANDARD
2195
using System.Reflection;
3196
#endif
4-
5197
namespace System.Linq.Dynamic.Core
6198
{
7199
/// <summary>
@@ -68,4 +260,5 @@ public void SetDynamicProperty(string propertyName, object value)
68260
propInfo.SetValue(this, value, null);
69261
}
70262
}
71-
}
263+
}
264+
#endif

src/System.Linq.Dynamic.Core/DynamicClassFactory.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Concurrent;
1+
#if !(UAP10_0)
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq.Dynamic.Core.Validation;
@@ -370,4 +371,5 @@ private static string Escape(string str)
370371
return str;
371372
}
372373
}
373-
}
374+
}
375+
#endif

src/System.Linq.Dynamic.Core/ExpressionParser.cs

+27-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
using System.Globalization;
77
using System.Linq.Dynamic.Core.Exceptions;
88
using System.Linq.Dynamic.Core.Tokenizer;
9+
910
namespace System.Linq.Dynamic.Core
1011
{
1112
internal class ExpressionParser
1213
{
13-
1414
interface ILogicalSignatures
1515
{
1616
void F(bool x, bool y);
@@ -225,7 +225,7 @@ interface IEnumerableSignatures
225225
ParameterExpression _parent;
226226
ParameterExpression _root;
227227

228-
228+
229229

230230
static ExpressionParser()
231231
{
@@ -1133,7 +1133,27 @@ Expression ParseNew()
11331133
_textParser.ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected);
11341134
_textParser.NextToken();
11351135

1136-
// http://solutionizing.net/category/linq/
1136+
return CreateNewExpression(properties, expressions);
1137+
}
1138+
1139+
private Expression CreateNewExpression(List<DynamicProperty> properties, List<Expression> expressions)
1140+
{
1141+
#if UAP10_0
1142+
// http://solutionizing.net/category/linq/
1143+
Type type = _resultType ?? typeof(DynamicClass);
1144+
var parameters = new List<Expression>();
1145+
var constructorKeyValuePair = typeof(KeyValuePair<string, object>).GetTypeInfo().DeclaredConstructors.First();
1146+
for (int i = 0; i < properties.Count; i++)
1147+
{
1148+
// Just convert the expression always to an object expression.
1149+
var boxingExpression = Expression.Convert(expressions[i], typeof(object));
1150+
var parameter = Expression.New(constructorKeyValuePair, new[] { (Expression)Expression.Constant(properties[i].Name), boxingExpression });
1151+
parameters.Add(parameter);
1152+
}
1153+
var constructor = type.GetTypeInfo().DeclaredConstructors.First(x => x.GetParameters().Count() == properties.Count());
1154+
return Expression.New(constructor, parameters);
1155+
#else
1156+
// http://solutionizing.net/category/linq/
11371157
Type type = _resultType ?? DynamicClassFactory.CreateType(properties, _createParameterCtor);
11381158

11391159
var propertyTypes = type.GetProperties().Select(p => p.PropertyType).ToArray();
@@ -1145,6 +1165,7 @@ Expression ParseNew()
11451165
for (int i = 0; i < bindings.Length; i++)
11461166
bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);
11471167
return Expression.MemberInit(Expression.New(type), bindings);
1168+
#endif
11481169
}
11491170

11501171
Expression ParseLambdaInvocation(LambdaExpression lambda)
@@ -2332,9 +2353,9 @@ static Expression OptimizeStringForEqualityIfPossible(string text, Type type)
23322353
return null;
23332354
}
23342355

2335-
23362356

2337-
2357+
2358+
23382359

23392360
bool TokenIdentifierIs(string id)
23402361
{
@@ -2349,10 +2370,6 @@ string GetIdentifier()
23492370
return id;
23502371
}
23512372

2352-
2353-
2354-
2355-
23562373
Exception ParseError(string format, params object[] args)
23572374
{
23582375
return ParseError(_textParser.CurrentToken.Pos, format, args);
@@ -2407,10 +2424,9 @@ static Dictionary<string, object> CreateKeywords()
24072424
return d;
24082425
}
24092426

2410-
24112427
internal static void ResetDynamicLinqTypes()
24122428
{
24132429
_keywords = null;
24142430
}
24152431
}
2416-
}
2432+
}

0 commit comments

Comments
 (0)