/
Emit.Box.cs
49 lines (42 loc) · 1.4 KB
/
Emit.Box.cs
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
using Sigil.Impl;
using System;
using System.Reflection.Emit;
namespace Sigil
{
public partial class Emit<DelegateType>
{
/// <summary>
/// Boxes the given value type on the stack, converting it into a reference.
/// </summary>
public Emit<DelegateType> Box<ValueType>()
where ValueType : struct
{
return Box(typeof(ValueType));
}
/// <summary>
/// Boxes the given value type on the stack, converting it into a reference.
/// </summary>
public Emit<DelegateType> Box(Type valueType)
{
if (valueType == null)
{
throw new ArgumentNullException("valueType");
}
if (!valueType.IsValueType || valueType == typeof(void))
{
throw new ArgumentException("Only ValueTypes can be boxed, found " + valueType, "valueType");
}
if (!AllowsUnverifiableCIL && valueType.IsByRef)
{
throw new InvalidOperationException("Box with by-ref types is not verifiable");
}
var transitions =
new[]
{
new StackTransition(new [] { valueType }, new [] { typeof(object) })
};
UpdateState(OpCodes.Box, valueType, Wrap(transitions, "Box"));
return this;
}
}
}