Using ILGenerator to create and populate a List<string>
I'm currently trying to figure out the ILGenerator procedure to add items to a List.
Using www.sharplab.io I can see that this code:
Generates a constructor that looks like this:
I'm trying to replicate the setting of
MyStringList
in my own custom constructor, and I'm getting an error saying that it's an invalid program.
Here's my constructor:
1 Reply
I've marked where I think the error is. If I'm creating an empty list, and I skip the code between the
set list members
comments, it works fine.
My feeling is that the issue is something to do with the (!0)
at the end. I'm not sure what I would do to make this. I've tried changing the potential issue lines to:
which does the same thing as just doing il.Call
. I've also tried passing in an array of a single typeof(string)
instead of null
, but in that situation it says Calling convention must be VarArgs
Any idea how I can re-create this IL dynamically?
Looking into it a little further, I believe that the !0
in the parameter type list for Add
just refers to it being the first generic type argument (i.e. string). A different IL viewer shows that line as: