Recently, I have been working on a custom LINQ provider in C#. In a later post (when I have more time to write) I will go in depth into what I am worked on and what have I learned about writing a LINQ provider. But for now I will present a simple function that simplified part of my work. It is called GetMemberTypeChain (not as bad as it sounds). Given a member access expression it will return the types of all parts of the member access chain. What this means is that given some expression like “myObject.FieldA.FiledB” it will return a list containing the type of each field/property.
In addition to this the method also allows you to filter out compiler created types. When you have a member access which is using a parameter from a FROM clause in a LINQ query the compiler will often createone or more anonymous type. Then unknowlingly the object you are trying to access now has a few anonymous field accesses in front of it. For my purposes I wanted to be able to easily just analyze the types of the relevant parts of the expression.
So without further adieu … GetMemberTypeChain:
private List<Type> GetMemberTypeChain(Expression expression, bool ignoreCompilerGeneratedTypes) { if (expression == null) return null; if (expression.NodeType == ExpressionType.MemberAccess) { MemberExpression memberExpression = (MemberExpression)expression; var result = GetMemberTypeChain(memberExpression.Expression, ignoreCompilerGeneratedTypes); Type type = memberExpression.Type; if (!(ignoreCompilerGeneratedTypes && IsCompilerGeneratedType(type))) result.Add(type); return result; } else if (expression.NodeType == ExpressionType.Constant || expression.NodeType == ExpressionType.Parameter) { var typeList = new List<CustomTypeInfo>(); Type type = expression.Type; if (!(ignoreCompilerGeneratedTypes && IsCompilerGeneratedType(type))) typeList.Add(type); return typeList; } else { throw new NotSupportedException("Expression type not supported: " + expression.GetType().FullName); } } private bool IsCompilerGeneratedType(Type type) { var compilerGenerateds = type.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true); return (compilerGenerateds != null && compilerGenerateds.Length > 0); }