Git Product home page Git Product logo

typeextender's Introduction

TypeExtender

A light-weight (actually contains one class) library that extends any unsealed type at runtime.

forthebadge

Actions Status Actions Status nuget Nuget

Built by Ndubuisi Jr Chukuigwe


Usage

Simply install nuget package and you are ready to zoom off!

Creating a new class

var className = "ClassA";
var typeExtender = new TypeExtender(className);
var returnedType = typeExtender.FetchType();
var obj = Activator.CreateInstance(returnedClass);

Extending an existing class

var className = "ClassA";
var baseType = typeof(List<string>);
var typeExtender = new TypeExtender(className, baseType);
var returnedClass = typeExtender.FetchType();
var obj = Activator.CreateInstance(returnedClass);

Adding CustomAttribute to type

 var typeExtender = new TypeExtender("ClassA");
 typeExtender.AddAttribute<CustomAAttribute>(new object[] { "Jon Snow" });

Adding Properties to type

There are several overloads on the AddProperty method. Few of them are shown below:

var typeExtender = new TypeExtender("ClassA");
typeExtender.AddProperty("IsAdded", typeof(bool));
typeExtender.AddProperty("IsEnabled", typeof(bool), true);
typeExtender.AddProperty<double>("Length");
typeExtender.AddProperty<double>("Width", true);

You can add properties with custom attributes

 var attributeType = typeof(CustomAAttribute);
 var attributeParams = new object[] { "Jon Snow" };
 var typeExtender = new TypeExtender("ClassA");
 typeExtender.AddProperty("IsAdded", typeof(bool), attributeType, attributeParams);

Adding Field to Type

The AddField method helps add a field to the type you are creating or extending

  var typeExtender = new TypeExtender("ClassA");
  typeExtender.AddField("IsAdded", typeof(bool)); // first overload
  typeExtender.AddField<bool>("IsEnabled"); // second overload

typeextender's People

Contributors

jindrichsusen avatar ndubuisi-ccl avatar ndubuisijr avatar teamecsterconnect avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typeextender's Issues

Add attributes to /existing/ properties?

Hi,

I found this library from your comment here: https://stackoverflow.com/questions/14663763/how-to-add-an-attribute-to-a-property-at-runtime

The example you provided adds a new property with some attribute - but is it possible to add an attribute to an existing property?

I'm trying to dynamically add some attributes to some models in an ASP.Net site, and have attempted to work off of examples using TypeDescriptors and providers in that, to alter existing properties. It doesn't seem to take though, as the output from GetType() does not include my new property. I hoped this library had working code, but couldn't find out how to add to existing properties.. :)

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.