
January 9, 2010 06:56 by
Steele
I was working on a conversion of a Visitor Pattern and this was not giving me much information to go on to resolve the problem:
See: http://msdn.microsoft.com/en-us/library/bb534318.aspx
.NET Framework Class Library
Expression.New Method(ConstructorInfo, IEnumerable(Expression), MemberInfo[])
Creates a NewExpression that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified as an array.
The usage sample for VB:
returnValue = Expression.New(constructor, arguments, members)
This will not Compile, you will get the following error:
Constructor call is valid only as the first statement in an instance constructor.
This is an erroneous error since that is not actually what we are trying to do. We are trying to call a method that just happens to be the same name as a Constructor in VB.
To work around this, surround the New keyword with brackets:
returnValue = Expression.[New](constructor, arguments, members)
You will not be prompted to do this, nor will intellisense change it for you, it can be rather frustrating when you are not sure what the problem is.
You will find this also happens when trying to use System.Enum at various times, this one however is not consistent it depends on what namespaces you have Imported.
This same problem occurs in different variations for other Methods or Properties with names that are special keywords, surrounding them with brackets is the solution.
33e209a5-a7ec-46c9-9681-be6fe091ad06|0|.0