Have you ever had a class that is generated during the build process or at runtime but you need to add attributes to it? This is the solution! You can add attributes and even properties to classes at runtime by defining an extension class!
- ๐ฑโ๐ค Out of the box support for Ninject!
- ๐ 0 configuration time, just start using.
- โน๏ธ Semantic extensions, easy to use.
- .NET Core support
Say that you are using NSwag to generate swagger based classes, but they change a lot and you are worried that you forget to add some attributes to it and decide to use this library:
First you'll install the NuGet package
Install-Package Proxier
Then you'll create a extension class like so:
public class MyClassExtension : AttributeMapper<MyClass> {
public MyClassExtension(){
//Here you can add any class attributes, properties attributes and even properties!
AddPropertyAttribute(u => u.Business, () => new MyAttribute());
AddProperty("MyProperty", typeof(string));
AddClassAttribute(() => new MyAttribute());
}
}
and then these properties will be added to a static mapper list, the Mapper.TypesOverrides
with all the current extensions. To convert a object from the "MyClass" type to the injected type you would call:
typeof(MyClass).GetInjectedType();
To get a injected object you can use:
var InjectedMyClassObject = MyClassObject.GetInjectedObject();
Note that you can convert the inject object back to the original type by calling
InjectedMyClassObject.CopyTo(MyClassObject);
and all the common properties will be overriden by the ones on the injected type.
You can pass a Kernel to the Mapper initializer like this:
Mapper.InitializeMapperClasses(MyNinjectKernel);
and use the default [Inject]
attribute from Ninject to get properties inside the extension class which in turn can add those into its current extended type.
- AttributeBuilder - Used to generate attributes, converted to .NET Standard
This project is licensed under the MIT License - see the LICENSE.md file for details
This documentation is incomplete and may change overtime.