Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leonardoporro/detached-runtimetypes
Create clr types at runtime without having to emit opcodes. Method bodies can be defined using expressions.
https://github.com/leonardoporro/detached-runtimetypes
clr dynamic-programming emit expression expression-tree expressions il lambda linq opcodes proxy reflection roslyn types
Last synced: 4 days ago
JSON representation
Create clr types at runtime without having to emit opcodes. Method bodies can be defined using expressions.
- Host: GitHub
- URL: https://github.com/leonardoporro/detached-runtimetypes
- Owner: leonardoporro
- License: mit
- Created: 2021-03-16T14:14:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-22T03:57:42.000Z (12 months ago)
- Last Synced: 2024-12-06T17:47:59.730Z (about 1 month ago)
- Topics: clr, dynamic-programming, emit, expression, expression-tree, expressions, il, lambda, linq, opcodes, proxy, reflection, roslyn, types
- Language: C#
- Homepage:
- Size: 204 KB
- Stars: 8
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Detached Banner](banner.png?raw=true)
# Runtime Types
#### What is itThis library tries to simplify the runtime type creation by adding new things like the ability to define methods using Expressions and
automatic interface implementation.
It's a part of [Detached.Mappers](https://github.com/leonardoporro/Detached-Mapper) library.
Thanks to the people who made [FastExpressionCompiler](https://github.com/dadhi/FastExpressionCompiler), that is the core of this library.## What does it solve
It allows devs to create tools like dynamic proxies, comparers, dirty check (INotifyPropertyChange) or any other thing
that may be a good fit for dynamic code without having to manually emit op codes. Methods can be defined using Expression trees.### How it works
Lets say that we want to dinamically create a type for this interface:
```csharp
public interface ISumService
{
int Sum(int a, int b);
}
```
Then we need to intialize a type builder, define an implementation for Sum method using an expression tree, and
call AutoImplementInterface to perform the override.```csharp
// create a type builder, this will handle the creation of a new Type.
RuntimeTypeBuilder typeBuilder = new RuntimeTypeBuilder("MyISumServiceImplementation", typeof(BasePropertyClass));// define the parameters expected by the interface method.
var aParam = Parameter(typeof(int), "a");
var bParam = Parameter(typeof(int), "b");// define a mehtod with the same signature (name and parameters),
// use "Add" expression as the method body
typeBuilder.DefineMethod(
"Sum",
new[] { aParam, bParam },
Block(
Add(aParam, bParam)
)
);
// implement the interface, this will iterate over all methods and
// call DefineMethodOverride to bind the existing methods to the given
// interface methods
typeBuilder.AutoImplementInterface(typeof(ISumService));// create the type that will be used to initialize new instances of ISumService
Type myISumServiceType = typeBuilder.Create();// create an instance of our dynamic ISumService implementation
ISumService myISumService = (ISumService)Activator.CreateInstance(myISumServiceType);// test the implementation
Assert.Equal(14, myISumService.Sum(5, 9));```
More info and examples will be added later.
Check unit tests for more samples!