Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dburriss/philosophicalmonkey
A C# Reflection Helper Library
https://github.com/dburriss/philosophicalmonkey
Last synced: 21 days ago
JSON representation
A C# Reflection Helper Library
- Host: GitHub
- URL: https://github.com/dburriss/philosophicalmonkey
- Owner: dburriss
- License: mit
- Created: 2015-09-29T05:08:38.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-23T17:25:19.000Z (about 7 years ago)
- Last Synced: 2024-11-01T17:12:07.436Z (about 2 months ago)
- Language: C#
- Size: 72.3 KB
- Stars: 14
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Philosophical Monkey
| DEV |MASTER|BLEEDING|NUGET|
|-----|------|--------|-----|
|[![Build status](https://ci.appveyor.com/api/projects/status/05drr0dq7omoru07?svg=true)](https://ci.appveyor.com/project/dburriss/philosophicalmonkey)|[![Master Build status](https://ci.appveyor.com/api/projects/status/pmgou6qm452s50d0/branch/master?svg=true)](https://ci.appveyor.com/project/dburriss/philosophicalmonkey/branch/master)|[![MyGet CI](https://img.shields.io/myget/dburriss-ci/vpre/PhilosophicalMonkey.svg)](http://myget.org/gallery/dburriss-ci)|[![NuGet CI](https://img.shields.io/nuget/v/PhilosophicalMonkey.svg)](https://www.nuget.org/packages/PhilosophicalMonkey/)|
I am a reflection helper that helps you reflect on the code around you.
> An important feature of this library is it abstracts the differences between the full .NET Framework and the new .NET Core reflection API.
## Install from nuget
> `Install-Package PhilosophicalMonkey`
## Basic Usage
The Philosophical Monkey has dozens of nugets of wisdom but here are a few examples of how he can help.
## Getting all types from a namespace
```csharp
var assembly = typeof(TestModel).GetTypeInfo().Assembly;
var types = Reflect.OnTypes.GetTypesFromNamespace(assembly, "TestModels");Assert.Contains(types, t => t == typeof(TestModel));
```## Get a null safe property value
```csharp
var obj = new TestModel
{
Id = 1,
MyString = null
};
var result = Reflect.OnProperties.NullSafeGetValue(obj, x => x.MyString, "Default");Assert.Equal("Default", result);
```### Mapping a dynamic to a dictionary
```csharp
dynamic d = new { Nr = 1, Name = "Devon" };
var dictionary = Reflect.OnMappings.TurnObjectIntoDictionary(d);Assert.Equal(2, dictionary.Keys.Count);
```Or fill object properties:
```csharp
dynamic d = new { StreetNr = 1, Street = "Main Rd" };
var dictionary = new Dictionary() { { "StreetNr", 1 }, { "Street", "Main Rd" } };
var instance = new Address();
Reflect.OnMappings.Map(dictionary, instance);
Assert.Equal(instance.StreetNr, 1);
Assert.Equal(instance.Street, "Main Rd");
```## Overview
The Philosphical Monkey has a couple topics he likes to reflect on. These include:
* Types
* Properties
* Attributes
* Mappings### OnTypes
* `IEnumerable GetTypesFromNamespace(Assembly assembly, params string[] @namespaces)`
* `Type[] GetAllExportedTypes(IEnumerable assemblies)`
* `Type[] GetAllTypes(IEnumerable assemblies)`
* `IEnumerable GetInterfaces(Type type)`
* `bool IsAbstract(Type type)`
* `bool ISClass(Type type)`
* `bool IsInterface(Type type)`
* `bool IsGenericType(Type type)`
* `bool IsPrimitive(Type type)`
* `bool IsSimple(Type type)`
* `IEnumerable GetAssemblies(IEnumerable types)`
* `Assembly GetAssembly(Type type)`
* `bool bool IsAssignable(Type concretion, Type abstraction)`
* `IEnumerable GetGenericArguments(Type type)`### OnProperties
* `string GetPropertyName(Expression> expression)`
* `string TypeSafeGetPropertyName(Expression> expression)`
* `GetPropertyType(Expression> expression)`
* `MemberInfo GetMemberInformation(Expression> propertyExpression)`
* `MemberInfo GetMemberInformation(Expression propertyExpression)`
* `MemberInfo GetMemberInformation(Type type, string propertyName)`
* `PropertyInfo GetPropertyInformation(Type type, string propertyName)`
* `PropertyInfo GetPropertyInfoFromPath(string path)`
* `PropertyInfo GetPropertyInfoFromPath(Type type, string path)`
* `TResult NullSafeGetValue(TSource source, Expression> expression, TResult defaultValue)`
* `TCastResultType NullSafeGetValue(TSource source, Expression> expression, TCastResultType defaultValue, Func convertToResultToAction)`
* `string GetFullPropertyPathName(Expression> expression)`
* `object GetValue(TSource source, Expression> expression)`
* `object GetNestedPropertyValue(string name, object obj)`
* `void SetProperty(TModel instance, Expression> exp, object value)`
* `void TypeSafeSetProperty(TModel instance, Expression> exp, TPropertyType value)`### OnAttributes
* `T GetAttribute(MemberInfo member, bool isRequired = false) where T : Attribute`
* `IEnumerable GetCustomAttributesData(MemberInfo memberInfo)`
* `ConstructorInfo ConstructorInfo(CustomAttributeData attributeData)`### OnMappings
* `TTo Map(IDictionary dictionary)`
* `void Map(TFrom from, TTo to)`
* `void Map(IDictionary dictionary, TTo instance)`
* `object TurnDictionaryIntoObject(IDictionary dictionary, Type type)`
* `TTo TurnDictionaryIntoObject(IDictionary dictionary)`
* `TTo TurnDictionaryIntoObject(IDictionary dictionary)`