How to pass ref struct parameter to MethodInfo if struct has ReadOnlySpan field


cdjc

I have an object representing MethodInfoa method that takes a ref structas a parameter and the struct has a ReadOnlySpan<byte>field. How to call a method through an MethodInfoobject ? I can't use MethodInfo.Invokebecause Invokethe parameter needs to be an object?[]?array, and parameters structwith ReadOnlySpan<byte>fields cannot be cast to object.

So how can I call a method via reflection and pass it a ref struct value (where the struct has field types) ReadOnlySpan?

generally

Since you are structnaturally going to be a ref structthat will live on the stack (and cannot go to the managed heap), you face some compiler limitations, and there are some hurdles to overcome.

  • A reference structure cannot be an element type of an array.
  • A reference struct cannot be the declared type of a class or a field of a non-reference struct.
  • A reference struct cannot implement an interface.
  • Reference structures cannot be boxed to System.ValueType or System.Object.
  • A reference structure cannot be a type parameter. ref struct variables cannot be captured by vlambda expressions or local functions.
  • ref struct variables cannot be used in async methods. However, you can use ref struct variables in synchronized methods, for example, in methods that return Task or Task.
  • ref struct variables cannot be used in iterators.

As you can see, ref structcannot be boxed on heap. You will not be able to, which means to vote and use . However, you can use an expression tree orobjectInvokerelection.emit

When using an expression tree , you will need to use a delegate that cannot be used as a Expression.Lambdatype parameter .ref structFunc

given

public readonly ref struct Bob
{
   public Bob(ReadOnlySpan<byte> myProperty)
      => MyProperty = myProperty;    

   public ReadOnlySpan<byte> MyProperty { get; }
}

...

public static byte DoSomething(Bob bob)
   => bob.MyProperty[1]; // return something from the span ¯\_(ツ)_/¯

delegate byte myDelegate(Bob asd);

...

var bytes = new byte[] {1, 2, 3};
var span = bytes.AsSpan();
var bob = new Bob(span);

usage

var method = typeof(SomeType).GetMethod("DoSomething");
var parameter = Expression.Parameter(typeof(Bob), "b");
var call = Expression.Call(method, parameter);
var expression = Expression.Lambda<myDelegate>(call, parameter);
var func = expression.Compile();
var result = func(bob);

Console.WriteLine(result);

result

2

or with the in parameter modifier, you need to take advantage ofMakeByRefType

When passed as a ref parameter, returns a Type object representing the current type

public static byte DoSomething(in Bob bob)
  => bob.MyProperty[1]; // return something from the span ¯\_(ツ)_/¯

delegate byte myDelegate(in Bob asd);

...

var method = typeof(SomeType).GetMethod("DoSomething", new[] {typeof(Bob).MakeByRefType()});
var parameter = Expression.Parameter(typeof(Bob).MakeByRefType(), "b");
var call = Expression.Call(method, parameter);
var expression = Expression.Lambda<myDelegate>(call, parameter);
var func = expression.Compile();
var result = func(bob);

Note : This code is not fortress perfect expression, but it should work. Also, this is built for staticmethods , if not, you need to pass in an instance

Related


How to pass struct as parameter

Diego_cz I'm writing a piece of C code and should use a static library that contains a function. I have a header file that contains a typedefstruct that should be used as an input parameter. My understanding of functions is that I create a struct of a data typ

How to pass struct as parameter

Diego_cz I'm writing a piece of C code and should use a static library that contains a function. I have a header file that contains a typedefstruct that should be used as an input parameter. My understanding of functions is that I create a struct of a data typ

How to pass a struct as a parameter to a function

Mirabeau: I have several configuration JSON files, each with a specific struc type Currently, I create a function for each file/struct name: type ConfigOne struct { App string `json:"app"` Web string `json:"web"` Archive string `json:"archi

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

How to pass a struct as a parameter to a function?

mess How to pass struct to use as parameter in golang? There is my code: package main import ( "fmt" ) type MyClass struct { Name string } func test(class interface{}) { fmt.Println(class.Name) } func main() { test(MyClass{Name: "Jhon"}) }

How to pass a struct as a parameter to a function

Mirabeau: I have several configuration JSON files, each with a specific struc type Currently, I create a function for each file/struct name: type ConfigOne struct { App string `json:"app"` Web string `json:"web"` Archive string `json:"archi

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

How to pass a struct * as a parameter to a function

unknown I want to pass alloc as a parameter but don't know how, can anyone help me? void parameter(unsigned int argnum, struct resistor* alloc) { /*...*/ } struct resistort { const double E6[6]; double E12[12]; const double E24[24]; char e[3]; doubl

How to pass struct type in parameter?

mk .. How to pass struct type in parameter? I guess it's not possible and still want to check if it works. My requirement is like this Following are the macros list_entryused in the Linux kernel #define list_entry(ptr, type, member) \ ((type *)((char *)(pt

How to pass a struct as a parameter to a function?

mess How to pass struct to use as parameter in golang? There is my code: package main import ( "fmt" ) type MyClass struct { Name string } func test(class interface{}) { fmt.Println(class.Name) } func main() { test(MyClass{Name: "Jhon"}) }

How to pass a struct as a parameter to a function

Mirabeau: I have several configuration JSON files, each with a specific struc type Currently, I create a function for each file/struct name: type ConfigOne struct { App string `json:"app"` Web string `json:"web"` Archive string `json:"archi

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

How to pass struct as parameter to function

Nagri: what should I do? I am trying to pass a structas a parameter to Go. func handleEntityProperties(w http.ResponseWriter, r *http.Request) { const sliceSize = 100 var entityProperties struct { Instance string `json:"instance"` Entit

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

Pass struct parameter in struct function

Owais Qayum I'm testing passing a struct array parameter in a struct function "push" but I get an error message "passing 'shelf' (aka 'struct shelf') to the parameter of incompatible type 'shelf *' (aka 'struct shelf *');". How to fix this error? I don't kn

How to get sizeof of reference struct like ReadOnlySpan?

Joel V. Ernst DeYoung Microsoft recommends in " Writing Safe and Efficient C# Code" : Apply inmodifier to readonly structgreater thanSystem.IntPtr.Size Is there an easy way to check the managed memory size of a ref struct ReadOnlySpan<byte>? The following meth

How to get sizeof of reference struct like ReadOnlySpan?

Joel V. Ernst DeYoung Microsoft recommends in " Writing Safe and Efficient C# Code" : Apply inmodifier to readonly structgreater thanSystem.IntPtr.Size Is there an easy way to check the managed memory size of a ref struct ReadOnlySpan<byte>? The following meth

How to get sizeof of reference struct like ReadOnlySpan?

Joel V. Ernst DeYoung Microsoft recommends in " Writing Safe and Efficient C# Code" : Apply inmodifier to readonly structgreater thanSystem.IntPtr.Size Is there an easy way to check the managed memory size of a ref struct ReadOnlySpan<byte>? The following meth

How to get sizeof of reference struct like ReadOnlySpan?

Joel V. Ernst DeYoung Microsoft recommends in " Writing Safe and Efficient C# Code" : Apply inmodifier to readonly structgreater thanSystem.IntPtr.Size Is there an easy way to check the managed memory size of a ref struct ReadOnlySpan<byte>? The following meth

How to get sizeof of reference struct like ReadOnlySpan?

Joel V. Ernst DeYoung Microsoft recommends in " Writing Safe and Efficient C# Code" : Apply inmodifier to readonly structgreater thanSystem.IntPtr.Size Is there an easy way to check the managed memory size of a ref struct ReadOnlySpan<byte>? The following meth