You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -64,22 +58,18 @@ var operation = (IOperation)Activator.CreateInstance(type);
64
58
// Use our new type
65
59
varresult=operation.Calculate(1, 2);
66
60
```
67
-
<sup><ahref='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Codegen.cs#L29-L66'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_using-assemblygenerator'title='Start of snippet'>anchor</a></sup>
68
-
<!-- endSnippet -->
69
61
70
62
There's only a couple things going on in the code above:
71
63
72
64
1. I added an assembly reference for the .Net assembly that holds the `IOperation` interface
73
-
1. I passed a string to the `GenerateCode()` method, which successfully compiles my code and hands me back a .Net [Assembly](https://msdn.microsoft.com/en-us/library/system.reflection.assembly(v=vs.110).aspx) object
74
-
1. Load the newly generated type from the new Assembly
75
-
1. Use the new `IOperation`
65
+
2. I passed a string to the `GenerateCode()` method, which successfully compiles my code and hands me back a .Net [Assembly](https://msdn.microsoft.com/en-us/library/system.reflection.assembly(v=vs.110).aspx) object
66
+
3. Load the newly generated type from the new Assembly
67
+
4. Use the new `IOperation`
76
68
77
69
If you're not perfectly keen on doing brute force string manipulation to generate your code, you can
78
70
also use Lamar's built in [ISourceWriter](/guide/compilation/source-writer) to generate some of the code for you with
Copy file name to clipboardExpand all lines: docs/guide/compilation/frames/index.md
+3-23
Original file line number
Diff line number
Diff line change
@@ -19,21 +19,15 @@ The various types represent:
19
19
20
20
Alright then, let's make this concrete. Let's say that we want to generate and use dynamic instances of this interface:
21
21
22
-
<!-- snippet: sample_ISaySomething -->
23
-
<aid='snippet-sample_isaysomething'></a>
24
22
```cs
25
23
publicinterfaceISaySomething
26
24
{
27
25
voidSpeak();
28
26
}
29
27
```
30
-
<sup><ahref='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L67-L72'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_isaysomething'title='Start of snippet'>anchor</a></sup>
31
-
<!-- endSnippet -->
32
28
33
29
Moreover, I want a version of `ISaySomething` that will call the following method and write the current time to the console:
34
30
35
-
<!-- snippet: sample_NowSpeaker -->
36
-
<aid='snippet-sample_nowspeaker'></a>
37
31
```cs
38
32
publicstaticclassNowSpeaker
39
33
{
@@ -43,15 +37,11 @@ public static class NowSpeaker
43
37
}
44
38
}
45
39
```
46
-
<sup><ahref='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L57-L65'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_nowspeaker'title='Start of snippet'>anchor</a></sup>
47
-
<!-- endSnippet -->
48
40
49
41
Our dynamic class for ISaySomething will need to pass the current time to the now parameter of that method. To help out here, there's some built in helpers in Lamar specifically to write in the right code to get the current time to a variable of DateTime or DateTimeOffset that is named "now."
50
42
51
43
To skip ahead a little bit, let's generate a new class and object with the following code:
52
44
53
-
<!-- snippet: sample_write-new-method -->
54
-
<aid='snippet-sample_write-new-method'></a>
55
45
```cs
56
46
// Configures the code generation rules
57
47
// and policies
@@ -79,8 +69,6 @@ method.Frames.Add(@call);
79
69
// Compile the new code!
80
70
assembly.CompileAll();
81
71
```
82
-
<sup><ahref='https://github.com/JasperFx/lamar/blob/master/src/LamarCompiler.Testing/Samples/Frames.cs#L23-L49'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_write-new-method'title='Start of snippet'>anchor</a></sup>
83
-
<!-- endSnippet -->
84
72
85
73
After all that, if we interrogate the source code for the generated type above (type.SourceCode), we'd see this ugly generated code:
86
74
@@ -106,8 +94,6 @@ Some notes about the generated code:
106
94
107
95
So now let's look at how Lamar was able to add the code to pass along DateTime.UtcNow. First off, let's look at the code that just writes out the date variable:
108
96
109
-
<!-- snippet: sample_NowFetchFrame -->
110
-
<aid='snippet-sample_nowfetchframe'></a>
111
97
```cs
112
98
publicclassNowFetchFrame : SyncFrame
113
99
{
@@ -127,22 +113,18 @@ public class NowFetchFrame : SyncFrame
127
113
}
128
114
}
129
115
```
130
-
<sup><ahref='https://github.com/JasperFx/lamar/blob/master/src/LamarCodeGeneration/Model/NowTimeVariableSource.cs#L31-L49'title='Snippet source file'>snippet source</a> | <ahref='#snippet-sample_nowfetchframe'title='Start of snippet'>anchor</a></sup>
131
-
<!-- endSnippet -->
132
116
133
117
In the frame above, you'll see that the `GenerateCode()` method writes its code into the source, then immediately turns around and tells the next Frame - if there is one - to generated its code. As the last step to write out the new source code, Lamar:
134
118
135
119
1. Goes through an effort to find any missing frames and variables
136
-
1. Sorts them with a topological sort (what frames depend on what other frames or variables, what variables are used or created by what frames)
137
-
1. Organizes the frames into a single linked list
138
-
1. Calls `GenerateCode()` on the first frame
120
+
2. Sorts them with a topological sort (what frames depend on what other frames or variables, what variables are used or created by what frames)
121
+
3. Organizes the frames into a single linked list
122
+
4. Calls `GenerateCode()` on the first frame
139
123
140
124
In the generated method up above, the call to `NowSpeaker.Speak(now)` depends on the now variable which is in turn created by the `NowFetchFrame`, and that's enough information for Lamar to order things and generate the final code.
141
125
142
126
Lastly, we had to use a custom `IVariableSource` to teach Lamar how to resolve the now variable. That code looks like this:
0 commit comments