question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

generic struct ref parameters

See original GitHub issue

Suppose you have a generic struct and want to do something neat with this inside some method, like pass it to Console.WriteLine. Maybe you just want to print this out every time the constructor for a generic struct is called. This is just an example, of course.

I can call param.Type.ToByRefSig().Next to get a TypeSig for this, but I can’t convert that into something that I can box up. It seems fairly insistent that it’s NOT a generic sig, and it omits the <T, U> stuff whenever I convert it to a TypeDef with ToTypeDefOrRef(). This kind of makes sense in the context of the tree structure of sigs, and I’m not surprised it doesn’t work.

For return values, which are GenericInstSig for generic structs, I can just call OpCodes.Box.ToInstruction(method.ReturnValue.ToTypeDefOrRef()), and it works great, but I can’t figure out a way to do the same for ref parameters. My first thought was to manually create a GenericInstSig from the TypeSig, but I’m having trouble wrapping my head around that process.

Am I heading along the right track? If so, could you give me some pointers on how to do that? If not, what’s the proper method for getting this done?

Thanks for any help you can provide! I can show some code if anything isn’t clear.

Edit: removed misleading summary

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tgrindingercommented, Mar 3, 2018

@bprg Thanks very much! That’s very helpful.

1reaction
bprgcommented, Mar 3, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why ref structs cannot be used as type arguments?
1 Answer 1 ... The primary guarantee made by a ref struct is that it will never escape to the heap. In a...
Read more >
generic constraint: where T : ref struct · Issue #1148
Today it's not possible to use a ref struct as a type argument to a generic method or method of a generic type....
Read more >
Constraints on type parameters - C# Programming Guide
Learn about constraints on type parameters. Constraints tell the compiler what capabilities a type argument must have.
Read more >
ref structs in C# 7.2 - .NET Concept of the Week - Episode 16
In this episode we talk about 'ref structs', which was introduced in C# ... within a class or another struct, a generic parameter...
Read more >
ref keyword - C# Reference
When used in a method's parameter list, the ref keyword indicates that an argument is passed by reference, not by value. The ref...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found