Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)`