2012-01-18 20 views
0

通用和数组类型我建立一个ExpressionConverter,让我大厦嵌套,在运行时

Expression<Func<A1, B1, C1, ..., Z1>>类型的表达式转换为类型的表达式Expression<Func<A2, B2, C2, ..., Z2>>

我有一个类型A1现有地图映射键入A2,B1B2,C1C2等等。 所以简单的地图工作很容易。

private Type GetMappingType(Type type) 
{ 
    var types = _mappingFinder.FindTypesFor(type).ToArray(); 
    if (types.Length == 0) 
    { 
     if (type.IsNested) 
     { 
      var nestedTypes = type.GetNestedTypes(); 
      var mappedNestedTypes = nestedTypes.Select(this.GetMappingType).ToArray();    
      //TODO: return the nested type   
     } 
     if (type.HasElementType) 
     { 
      var mappedElementType = this.GetMappingType(type.GetElementType()); 
      //TODO: return the right container type with the mappedElementType 
     } 
     if (type.IsGenericType) 
     { 
      var genericTypes = type.GetGenericArguments(); 
      var mappedGenericTypes = genericTypes.Select(this.GetMappingType).ToArray();      
      //TODO: return generictype with the mappedGenericTypes as arguments 
     } 
     return type; 
    } 
    if (types.Length == 1) 
     return _types.Contains(types[0]) ? type : types[0]; 

    throw new Exception("Too many mapped types for " + type); 
}   

问题的情况下都标有//TODO 因为我只映射到A1直接A2,我需要建立这些数组类型,如A2[]动态,当我看到一个A1[](一般情况下:至Func<A2>,... ) 任何人都可以指向正确的方向/文档?

回答

1

对于数组: mappedElementType.MakeArrayType();

对于仿制药: return type.GetGenericTypeDefinition().MakeGenericType(mappedGenericTypes);

但我错过了嵌套类型来看,最有可能是因为我从来没有通过自己所面临的问题。