Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skbkontur/gremit
.NET Reflection.Emit extensions
https://github.com/skbkontur/gremit
csharp dotnet il ilgenerator reflection
Last synced: 1 day ago
JSON representation
.NET Reflection.Emit extensions
- Host: GitHub
- URL: https://github.com/skbkontur/gremit
- Owner: skbkontur
- License: mit
- Created: 2015-06-09T10:29:34.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-07T18:11:02.000Z (7 months ago)
- Last Synced: 2024-04-28T22:03:29.933Z (7 months ago)
- Topics: csharp, dotnet, il, ilgenerator, reflection
- Language: C#
- Size: 1.61 MB
- Stars: 157
- Watchers: 34
- Forks: 22
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# GrEmit
[![NuGet Status](https://img.shields.io/nuget/v/GrEmit.svg)](https://www.nuget.org/packages/GrEmit/)
[![Build status](https://github.com/skbkontur/gremit/actions/workflows/actions.yml/badge.svg)](https://github.com/skbkontur/gremit/actions)GrEmit is a library containing different helpers for generating code using Reflection.Emit with the main one being GroboIL - a smart wrapper over [ILGenerator](http://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.aspx).
## Usage
GroboIL is a replacement for ILGenerator. Instead of calling ILGenerator.Emit(OpCode, ..), one may call GroboIL.OpCode(..).
### Example
ILGenerator:
```
// .. creating DynamicMethod, MethodBuilder or ConstructorBuilder
var il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_i4_4);
il.EmitCall(OpCodes.Callvirt, someMethod, null);
il.Emit(OpCodes.Ret);
```
GroboIL:
```
// .. creating DynamicMethod, MethodBuilder or ConstructorBuilder
using(var il = new GroboIL(method))
{
il.Ldarg(0);
il.Ldc_I4(4);
il.Call(someMethod);
il.Ret();
}
```## Advantages
Besides more beautiful interface GroboIL has some more advantages over ILGenerator:
- GroboIL has a single method for all instructions from the same family, for instance, instead of 11 instructions OpCodes.Ldelem_* there is one method GroboIL.Ldelem(Type type).
- During code generation GroboIL builds the content of the evaluation stack and validates instructions arguments, and if something is not OK, immediately throws an Exception.
- There is a debug ouput of the code being generated.
- Full generics support.
- It is possible to debug MethodBuilders (use constructor of GroboIL with parameter [ISymbolDocumentWriter](http://msdn.microsoft.com/en-us/library/system.diagnostics.symbolstore.isymboldocumentwriter.aspx)).
- Appropriate performance. For instance, once I had to compile a program with 500000 instructions and it was verified by GroboIL in 3 seconds (I guess there is a way to break performance, but in practice such a program won't occure).Example of debug output:
GroboIL.GetILCode()
```
ldarg.0 // [List]
dup // [List, List]
brtrue notNull_0 // [null]
pop // []
ldc.i4.0 // [Int32]
newarr T // [T[]]
notNull_0: // [{Object: IList, IList, IReadOnlyList}]
ldarg.1 // [{Object: IList, IList, IReadOnlyList}, Func]
call Int32 Enumerable.Sum(IEnumerable, Func)
// [Int32]
ret // []
```## Release Notes
See [CHANGELOG](CHANGELOG.md).