Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AssemblyGenerator.workbook « mdoc.Test « mdoc - github.com/mono/api-doc-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a65e07b80b3eadf12bcc8ef1ead713f9b7a95f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
id: c70709d1-d0a6-4dec-9ce5-de04540e5de6
title: AssemblyGenerator
uti: com.xamarin.workbook
platforms:
- Console
---

From [this article](https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.isudtreturn\(v=vs.110\).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1)

```csharp
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Threading;
```

```csharp
class CodeEmitter {
    AssemblyBuilder asmBuilder;
    string asmName;
    ModuleBuilder modBuilder;
    public CodeEmitter(string name) {
        asmName = name;
        AssemblyName aname = new AssemblyName { Name = name };
        AppDomain currentDomain = Thread.GetDomain();
        asmBuilder = currentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.RunAndSave);
        modBuilder = asmBuilder.DefineDynamicModule(asmName);
    }

    public TypeBuilder CreateType(string name) {
        return modBuilder.DefineType(name, TypeAttributes.Public);
    }

    public void WriteAssembly(MethodBuilder entryPoint) {
        asmBuilder.SetEntryPoint(entryPoint);
        asmBuilder.Save(asmName);
    }
}
```

```csharp
void main() {
    CodeEmitter e = new CodeEmitter("test-mod.exe");
    TypeBuilder mainClass = e.CreateType("MainClass");

    // main method
    MethodBuilder mBuilder = mainClass.DefineMethod("mainMethod", MethodAttributes.Static);
    ILGenerator ilGen = mBuilder.GetILGenerator();
    ilGen.Emit(OpCodes.Ldstr, "Hello World");
    Type[] type = new [] {typeof(string)};
    MethodInfo writeMI = typeof(Console).GetMethod("WriteLine", type);
    ilGen.EmitCall(OpCodes.Call, writeMI, null);
    ilGen.Emit(OpCodes.Ret);

    Type[] fType = new [] { typeof(IsUdtReturn)};

    // operator overload
    MethodBuilder sBuilder = mainClass.DefineMethod(
        "op_Decrement", 
        MethodAttributes.Static | MethodAttributes.SpecialName | MethodAttributes.Public,
        CallingConventions.Any,
        Type.GetType("System.Void"),
        fType, // rtype required mods
        null, // rtype optional mods
        null, // parameters
        null, // parameter modreq
        null); // parameters modopt
    
    var silGen = sBuilder.GetILGenerator();
    silGen.Emit(OpCodes.Ret);

    // field
    //Type[] fType = new [] { typeof(IsUdtReturn)};
    mainClass.DefineField("modifiedInteger", Type.GetType("System.Type"), fType, null, FieldAttributes.Public);

    // write
    mainClass.CreateType();
    e.WriteAssembly(mBuilder);
    Console.WriteLine("Assembly created");
}
```

```csharp
main();
```