-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renamed the project to just "Dictionary"
- Loading branch information
Showing
11 changed files
with
73 additions
and
65 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace PropertiesHash.Tests | ||
namespace DictionaryLibrary.Tests | ||
{ | ||
using System; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace PropertiesHash.Tests | ||
namespace DictionaryLibrary.Tests | ||
{ | ||
using System; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters