I’m building a ExpressionConverter that allows me to convert expressions of type
Expression<Func<A1, B1, C1, ..., Z1>> to expressions of type Expression<Func<A2, B2, C2, ..., Z2>>
I have an existing map that maps type A1 to type A2, B1 to B2, C1 to C2 and so on.
So the simple map work easy.
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);
}
the problem cases are marked with //TODO
Since i only map A1 to A2 directly, i need to build those Array types like A2[] dynamically when i see a A1[] (generic case: Func<A1> to Func<A2>, …)
can anyone point me to the right direction/documentation?
For array :
mappedElementType.MakeArrayType();For generics :
return type.GetGenericTypeDefinition().MakeGenericType(mappedGenericTypes);But I missed the point for nested types, most probably because I never faced the problem by myself.