Bug: delegate void A1ByRef(ref int x);

Feb 2, 2009 at 1:52 PM

Dear all,

I could reproduce the following bug report on Preview 4:

http://www.codeplex.com/WorkItem/View.aspx?ProjectName=MEF&WorkItemId=6427

In a few words, imported/exported delegates with ref actual parameters are buggy (or "not supported at all"):

public delegate void A1ByRef<T1>(ref T1 x);

I am biased of course, but it seems like an important bug/missing feature. Any chance to have a fix for the next release?

Thanks,
Sebastien.


Feb 2, 2009 at 7:50 PM
I am pretty confident when I say that MEF doesn't currently support generic arguments for method exports either. The reason is simple. A method export is wrapped up in an Action<...> or Func<...> delegate and it would be hard to capture and expose a generic parameter in the strong-typed delegate wrapper. I believe there are some issues with using ref/out in delegates as well
Feb 2, 2009 at 8:15 PM

Indeed. Since generic arguments are not supported for method exports, one should replace the delegate declaration by:

public delegate void A1ByRef(ref int x);

I will update the bug report.

My post was precisely on the problem of ref parameters: does the MEF team plans to support ref parameters ?

It's very easy to reproduce the bug, but for convenience here is part of the stack trace:

   at System.RuntimeType.ThrowIfTypeNeverValidGenericArgument(Type type)
   at System.RuntimeType.SanityCheckGenericArguments(Type[] genericArguments, Type[] genericParamters)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at System.ComponentModel.Composition.ReflectionModel.ReflectionMethod.GetDelegate(MethodInfo method, Object instance) in c:\...\Preview4\src\ComponentModel\System\ComponentModel\Composition\ReflectionModel\ReflectionMethod.cs:line 70

"The type 'System.Int32&' may not be used as a type argument."

In class ReflectionMethod:

Type delegateType = typeArray[parameters.Length].IsGenericType ?
                typeArray[parameters.Length].MakeGenericType(genericArgTypes) :
                typeArray[parameters.Length];




Sebastien.


Feb 2, 2009 at 9:31 PM
Yeah but the issue with out/ref parameters is that you cannot use them as type parameters on the strong-type delegate. You cannot wrap

public void Foo(ref int x)

into

Action<ref int>

since thats an invalid syntax. Even if u detected that the parameter was out/ref and stipped it (so from System.Int32& to System.Int32) then you could have no means of
getting back the updated reference through the strong-typed delegate anyway.

So this comes down to a .NET issue, not a MEF issue
Feb 2, 2009 at 9:54 PM


Action<ref int> is not valid but delegate void A1ByRef(ref int x) is valid (see the "direct" delegate call in the bug report).


Feb 3, 2009 at 1:06 AM

Sebastien

Thanks for the bug report, unfortunately, I don't think we'll have time to support custom delegate types for v1. Only methods that can be represented as Func<> or Action delegate will likely be supported.

However, I'll make sure we'll clean up that error message, and I'll add your feature request to support custom delegates to our internal feature list.

As a workaround, you could define a interface with a single method with the same signature as your custom delegate:

interface IFuncByRef<T>
{
     void Func(ref T value);
}

Regards

David