Posted by: leppie | December 14, 2008

Things you can do in IL, but not C#

I learnt this weekend it is possible to create a delegate that also implements an interface. Why would this be handy? By providing your own ‘overloads’ in this, you can effectively alter the delegate’s ‘calling convention’, without resorting to reflection.

Pseudo example:

interface IFunction
  object Invoke(object arg);
  object Invoke(object[] args);

delegate object Function(object arg) : IFunction;

Now in the background, I emit the following code for the ‘overload’ in the delegate class:

object IFunction.Invoke(object[] args)
  return this(args[0]);

Now we can simply cast a delegate of type Function, to IFunction, and call it with an array. 🙂



  1. Hi Leppie,

    This is not a direct comment on the post, but I think this is a valid place to post.

    I saw your interview on InfoQ about xacc.ide and IronScheme, even though I have not used either. Thank you for your contributions.

    I wanted to play with IronScheme, since learning lispy language was on top my mind since I dropped out of my A.I. class in my college. I am currently stuck with Mac OS X waiting for my Vista 64-bit copy. So, I took the sourcecode of xacc.ide from sourceforge and tried to compile it using MonoDevelop 2.0 alpha 1. I was able to make some minor changes to the source code to get over compilation errors, but apparently landed up with the following error.

    Building: xacc.lexers.managed (Debug)
    Performing main compilation…

    ERROR:object.c:683:compute_class_bitmap: code should not be reached

    at (wrapper managed-to-native) System.Reflection.MonoField.GetValueInternal (object)
    at (wrapper managed-to-native) System.Reflection.MonoField.GetValueInternal (object)
    at System.Reflection.MonoField.GetValue (object)
    at Mono.CSharp.ExternalConstant.ResolveValue ()
    at Mono.CSharp.ConstantExpr.DoResolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.SimpleName.DoSimpleNameResolve (Mono.CSharp.EmitContext,Mono.CSharp.Expression,bool)
    at Mono.CSharp.SimpleName.SimpleNameResolve (Mono.CSharp.EmitContext,Mono.CSharp.Expression,bool)
    at Mono.CSharp.SimpleName.DoResolve (Mono.CSharp.EmitContext,bool)
    at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext,Mono.CSharp.ResolveFlags)
    at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.Assign.DoResolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext,Mono.CSharp.ResolveFlags)
    at Mono.CSharp.Expression.Resolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.EmitContext)
    at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.Block.Resolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.ExplicitBlock.Resolve (Mono.CSharp.EmitContext)
    at Mono.CSharp.EmitContext.ResolveTopBlock (Mono.CSharp.EmitContext,Mono.CSharp.ToplevelBlock,Mono.CSharp.Parameters,Mono.CSharp.IMethodData,bool&)
    at Mono.CSharp.EmitContext.EmitTopBlock (Mono.CSharp.IMethodData,Mono.CSharp.ToplevelBlock)
    at Mono.CSharp.MethodData.Emit (Mono.CSharp.DeclSpace)
    at Mono.CSharp.Method.Emit ()
    at Mono.CSharp.TypeContainer.EmitType ()
    at Mono.CSharp.RootContext.EmitCode ()
    at Mono.CSharp.Driver.Compile ()
    at Mono.CSharp.Driver.Main (string[])
    at (wrapper runtime-invoke) Mono.CSharp.Driver.runtime_invoke_int_string[] (object,intptr,intptr,intptr)

    Build complete — 1 error, 0 warnings

    It may be the result of PInvoke or OS related interoperability, but I thought this is something you can help me out with. I think xacc.ide if ported OS X would be a great tool since it is light-weight and provides good alternatives to MonoDevelop.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: