Class CilInstructionCollection
- Namespace
- AsmResolver.DotNet.Code.Cil
- Assembly
- AsmResolver.DotNet.dll
Represents a collection of CIL instructions found in a method body.
public class CilInstructionCollection : IList<CilInstruction>, ICollection<CilInstruction>, IReadOnlyList<CilInstruction>, IReadOnlyCollection<CilInstruction>, IEnumerable<CilInstruction>, IEnumerable
- Inheritance
-
CilInstructionCollection
- Implements
- Inherited Members
Constructors
CilInstructionCollection(CilMethodBody)
Creates a new collection of CIL instructions stored in a method body.
public CilInstructionCollection(CilMethodBody body)
Parameters
bodyCilMethodBodyThe method body that owns the collection.
Properties
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
EndLabel
Gets the label indicating the end of the CIL code stream.
public ICilLabel EndLabel { get; }
Property Value
Remarks
This label does not point to the beginning of an instruction. The offset of this label is equal to the last instruction's offset + its size.
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[int]
Gets or sets the element at the specified index.
public CilInstruction this[int index] { get; set; }
Parameters
indexintThe zero-based index of the element to get or set.
Property Value
- CilInstruction
The element at the specified index.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The property is set and the IList<T> is read-only.
Owner
Gets the method body that owns the collection of CIL instructions.
public CilMethodBody Owner { get; }
Property Value
Size
Gets the size in bytes of the collection.
public int Size { get; }
Property Value
Methods
Add(CilInstruction)
Adds an item to the ICollection<T>.
public void Add(CilInstruction item)
Parameters
itemCilInstructionThe object to add to the ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Add(CilOpCode)
Verifies and adds an instruction to the end of the collection.
public CilInstruction Add(CilOpCode code)
Parameters
codeCilOpCodeThe code.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation requires an operand.
Add(CilOpCode, CilLocalVariable)
Verifies and adds a instruction to the end of the collection that references a local variable.
public CilInstruction Add(CilOpCode code, CilLocalVariable variable)
Parameters
codeCilOpCodeThe opcode.
variableCilLocalVariableThe referenced variable.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a variable.
- ArgumentNullException
Occurs when
variableis null.
Add(CilOpCode, Parameter)
Verifies and adds a instruction to the end of the collection that references a parameter.
public CilInstruction Add(CilOpCode code, Parameter parameter)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a parameter.
- ArgumentNullException
Occurs when
parameteris null.
Add(CilOpCode, IFieldDescriptor)
Verifies and adds an instruction to the end of the collection that references a field.
public CilInstruction Add(CilOpCode code, IFieldDescriptor field)
Parameters
codeCilOpCodeThe field opcode.
fieldIFieldDescriptorThe field referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a field opcode.
- ArgumentNullException
Occurs when
fieldis null.
Add(CilOpCode, IMetadataMember)
Verifies and adds an instruction to the end of the collection that references a metadata member.
public CilInstruction Add(CilOpCode code, IMetadataMember member)
Parameters
codeCilOpCodeThe method opcode.
memberIMetadataMemberThe member referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a member opcode.
- ArgumentNullException
Occurs when
memberis null.
Add(CilOpCode, IMethodDescriptor)
Verifies and adds an instruction to the end of the collection that references a method.
public CilInstruction Add(CilOpCode code, IMethodDescriptor method)
Parameters
codeCilOpCodeThe method opcode.
methodIMethodDescriptorThe method referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a method opcode.
- ArgumentNullException
Occurs when
methodis null.
Add(CilOpCode, ITypeDefOrRef)
Verifies and adds an instruction to the end of the collection that references a type.
public CilInstruction Add(CilOpCode code, ITypeDefOrRef type)
Parameters
codeCilOpCodeThe type opcode.
typeITypeDefOrRefThe type referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a type opcode.
- ArgumentNullException
Occurs when
typeis null.
Add(CilOpCode, MemberReference)
Verifies and adds an instruction to the end of the collection that references a member.
public CilInstruction Add(CilOpCode code, MemberReference member)
Parameters
codeCilOpCodeThe member opcode.
memberMemberReferenceThe member referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation does not match with the provided member reference.
- ArgumentNullException
Occurs when
memberis null.
Add(CilOpCode, StandAloneSignature)
Verifies and adds a instruction to the end of the collection that references a standalone signature referencing a method signature in the blob stream.
public CilInstruction Add(CilOpCode code, StandAloneSignature signature)
Parameters
codeCilOpCodeThe opcode.
signatureStandAloneSignatureThe referenced signature.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a standalone signature.
Add(CilOpCode, ICilLabel)
Verifies and adds a branch instruction to the end of the collection.
public CilInstruction Add(CilOpCode code, ICilLabel label)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelis null.
Add(CilOpCode, params ICilLabel[])
Verifies and adds a switch instruction to the end of the collection.
public CilInstruction Add(CilOpCode code, params ICilLabel[] labels)
Parameters
codeCilOpCodeThe switch opcode.
labelsICilLabel[]The labels referenced by the switch instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelsis null.
Add(CilOpCode, MetadataToken)
Verifies and adds a instruction to the end of the collection that references a metadata member by its token.
public CilInstruction Add(CilOpCode code, MetadataToken token)
Parameters
codeCilOpCodeThe opcode.
tokenMetadataTokenThe token of the referenced member.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a metadata member.
- ArgumentOutOfRangeException
Occurs when the provided token is not valid in a CIL stream.
Add(CilOpCode, IEnumerable<ICilLabel>)
Verifies and adds a switch instruction to the end of the collection.
public CilInstruction Add(CilOpCode code, IEnumerable<ICilLabel> labels)
Parameters
codeCilOpCodeThe switch opcode.
labelsIEnumerable<ICilLabel>The labels referenced by the switch instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelsis null.
Add(CilOpCode, double)
Verifies and adds a instruction to the end of the collection that references a float64 constant.
public CilInstruction Add(CilOpCode code, double constant)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a float64 constant.
Add(CilOpCode, int)
Verifies and adds a instruction to the end of the collection that pushes an integer constant.
public CilInstruction Add(CilOpCode code, int constant)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing an integer constant.
Add(CilOpCode, long)
Verifies and adds a instruction to the end of the collection that pushes a 64-bit integer constant.
public CilInstruction Add(CilOpCode code, long constant)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a 64-bit integer constant.
Add(CilOpCode, float)
Verifies and adds a instruction to the end of the collection that references a float32 constant.
public CilInstruction Add(CilOpCode code, float constant)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a float32 constant.
Add(CilOpCode, string)
Verifies and adds a instruction to the end of the collection that pushes a string constant.
public CilInstruction Add(CilOpCode code, string constant)
Parameters
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a string constant.
- ArgumentNullException
Occurs when
constantis null.
AddRange(IEnumerable<CilInstruction>)
Adds a collection of CIL instructions to the end of the list.
public void AddRange(IEnumerable<CilInstruction> items)
Parameters
itemsIEnumerable<CilInstruction>The instructions to add.
CalculateOffsets()
Calculates the offsets of each instruction in the list.
public void CalculateOffsets()
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Contains(CilInstruction)
Determines whether the ICollection<T> contains a specific value.
public bool Contains(CilInstruction item)
Parameters
itemCilInstructionThe object to locate in the ICollection<T>.
Returns
- bool
true if
itemis found in the ICollection<T>; otherwise, false.
CopyTo(CilInstruction[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public void CopyTo(CilInstruction[] array, int arrayIndex)
Parameters
arrayCilInstruction[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
ExpandMacros()
Simplifies the CIL instructions by transforming macro instructions to their expanded form.
public void ExpandMacros()
Remarks
This method reverses any optimizations done by OptimizeMacros().
GetEnumerator()
Returns an enumerator that enumerates through the instructions sequentially.
public CilInstructionCollection.Enumerator GetEnumerator()
Returns
- CilInstructionCollection.Enumerator
The enumerator.
GetLabel(int)
Gets a label at the provided offset.
public ICilLabel GetLabel(int offset)
Parameters
offsetintThe offset.
Returns
- ICilLabel
The label.
Remarks
If the provided offset falls outside of the CIL code stream, EndLabel is returned instead.
IndexOf(CilInstruction)
Determines the index of a specific item in the IList<T>.
public int IndexOf(CilInstruction item)
Parameters
itemCilInstructionThe object to locate in the IList<T>.
Returns
- int
The index of
itemif found in the list; otherwise, -1.
Insert(int, CilInstruction)
Inserts an item to the IList<T> at the specified index.
public void Insert(int index, CilInstruction item)
Parameters
indexintThe zero-based index at which
itemshould be inserted.itemCilInstructionThe object to insert into the IList<T>.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The IList<T> is read-only.
Insert(int, CilOpCode)
Verifies and inserts an instruction into the collection.
public CilInstruction Insert(int index, CilOpCode code)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe code.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation requires an operand.
Insert(int, CilOpCode, CilLocalVariable)
Verifies and inserts a instruction into the collection that references a local variable.
public CilInstruction Insert(int index, CilOpCode code, CilLocalVariable variable)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
variableCilLocalVariableThe referenced variable.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a variable.
- ArgumentNullException
Occurs when
variableis null.
Insert(int, CilOpCode, Parameter)
Verifies and inserts a instruction into the collection that references a parameter.
public CilInstruction Insert(int index, CilOpCode code, Parameter parameter)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
parameterParameterThe referenced parameter.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a parameter.
- ArgumentNullException
Occurs when
parameteris null.
Insert(int, CilOpCode, IFieldDescriptor)
Verifies and inserts an instruction into the collection that references a field.
public CilInstruction Insert(int index, CilOpCode code, IFieldDescriptor field)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe field opcode.
fieldIFieldDescriptorThe field referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a field opcode.
- ArgumentNullException
Occurs when
fieldis null.
Insert(int, CilOpCode, IMetadataMember)
Verifies and inserts an instruction into the collection that references a metadata member.
public CilInstruction Insert(int index, CilOpCode code, IMetadataMember member)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe method opcode.
memberIMetadataMemberThe member referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a member opcode.
- ArgumentNullException
Occurs when
memberis null.
Insert(int, CilOpCode, IMethodDescriptor)
Verifies and inserts an instruction into the collection that references a method.
public CilInstruction Insert(int index, CilOpCode code, IMethodDescriptor method)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe method opcode.
methodIMethodDescriptorThe method referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a method opcode.
- ArgumentNullException
Occurs when
methodis null.
Insert(int, CilOpCode, ITypeDefOrRef)
Verifies and inserts an instruction into the collection that references a type.
public CilInstruction Insert(int index, CilOpCode code, ITypeDefOrRef type)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe type opcode.
typeITypeDefOrRefThe type referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a type opcode.
- ArgumentNullException
Occurs when
typeis null.
Insert(int, CilOpCode, MemberReference)
Verifies and inserts an instruction into the collection that references a member.
public CilInstruction Insert(int index, CilOpCode code, MemberReference member)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe member opcode.
memberMemberReferenceThe member referenced by the instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation does not match with the provided member reference.
- ArgumentNullException
Occurs when
memberis null.
Insert(int, CilOpCode, StandAloneSignature)
Verifies and inserts a instruction into the collection that references a standalone signature referencing a method signature in the blob stream.
public CilInstruction Insert(int index, CilOpCode code, StandAloneSignature signature)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
signatureStandAloneSignatureThe referenced signature.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a standalone signature.
Insert(int, CilOpCode, ICilLabel)
Verifies and inserts a branch instruction into the collection.
public CilInstruction Insert(int index, CilOpCode code, ICilLabel label)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe branch opcode.
labelICilLabelThe label referenced by the branch instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelis null.
Insert(int, CilOpCode, params ICilLabel[])
Verifies and inserts a switch instruction into the collection.
public CilInstruction Insert(int index, CilOpCode code, params ICilLabel[] labels)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe switch opcode.
labelsICilLabel[]The labels referenced by the switch instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelsis null.
Insert(int, CilOpCode, MetadataToken)
Verifies and inserts a instruction into the collection that references a metadata member by its token.
public CilInstruction Insert(int index, CilOpCode code, MetadataToken token)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
tokenMetadataTokenThe token of the referenced member.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a metadata member.
- ArgumentOutOfRangeException
Occurs when the provided token is not valid in a CIL stream.
Insert(int, CilOpCode, IEnumerable<ICilLabel>)
Verifies and inserts a switch instruction into the collection.
public CilInstruction Insert(int index, CilOpCode code, IEnumerable<ICilLabel> labels)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe switch opcode.
labelsIEnumerable<ICilLabel>The labels referenced by the switch instruction.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not a branch opcode.
- ArgumentNullException
Occurs when
labelsis null.
Insert(int, CilOpCode, double)
Verifies and inserts a instruction into the collection that references a float64 constant.
public CilInstruction Insert(int index, CilOpCode code, double constant)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
constantdoubleThe constant to push.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a float64 constant.
Insert(int, CilOpCode, int)
Verifies and inserts a instruction into the collection that pushes an integer constant.
public CilInstruction Insert(int index, CilOpCode code, int constant)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
constantintThe constant to push.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing an integer constant.
Insert(int, CilOpCode, long)
Verifies and inserts a instruction into the collection that pushes a 64-bit integer constant.
public CilInstruction Insert(int index, CilOpCode code, long constant)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
constantlongThe constant to push.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a 64-bit integer constant.
Insert(int, CilOpCode, float)
Verifies and inserts a instruction into the collection that references a float32 constant.
public CilInstruction Insert(int index, CilOpCode code, float constant)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
constantfloatThe constant to push.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a float32 constant.
Insert(int, CilOpCode, string)
Verifies and inserts a instruction into the collection that pushes a string constant.
public CilInstruction Insert(int index, CilOpCode code, string constant)
Parameters
indexintThe zero-based index at which the instruction should be inserted at.
codeCilOpCodeThe opcode.
constantstringThe constant to push.
Returns
- CilInstruction
The created instruction.
Exceptions
- InvalidCilInstructionException
Occurs when the provided operation is not an opcode referencing a string constant.
- ArgumentNullException
Occurs when
constantis null.
InsertRange(int, IEnumerable<CilInstruction>)
Inserts a collection of CIL instructions at the provided index.
public void InsertRange(int index, IEnumerable<CilInstruction> items)
Parameters
indexintThe index to insert the instructions into.
itemsIEnumerable<CilInstruction>The instructions to insert.
OptimizeMacros()
Optimizes all instructions to use the least amount of bytes required to encode all operations.
public void OptimizeMacros()
Remarks
This method reverses any effects introduced by ExpandMacros().
Remove(CilInstruction)
Removes the first occurrence of a specific object from the ICollection<T>.
public bool Remove(CilInstruction item)
Parameters
itemCilInstructionThe object to remove from the ICollection<T>.
Returns
- bool
true if
itemwas successfully removed from the ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
RemoveAt(int)
Removes the IList<T> item at the specified index.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The IList<T> is read-only.
RemoveAt(int, IEnumerable<int>)
Removes a set of CIL instructions based on a list of indices that are relative to a starting index.
public void RemoveAt(int baseIndex, IEnumerable<int> relativeIndices)
Parameters
baseIndexintThe base index.
relativeIndicesIEnumerable<int>The indices relative to
baseIndexto remove.
Exceptions
- ArgumentOutOfRangeException
Occurs when any relative index in
relativeIndicesresults in an index that is out of bounds of the instruction collection.
RemoveAt(int, params int[])
Removes a set of CIL instructions based on a list of indices that are relative to a starting index.
public void RemoveAt(int baseIndex, params int[] relativeIndices)
Parameters
Exceptions
- ArgumentOutOfRangeException
Occurs when any relative index in
relativeIndicesresults in an index that is out of bounds of the instruction collection.
RemoveRange(IEnumerable<CilInstruction>)
Removes a set of CIL instructions from the collection.
public void RemoveRange(IEnumerable<CilInstruction> items)
Parameters
itemsIEnumerable<CilInstruction>The instructions to remove.
RemoveRange(int, int)
Removes a range of CIL instructions from the collection.
public void RemoveRange(int index, int count)