表达式树在VisitMemberAssignment中转换

时间:2015-10-20 20:05:12

标签: c# linq expression-trees

我试图在ExpressionVisitors中重写一个值覆盖VisitMemberAssignment

protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
{
    if (null == node.Expression)
    {
        return base.VisitMemberAssignment(node);
    }
    var targetExpression = this.Visit(node.Expression);
    var sourceType = node.Member.DeclaringType;
    var targetType = this.ConvertType(sourceType);
    return Expression.Bind(this.ConvertMemberInfo(sourceType, node.Member, targetType), targetExpression);
}

我的问题是Expression Target的类型是Int32。 Node.Member Reflection信息表明它需要Enumsration of Enums.PhoneType。

有谁知道如何将Expression Targets类型转换为枚举?

修改

// trys to map type base 
protected Type ConvertType(Type type)
{
    return this._typeConverter.MapType(type);
}

protected virtual MemberInfo ConvertMemberInfo(Type sourceType, MemberInfo sourceMemberInfo, Type targetType)
{
    // TODO: Support overloaded members?
    return GetMemberWithAnyBinding(targetType, sourceMemberInfo).Single();
}

protected static IEnumerable<MemberInfo> GetMemberWithAnyBinding(Type targetType, MemberInfo sourceMemberInfo)
{
  return Enumerable.Union(targetType.GetMember(sourceMemberInfo.Name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic),
  targetType.GetMember(sourceMemberInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
}

0 个答案:

没有答案