Skip to content

Commit b905a8f

Browse files
committed
Added dynamic property binding for VObject
- Borrowed a few MIT licensed utils from Json.Net.
1 parent 1fd4d4f commit b905a8f

File tree

8 files changed

+725
-3
lines changed

8 files changed

+725
-3
lines changed

Gameloop.Vdf/Gameloop.Vdf.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
</ItemGroup>
4141
<ItemGroup>
4242
<Compile Include="Properties\AssemblyInfo.cs" />
43+
<Compile Include="Utilities\CollectionUtils.cs" />
44+
<Compile Include="Utilities\DynamicProxy.cs" />
45+
<Compile Include="Utilities\DynamicProxyMetaObject.cs" />
46+
<Compile Include="Utilities\ReflectionUtils.cs" />
47+
<Compile Include="Utilities\TypeExtensions.cs" />
4348
<Compile Include="VContainer.cs" />
4449
<Compile Include="VdfConvert.cs" />
4550
<Compile Include="VdfReader.cs" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
using System;
27+
using System.Collections.Generic;
28+
using System.Diagnostics;
29+
using System.Linq;
30+
31+
namespace Gameloop.Vdf.Utilities
32+
{
33+
internal static class CollectionUtils
34+
{
35+
/// <summary>
36+
/// Adds the elements of the specified collection to the specified generic <see cref="IList{T}"/>.
37+
/// </summary>
38+
/// <param name="initial">The list to add to.</param>
39+
/// <param name="collection">The collection of elements to add.</param>
40+
public static void AddRange<T>(this IList<T> initial, IEnumerable<T> collection)
41+
{
42+
if (initial == null)
43+
{
44+
throw new ArgumentNullException(nameof(initial));
45+
}
46+
47+
if (collection == null)
48+
{
49+
return;
50+
}
51+
52+
foreach (T value in collection)
53+
{
54+
initial.Add(value);
55+
}
56+
}
57+
58+
public static T[] ArrayEmpty<T>()
59+
{
60+
T[] array = Enumerable.Empty<T>() as T[];
61+
Debug.Assert(array != null);
62+
// Defensively guard against a version of Linq where Enumerable.Empty<T> doesn't
63+
// return an array, but throw in debug versions so a better strategy can be
64+
// used if that ever happens.
65+
return array ?? new T[0];
66+
}
67+
}
68+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
using System.Collections.Generic;
27+
using System.Dynamic;
28+
29+
namespace Gameloop.Vdf.Utilities
30+
{
31+
internal class DynamicProxy<T>
32+
{
33+
public virtual IEnumerable<string> GetDynamicMemberNames(T instance)
34+
{
35+
return CollectionUtils.ArrayEmpty<string>();
36+
}
37+
38+
public virtual bool TryBinaryOperation(T instance, BinaryOperationBinder binder, object arg, out object result)
39+
{
40+
result = null;
41+
return false;
42+
}
43+
44+
public virtual bool TryConvert(T instance, ConvertBinder binder, out object result)
45+
{
46+
result = null;
47+
return false;
48+
}
49+
50+
public virtual bool TryCreateInstance(T instance, CreateInstanceBinder binder, object[] args, out object result)
51+
{
52+
result = null;
53+
return false;
54+
}
55+
56+
public virtual bool TryDeleteIndex(T instance, DeleteIndexBinder binder, object[] indexes)
57+
{
58+
return false;
59+
}
60+
61+
public virtual bool TryDeleteMember(T instance, DeleteMemberBinder binder)
62+
{
63+
return false;
64+
}
65+
66+
public virtual bool TryGetIndex(T instance, GetIndexBinder binder, object[] indexes, out object result)
67+
{
68+
result = null;
69+
return false;
70+
}
71+
72+
public virtual bool TryGetMember(T instance, GetMemberBinder binder, out object result)
73+
{
74+
result = null;
75+
return false;
76+
}
77+
78+
public virtual bool TryInvoke(T instance, InvokeBinder binder, object[] args, out object result)
79+
{
80+
result = null;
81+
return false;
82+
}
83+
84+
public virtual bool TryInvokeMember(T instance, InvokeMemberBinder binder, object[] args, out object result)
85+
{
86+
result = null;
87+
return false;
88+
}
89+
90+
public virtual bool TrySetIndex(T instance, SetIndexBinder binder, object[] indexes, object value)
91+
{
92+
return false;
93+
}
94+
95+
public virtual bool TrySetMember(T instance, SetMemberBinder binder, object value)
96+
{
97+
return false;
98+
}
99+
100+
public virtual bool TryUnaryOperation(T instance, UnaryOperationBinder binder, out object result)
101+
{
102+
result = null;
103+
return false;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)