Could you please suggest work around for .NET 4.0 defect which results into Common Language Runtime detected an invalid program. exception when following program is started (in Visual Studio 2010):
Note: Behavior does not reproduce when the same program is compiled in Visual Studion 2012.
namespace Namespace1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public class Tst1
{
public Action<DataType> Method1<DataType>(Func<DataType> param1) { return this.Method1<DataType>(param1, 0); }
public Action<DataType> Method1<DataType>(Func<DataType> param1, int param2)
{
return param => System.Windows.Forms.MessageBox.Show(param1().ToString() + " " + param.ToString());
}
}
public class TstBase { }
public class Tst2 : TstBase { }
public static class TstExtensions
{
public static string ExtensionMethod<TstType>(this TstType tst)
where TstType : TstBase
{
return "From extension method";
}
}
public class Application
{
public static void Main()
{
Tst1 tst1 = new Tst1();
Tst2 tst2 = new Tst2();
tst1.Method1<string>(tst2.ExtensionMethod)("From main");
}
}
}
Note: Assembly System.Windows.Forms.dll of .NET framework 4.0 needs to be referenced to build project.
Background information
I develop high-abstraction-level script-processing keyword-driven test automation framework upon low-level test automation tool shipped by 3-rd party (automation framework which performs keyword-driven scripts of high abstraction level with 3-rd party tool employed for accessing graphic UI on lower level). Construction listed above is required to implement uniform values validation approach.
Each element of listed code stand for following:
- Tst1 – class for validation of values
- Method1 – polymorphic method that performs validation of values which are being retrieved from UI (user interface) and can accept timeout to wait till UI element will acquire required value
- TstBase – base class for all UI controls handling from 3-rd party low-level automation tool API
- Tst2 – class for handling certain type of controls from 3-rd party low-level automation tool API
- ExtensionMethod – generic method for retrieving text from controls of all types using instance of any control-handler-class from 3-rd party low-level automation tool API
Method1 returns delegate which is passed as argument to other method that retrieves value from test-script-step parameters and immediately makes use of it. Altogether it looks like following:
testStepParameters.MakeUseOf("Field1ExpectedValue", validation.Verify<string>(field1.GetValue));
where validation.Verify<string>(field1.GetValue) goes instead of tst1.Method1<string>(tst2.ExtensionMethod) from first-most code snippet.
Important notice
I found one work-around for the defect but I do not like it because it adds some extent of clumsiness to the code. Work around that I found is to replace direct usage of extension method with Lambda expression – i.e. bug does no reproduce when line:
tst1.Method1<string>(tst2.ExtensionMethod)("From main");
is replaced with line:
tst1.Method1<string>(() => tst2.ExtensionMethod())("From main");
In final form it looks like:
testStepParameters.MakeUseOf("Field1ExpectedValue", validation.Verify<string>(() => field1.GetValue()));
And usage of that work-around becomes completely unpleasant when instead of field1 variable is used a complicated call for retrieving control (probably a delegate returning control – which is especially useful to check control state over time without bothering about how control is retrieved – with its own implementation of GetValue extension method).
You can fix it by creating the delegate yourself instead of letting C# do it for you. Like this: