John Lam 1997 — 2026

Generics and RbDynamicMethod

I added some features today to RbDynamicMethod to support the use of generics on CLR 2.0. To get a handle on the problem that I solved, let’s look at a simple C# example:

List<String> list = new List<String>();
list.Add("one");
list.Add("two");
return String.Format("{0}, {1}", list[0], list[1]);

Here’s the same code created using the .NET DynamicMethod API:

DynamicMethod m = new DynamicMethod(String.Empty,
typeof(void), null,
Assembly.GetExecutingAssembly().GetModules()[0]);
ILGenerator g = m.GetILGenerator();
Type listType = Type.GetType("System.Collections.Generic.List`1[System.String]");
ConstructorInfo ci = listType.GetConstructor(new Type[] { });
MethodInfo add = listType.GetMethod("Add", new Type[] {typeof(string)});
MethodInfo get_Item = listType.GetMethod("get_Item", new Type[] {typeof(Int32)});
Type console = Type.GetType("System.Console");
MethodInfo writeLine = console.GetMethod("WriteLine",
new Type[] {typeof(string), typeof(object), typeof(object)});
LocalBuilder list = g.DeclareLocal(listType);
g.Emit(OpCodes.Newobj, ci);
g.Emit(OpCodes.Stloc_S, list);
g.Emit(OpCodes.Ldloc_S, list);
g.Emit(OpCodes.Ldstr, "one");
g.Emit(OpCodes.Callvirt, add);
g.Emit(OpCodes.Ldloc_S, list);
g.Emit(OpCodes.Ldstr, "two");
g.Emit(OpCodes.Callvirt, add);
g.Emit(OpCodes.Ldstr, "{0}, {1}");
g.Emit(OpCodes.Ldloc_S, list);
g.Emit(OpCodes.Ldc_I4_0);
g.Emit(OpCodes.Callvirt, get_Item);
g.Emit(OpCodes.Ldloc_S, list);
g.Emit(OpCodes.Ldc_I4_1);
g.Emit(OpCodes.Callvirt, get_Item);
g.Emit(OpCodes.Call, writeLine);
g.Emit(OpCodes.Ret);
CallVoidMethod d = (CallVoidMethod)m.CreateDelegate(typeof(CallVoidMethod));

You’ll notice the non-trivial amount of book-keeping code that uses the Reflection API to lookup constructors, types, and methods. By comparison, the CIL generation code looks pretty clean, except for the annoying, repetitive “g.Emit(Opcodes.” text before each instruction.

Now, look at the same example written using RbDynamicMethod:

create_ruby_module_function(RbDynamicMethod, 'create_list_string') do
include   'System.Collections.Generic'
declare   'List<String>', :list
newobj    'List<String>()'
stloc_s   :list
ldloc_s   :list
ldstr     'one'
callvirt  'List<String>::Add(String)'
ldloc_s   :list
ldstr     'two'
callvirt  'List<String>::Add(String)'
ldstr     '{0}, {1}'
ldloc_s   :list
ldc_i4_0
callvirt  'List<String>::get_Item(Int32)'
ldloc_s   :list
ldc_i4_1
callvirt  'List<String>::get_Item(Int32)'
call      'static String::Format(String,Object,Object)'
call      'static Marshal::ToRubyString(String)'
ret
end

A few things have changed since the first drop of the code First, notice that the delimiter between a type name and a method name has changed from a . to a ::. Second, I’ve extended the type syntax to support generic types. Third, I now use Ruby symbols to represent variable and label names. Symbols are interned strings and can be recognized by their ’:’ prefix.

If you call Assembly.GetType(String) to find a generic type, you’ll immediately be struck by the rather obtuse syntax:

System.Collections.Generic.List`1[System.String]

Imagine typing that every time you wanted to reference a method on that type. Ugh. So I extended my code to search through namespaces that are in scope for that type, which yields the much more humane syntax:

List<String>

I’m going to write a few more test cases for this code tonight, but I think that this code is fairly feature complete now. Hopefully I’ll get another drop out tonight for folks who are interested in following along.