kevin-montrose / Sigil

A fail-fast validating helper for .NET CIL generation
Microsoft Public License
988 stars 92 forks source link

Question: Does Sigil support building generic methods? #38

Closed AlgorithmsAreCool closed 8 years ago

AlgorithmsAreCool commented 8 years ago

Does Sigil support building generic methods?

I have not found a way to specify generic parameters on the method.

Konard commented 8 years ago

https://github.com/kevin-montrose/Sigil/blob/master/Sigil/Emit.Call.cs#L90 - here an example of method with generic (type) parameter. If you need more - you can always make an extension methods for Emiter. But what exactly are you trying to achive?

AlgorithmsAreCool commented 8 years ago

Well I can call a generic method just fine. But I need to be able to specify/define a generic method. When I call Emit.InstanceMethod I kinda need some access to the method builder or an overload that can assign generic parameters to a method builder. I can submit a PR for this when I get a bit more free time.

AlgorithmsAreCool commented 8 years ago

It turns out i was being dense. Sigil can do this just fine.

The trick is you need is to fixup the generic args after you have built the body of the method.

When you call Emit.CreateMethod, you get the raw MethodBuilder that is still flexible so just call MethodBuilder.DefineGenericParameters(string[] names)

AlgorithmsAreCool commented 8 years ago

Ugh, i lied. My tests pass when i was emitting a stub method body with just a return in it. But when i wrote the actual method body it failed with a "Sigil does not currently support generic types" InvalidOperationException. Which answers my question about if they work or not. :(