Skip to content

Commit

Permalink
renamed the project to just "Dictionary"
Browse files Browse the repository at this point in the history
  • Loading branch information
tallesl committed Feb 18, 2016
1 parent 5456d47 commit 730cf9c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 65 deletions.
File renamed without changes.
50 changes: 50 additions & 0 deletions Library/DictionaryMaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace DictionaryLibrary
{
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;

/// <summary>
/// Makes a dictionary out of a given object.
/// </summary>
public static class DictionaryMaker
{
/// <summary>
/// Makes a dictionary out of the properties of the given input.
/// </summary>
/// <param name="input">Object to make a dictionary out of</param>
/// <returns>
/// A dictionary with the keys being the input object property names and the values their respective values
/// </returns>
public static IDictionary<string, object> Make(object input)
{
if (input == null)
throw new ArgumentNullException("input");

else if (input is ExpandoObject)
return (IDictionary<string, object>)input;

else
{
var properties = input.GetType().GetProperties();
var fields = input.GetType().GetFields();
var members = properties.Cast<MemberInfo>().Concat(fields.Cast<MemberInfo>());
return members.ToDictionary(m => m.Name, m => GetValue(input, m));
}
}

private static object GetValue(object obj, MemberInfo member)
{
if (member is PropertyInfo)
return ((PropertyInfo)member).GetValue(obj, null);

else if (member is FieldInfo)
return ((FieldInfo)member).GetValue(obj);

else
throw new ArgumentException("Passed member is neither a PropertyInfo nor a FieldInfo.");
}
}
}
8 changes: 4 additions & 4 deletions Library/Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{B8B563C0-DC8F-453C-AFDC-FBBBAA4C083B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PropertiesHash</RootNamespace>
<AssemblyName>PropertiesHash</AssemblyName>
<RootNamespace>DictionaryLibrary</RootNamespace>
<AssemblyName>Dictionary</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
Expand All @@ -29,13 +29,13 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\PropertiesHash.xml</DocumentationFile>
<DocumentationFile>bin\Release\Dictionary.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="PropertiesHasher.cs" />
<Compile Include="DictionaryMaker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
2 changes: 1 addition & 1 deletion Library/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PropertiesHash")]
[assembly: AssemblyTitle("Dictionary")]
[assembly: AssemblyDescription("Makes a dictionary out of the given object properties.")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
Expand Down
41 changes: 0 additions & 41 deletions Library/PropertiesHasher.cs

This file was deleted.

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# PropertiesHash
# Dictionary

[![][build-img]][build]
[![][nuget-img]][nuget]

Makes a dictionary out of the given object properties.

[build]: https://ci.appveyor.com/project/TallesL/PropertiesHash
[build-img]: https://ci.appveyor.com/api/projects/status/github/tallesl/PropertiesHash

[nuget]: http://badge.fury.io/nu/PropertiesHash
[nuget-img]: https://badge.fury.io/nu/PropertiesHash.png
[build]: https://ci.appveyor.com/project/TallesL/net-dictionary
[build-img]: https://ci.appveyor.com/api/projects/status/github/tallesl/net-dictionary?svg=true
[nuget]: https://www.nuget.org/packages/Dictionary
[nuget-img]: https://badge.fury.io/nu/Dictionary.svg

## Usage

```cs
using PropertiesHash;
using DictionaryLibrary;

// cmd.Keys are the property names and cmd.Values are their corresponding values
IDictionary<string, object> cmd = PropertiesHasher.Make(someObject);
// dict.Keys are the property names and dict.Values are their corresponding values
IDictionary<string, object> dict = DictionaryMaker.Make(someObject);
```
2 changes: 1 addition & 1 deletion Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PropertiesHash.Tests")]
[assembly: AssemblyTitle("Dictionary.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
Expand Down
2 changes: 1 addition & 1 deletion Tests/SomeData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PropertiesHash.Tests
namespace DictionaryLibrary.Tests
{
using System;

Expand Down
2 changes: 1 addition & 1 deletion Tests/SomeImmutableData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PropertiesHash.Tests
namespace DictionaryLibrary.Tests
{
using System;

Expand Down
10 changes: 5 additions & 5 deletions Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PropertiesHash.Tests
namespace DictionaryLibrary.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
Expand All @@ -22,7 +22,7 @@ public void Typical()
};

// Act
var dict = PropertiesHasher.Make(obj);
var dict = DictionaryMaker.Make(obj);

// Assert
Assert.AreEqual(obj.Id, dict["Id"]);
Expand All @@ -44,7 +44,7 @@ public void Expando()
// Act
// If I use "var dict" here the type of dict is an ExpandoObject...
// What on earth is that?
IDictionary<string, object> dict = PropertiesHasher.Make(obj);
IDictionary<string, object> dict = DictionaryMaker.Make(obj);

// Assert
Assert.AreEqual(obj.Id, dict["Id"]);
Expand All @@ -67,7 +67,7 @@ public void Anonymous()
};

// Act
var dict = PropertiesHasher.Make(obj);
var dict = DictionaryMaker.Make(obj);

// Assert
Assert.AreEqual(obj.Id, dict["Id"]);
Expand All @@ -83,7 +83,7 @@ public void Immutable()
var obj = new SomeImmutableData(new Guid("8d995e6e-af88-4043-8c31-8ba04c6b4298"), 23, "foo bar", null);

// Act
var dict = PropertiesHasher.Make(obj);
var dict = DictionaryMaker.Make(obj);

// Assert
Assert.AreEqual(obj.Id, dict["Id"]);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<ProjectGuid>{FE75CCDE-4CAD-4472-8F83-135EE1501CF8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PropertiesHash.Tests</RootNamespace>
<AssemblyName>PropertiesHash.Tests</AssemblyName>
<RootNamespace>DictionaryLibrary.Tests</RootNamespace>
<AssemblyName>Dictionary.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand Down

0 comments on commit 730cf9c

Please sign in to comment.