GithubHelp home page GithubHelp logo

bubdm / typesupport Goto Github PK

View Code? Open in Web Editor NEW

This project forked from replaysmike/typesupport

0.0 0.0 0.0 186 KB

A CSharp library that makes it easier to work with Types dynamically.

License: GNU General Public License v3.0

C# 99.12% PowerShell 0.88%

typesupport's Introduction

TypeSupport

nuget nuget Build status Codacy Badge Codacy Badge

A CSharp library that makes it easier to work with Types dynamically. TypeSupport includes a flexible Object factory for creating and initializing all kinds of types.

Description

The best way to understand what TypeSupport can do is to see it in action! It is used as the foundation for many other packages.

Installation

Install TypeSupport from the Package Manager Console:

PM> Install-Package TypeSupport

Usage

Type Support

Getting started - create a TypeSupport from a type

using TypeSupport;

var type = typeof(MyObject);
var typeSupport = new ExtendedType(type);

or do it using the extensions (we will use this syntax going forward):

using TypeSupport;

var type = typeof(MyObject);
var typeSupport = type.GetExtendedType();

get information about an array:

var type = typeof(int[]);
var typeSupport = type.GetExtendedType();

var isArray = typeSupport.IsArray; // true
var elementType = typeSupport.ElementType; // int

get information about a Dictionary:

var type = typeof(Dictionary<int, string>);
var typeSupport = type.GetExtendedType();

var isArray = typeSupport.IsDictionary; // true
var elementTypes = typeSupport.GenericArgumentTypes; // System.Int32, System.String

get info about an interface:

var type = typeof(IVehicle);
var typeSupport = type.GetExtendedType();

var isArray = typeSupport.IsInterface; // true
var classesThatImplementICustomInterface = typeSupport.KnownConcreteTypes;
// [] = Car, Truck, Van, Motorcycle

get info about a class:

[Description("A car object")]
public class Car : IVehicle
{
  public string Name { get; set; }
  public Car() { }
}
var type = typeof(Car);
var typeSupport = type.GetExtendedType();

var isArray = typeSupport.HasEmptyConstructor; // true
var attributes = typeSupport.Attributes;
// [] = DescriptionAttribute

working with enums:

public enum Colors : byte
{
  Red = 1,
  Green = 2,
  Blue = 3
}
var type = typeof(Colors);
var typeSupport = type.GetExtendedType();

var isEnum = typeSupport.IsEnum; // true
var enumValues = typeSupport.EnumValues;
// [] = <1, Red>, <2, Green>, <3, blue>
var enumType = typeSupport.EnumType; // System.Byte

working with Tuples:

var tupleType = typeof(Tuple<int, string, double>);
var valueTupleType = typeof((IVehicle, string));
var tupleTypeSupport = type.GetExtendedType();
var valueTupleTypeSupport = valueTupleType.GetExtendedType();

var isTuple = tupleTypeSupport.IsTuple; // true
var isValueTuple = valueTupleTypeSupport.IsValueTuple; // true
var tupleGenericArguments = tupleTypeSupport.GenericArgumentTypes; // System.Int32, System.String, System.Double
var valueTupleGenericArguments = valueTupleTypeSupport.GenericArgumentTypes; // IVehicle, System.String
// there's lots more you can do, such as getting the value from a Tuple instance:

var car = new Car();
var description = "My cool car";
var myTuple = (car, description);
var items = myTuple.GetValueTupleItemObjects();
// [] = Car, "My cool car"

Object factory

Create new objects of any type:

var factory = new ObjectFactory();
var listInstance = factory.CreateEmptyObject<IList<int>>(); // List<int>() 0 elements
var dictionaryInstance = factory.CreateEmptyObject<IDictionary<int, string>>(); // Dictionary<int, string>() 0 elements
var emptyByteArray = factory.CreateEmptyObject<byte[]>(); // byte[0] empty byte array
var byteArray = factory.CreateEmptyObject<byte[]>(length: 64); // byte[64]
var tupleInstance = factory.CreateEmptyObject<(int, string)>(); // tupleInstance.Item1 = 0, tupleInstance.item2 = null
var myComplexObject = factory.CreateEmptyObject<MyComplexObject>();

Create objects without parameterless constructors:

public class CustomObject
{
  public int Id { get; }
  public CustomObject(int id)
  {
    Id = id;
  }
}
var factory = new ObjectFactory();
var myObj = factory.CreateEmptyObject<CustomObject>(); // myObj.GetType() == typeof(CustomObject)

You can instruct the Object factory on how to map abstract interfaces when creating instances:

var typeRegistry = TypeRegistry.Configure((config) => {
  config.AddMapping<IVehicle, Car>();
});

var factory = new ObjectFactory(typeRegistry);
var car = factory.CreateEmptyObject<IVehicle>(); // car.GetType() == typeof(Car)

You can also register custom factories:

var typeRegistry = TypeRegistry.Configure((config) => {
  config.AddFactory<IVehicle, Car>(() => new Car(Color.Red));
});

var factory = new ObjectFactory(typeRegistry);
var car = factory.CreateEmptyObject<IVehicle>(); // car.GetType() == typeof(Car)

Capabilities

  • All basic types, enums, generics, collections and enumerables
  • Internal caching of type examination
  • Constructor analysis (empty constructors, parameterized constructors)
  • Easy listing of valid Enum values
  • Easy listing of concrete types implementing an interface
  • Easy listing of attributes
  • Easy listing of generic arguments
  • Easy listing of properties/fields
  • Easy listing of implemented interfaces
  • Easy listing of Tuple/ValueTuple types
  • Nullable type detection
  • Custom collection information detection
  • Primitive / Struct detection
  • Anonymous type detection
  • High performance testing and optimization

typesupport's People

Contributors

itnmike avatar replaysmike avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.