How to create "System.Runtime.CompilerServices.DefaultInterpolatedStringHandler" type dynamically in C#?

903 Views Asked by At

I am trying to create dynamically a type System.Runtime.CompilerServices.DefaultInterpolatedStringHandler in C#.

I try:

 var inst = Activator.CreateInstance(type);

type is System.Type = System.Runtime.CompilerServices.DefaultInterpolatedStringHandler

but this throw exception:

System.NotSupportedException: „Cannot dynamically create an instance of type 'System.Runtime.CompilerServices.DefaultInterpolatedStringHandler'. Reason: Cannot create boxed ByRef-like values.”

Context

I get DefaultInterpolatedStringHandler from methodInfo.GetMethodBody().LocalVariables - this is the list of local method variables visible in IL:

example IL instructions

High-level

I am trying to simulate/execute IL instructions for a method that has $"Some text with {1} value".

For example method:

public string Start()
{
    int i = 4;
    var text = $"Some text with {i} value";
    return text;
}

IL version: enter image description here

I have mostly everything done for the simulation, but I need somehow create this type DefaultInterpolatedStringHandler.

I think is a challange because of: [System.Runtime] and System.Runtime.CompilerServices.

0

There are 0 best solutions below