Exemplo n.º 1
0
 protected override Expression VisitMemberAccess(MemberExpression m)
 {
     Visit(m.Expression);
     PropertyType = m.Type;
     // try to get the value
     try
     {
         var rootInstance = Expression.Lambda(typeof(Func <object>), m.Expression).Compile().DynamicInvoke();
         if (rootInstance != null)
         {
             {
                 var    pi = m.Member as PropertyInfo;
                 object resultingInstance = null;
                 if (pi != null)
                 {
                     resultingInstance = pi.GetValue(rootInstance, null);
                 }
                 var fi = m.Member as FieldInfo;
                 if (fi != null)
                 {
                     resultingInstance = fi.GetValue(rootInstance);
                 }
                 if (resultingInstance != null)
                 {
                     var propertyPath = ObjectPaths.Get(resultingInstance);
                     if (propertyPath != null)
                     {
                         RootType = propertyPath.TypePrefix;
                         AppendPropertyPath(propertyPath.TypeSuffix);
                         return(m);
                     }
                 }
             }
         }
     }
     catch
     {
     }
     if (RootType == null)
     {
         RootType = ExtractType(m.Member).GetTypeString();
     }
     else
     {
         var name = m.Member.Name;
         AppendPropertyPath(name);
     }
     return(m);
 }
Exemplo n.º 2
0
        public static IEnumerable <TResult> SelectHtml <TSource, TResult>(this IEnumerable <TSource> source,
                                                                          object rootInstance,
                                                                          PropertyPath path,
                                                                          Func <TSource, TResult> selector)
        {
            if (source == null)
            {
                yield break;
            }
            int index = 0;

            foreach (var item in source)
            {
                try
                {
                    PropertyPath pathToAdd;
                    if (rootInstance != null)
                    {
                        PropertyPath rootPath = ObjectPaths.Get(rootInstance);
                        pathToAdd = new PropertyPath
                        {
                            TypePrefix = rootPath.TypePrefix,
                            TypeSuffix = rootPath.TypeSuffix + "." + path.TypeSuffix + ":" + index
                        };
                    }
                    else
                    {
                        pathToAdd = new PropertyPath
                        {
                            TypePrefix = path.TypePrefix,
                            TypeSuffix = path.TypeSuffix + ":" + index
                        };
                    }
                    ObjectPaths.Add(item, pathToAdd);
                    yield return(selector(item));
                }
                finally
                {
                    ObjectPaths.Remove(item);
                    index++;
                }
            }
        }