https://github.com/antelcat/il
Extensions of ILGenerator and the ability to create delegates more easily
https://github.com/antelcat/il
delegate dotnet emit extension il ilgenerator reflection
Last synced: 14 days ago
JSON representation
Extensions of ILGenerator and the ability to create delegates more easily
- Host: GitHub
- URL: https://github.com/antelcat/il
- Owner: Antelcat
- License: mit
- Created: 2023-06-02T07:05:29.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-17T08:14:21.000Z (over 1 year ago)
- Last Synced: 2024-08-21T09:19:54.635Z (8 months ago)
- Topics: delegate, dotnet, emit, extension, il, ilgenerator, reflection
- Language: C#
- Homepage:
- Size: 51.8 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Antelcat.IL
Extensions of ILGenerator and the ability to create delegates more easily
## Emit Extension
Along with [T4](https://learn.microsoft.com/zh-cn/visualstudio/modeling/code-generation-and-t4-text-templates?view=vs-2022) generated [ILExtension (Emit)](https://github.com/Antelcat/Antelcat.Shared/blob/main/src/Shared/IL/Extensions/ILExtension.g.cs), you can use Fluent Style EmitEx like :
```c#
iLGenerator.EmitEx(OpCodes.Ldarg_0)
.EmitEx(OpCodes.Ldind_Ref)
.EmitEx(OpCodes.Unbox_Any, targetType)
.EmitEx(OpCodes.Stloc_0)
.EmitEx(OpCodes.Ldloca, 0)
...
```## Delegates
### Supports
You can use extensions to create targets from these sources :
Source
TargetType
CtorHandler<>ConstructorInfo
PropertyInfo
SetHandler<,> & GetHandler<,>FieldInfo
MethodInfo
InvokeHandler<,>
### UsageCan be easily create delegate when you have a class like:
``` c#
class Foo
{
public Foo(int dependency){}
public int Method(ref int val) => ++val;
public static int StaticMethod(int val, out int source)
{
source = val--;
return val;
}
protected string Property { get; set; }
protected static string StaticProperty { get; set; }
private string field;
private static string staticField;
}
```Then :
``` c#
var type = typeof(Foo);var foo = type.GetConstructors()[0].CreateCtor().Invoke(1);
object? nothing = null;type.GetMethod(nameof(Foo.Method)).CreateInvoker().Invoke(foo, 1);
type.GetMethod(nameof(Foo.StaticMethod)).CreateInvoker().Invoke(null, new object?[]{ 1, null });var prop = type.GetProperty(nameof(Foo.Property));
prop.CreateGetter().Invoke(foo);
prop.CreateSetter().Invoke(ref foo, "value");var staticProp = type.GetProperty(nameof(Foo.StaticProperty));
staticProp.CreateGetter().Invoke(null);
staticProp.CreateSetter().Invoke(ref nothing, "value");var field = type.GetField(nameof(Foo.field));
field.CreateGetter().Invoke(foo);
field.CreateSetter().Invoke(ref foo, "value");var staticField = type.GetField(nameof(Foo.staticField));
staticField.CreateGetter().Invoke(null);
staticField.CreateSetter().Invoke(ref nothing, "value");
```Tests can be found in [UnitTest.cs](./src/Antelcat.IL.Test/UnitTest.cs)