Skip to content

Commit

Permalink
Merge pull request #223 from thomaslevesque/issue222
Browse files Browse the repository at this point in the history
Fix CustomAttributeInfo.FromExpression for VB.NET
  • Loading branch information
jonorossi authored Nov 15, 2016
2 parents 8cc77e7 + be81659 commit 910a007
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ public static CustomAttributeInfo FromExpression(Expression<Func<Attribute>> exp
var namedFields = new List<FieldInfo>();
var fieldValues = new List<object>();

var newExpression = expression.Body as NewExpression;
var body = UnwrapBody(expression.Body);

var newExpression = body as NewExpression;
if (newExpression == null)
{
var memberInitExpression = expression.Body as MemberInitExpression;
var memberInitExpression = body as MemberInitExpression;
if (memberInitExpression == null)
{
throw new ArgumentException("The expression must be either a simple constructor call or an object initializer expression");
Expand Down Expand Up @@ -151,6 +153,20 @@ public static CustomAttributeInfo FromExpression(Expression<Func<Attribute>> exp
fieldValues.ToArray());
}

private static Expression UnwrapBody(Expression body)
{
// In VB.NET, a lambda expression like `Function() New MyAttribute()` introduces
// a conversion to the return type. We need to remove this conversion expression
// to get the actual constructor call.

var convertExpression = body as UnaryExpression;
if (convertExpression != null && convertExpression.NodeType == ExpressionType.Convert)
{
return convertExpression.Operand;
}
return body;
}

private static object GetAttributeArgumentValue(Expression arg, bool allowArray)
{
var constant = arg as ConstantExpression;
Expand Down

0 comments on commit 910a007

Please sign in to comment.