Skip to content

Commit

Permalink
Added dynamic property binding for VObject
Browse files Browse the repository at this point in the history
- Borrowed a few MIT licensed utils from Json.Net.
  • Loading branch information
shravan2x committed Mar 26, 2017
1 parent 1fd4d4f commit b905a8f
Show file tree
Hide file tree
Showing 8 changed files with 725 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Gameloop.Vdf/Gameloop.Vdf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\CollectionUtils.cs" />
<Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Utilities\DynamicProxyMetaObject.cs" />
<Compile Include="Utilities\ReflectionUtils.cs" />
<Compile Include="Utilities\TypeExtensions.cs" />
<Compile Include="VContainer.cs" />
<Compile Include="VdfConvert.cs" />
<Compile Include="VdfReader.cs" />
Expand Down
68 changes: 68 additions & 0 deletions Gameloop.Vdf/Utilities/CollectionUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Gameloop.Vdf.Utilities
{
internal static class CollectionUtils
{
/// <summary>
/// Adds the elements of the specified collection to the specified generic <see cref="IList{T}"/>.
/// </summary>
/// <param name="initial">The list to add to.</param>
/// <param name="collection">The collection of elements to add.</param>
public static void AddRange<T>(this IList<T> initial, IEnumerable<T> collection)
{
if (initial == null)
{
throw new ArgumentNullException(nameof(initial));
}

if (collection == null)
{
return;
}

foreach (T value in collection)
{
initial.Add(value);
}
}

public static T[] ArrayEmpty<T>()
{
T[] array = Enumerable.Empty<T>() as T[];
Debug.Assert(array != null);
// Defensively guard against a version of Linq where Enumerable.Empty<T> doesn't
// return an array, but throw in debug versions so a better strategy can be
// used if that ever happens.
return array ?? new T[0];
}
}
}
106 changes: 106 additions & 0 deletions Gameloop.Vdf/Utilities/DynamicProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Collections.Generic;
using System.Dynamic;

namespace Gameloop.Vdf.Utilities
{
internal class DynamicProxy<T>
{
public virtual IEnumerable<string> GetDynamicMemberNames(T instance)
{
return CollectionUtils.ArrayEmpty<string>();
}

public virtual bool TryBinaryOperation(T instance, BinaryOperationBinder binder, object arg, out object result)
{
result = null;
return false;
}

public virtual bool TryConvert(T instance, ConvertBinder binder, out object result)
{
result = null;
return false;
}

public virtual bool TryCreateInstance(T instance, CreateInstanceBinder binder, object[] args, out object result)
{
result = null;
return false;
}

public virtual bool TryDeleteIndex(T instance, DeleteIndexBinder binder, object[] indexes)
{
return false;
}

public virtual bool TryDeleteMember(T instance, DeleteMemberBinder binder)
{
return false;
}

public virtual bool TryGetIndex(T instance, GetIndexBinder binder, object[] indexes, out object result)
{
result = null;
return false;
}

public virtual bool TryGetMember(T instance, GetMemberBinder binder, out object result)
{
result = null;
return false;
}

public virtual bool TryInvoke(T instance, InvokeBinder binder, object[] args, out object result)
{
result = null;
return false;
}

public virtual bool TryInvokeMember(T instance, InvokeMemberBinder binder, object[] args, out object result)
{
result = null;
return false;
}

public virtual bool TrySetIndex(T instance, SetIndexBinder binder, object[] indexes, object value)
{
return false;
}

public virtual bool TrySetMember(T instance, SetMemberBinder binder, object value)
{
return false;
}

public virtual bool TryUnaryOperation(T instance, UnaryOperationBinder binder, out object result)
{
result = null;
return false;
}
}
}
Loading

0 comments on commit b905a8f

Please sign in to comment.