Ejemplo n.º 1
0
        public TemplateElement GetElement(TemplateFragment fragment)
        {
            if (fragment == null)
            {
                throw new ArgumentNullException("fragment");
            }

            TemplateFragmentPlainText text = fragment as TemplateFragmentPlainText;

            if (text != null)
            {
                return(new TemplateElementText(text.Data));
            }

            TemplateElement       ret   = null;
            TemplateFragmentMacro macro = fragment as TemplateFragmentMacro;

            if (macro != null)
            {
                ret = ProcessMacro(macro);
            }

            if (ret == null)
            {
                throw new InvalidOperationException(
                          String.Format("Failed to generate a template element from fragment {0} at {1}:{2},{3}",
                                        fragment, fragment.InFile, fragment.LineStart, fragment.ColumnStart)
                          );
            }

            return(ret);
        }
        void ResolveProperty()
        {
            Type         t  = typeof(TData);
            PropertyInfo pi = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);

            if (pi == null)
            {
                throw new InvalidOperationException(String.Format("Public instance property '{0}' does not exist in type '{1}'", propertyName, t.FullName));
            }

            if (!pi.CanRead)
            {
                throw new InvalidOperationException(String.Format("Property '{0}.{1}' is not readable.", t.FullName, propertyName));
            }

            property = pi;
            Type pt = property.PropertyType;

            if (pt != typeof(string) && pt.IsGenericType)
            {
                Type[] interfaces = pt.GetInterfaces();
                bool   done       = false;
                foreach (Type ct in collectionTypes)
                {
                    foreach (Type iface in interfaces)
                    {
                        if (ct.IsAssignableFrom(iface) || (iface.IsGenericType && ct.IsAssignableFrom(iface.GetGenericTypeDefinition())))
                        {
                            IsCollection = done = true;
                            break;
                        }
                    }

                    if (done)
                    {
                        break;
                    }
                }
            }
            if (String.IsNullOrEmpty(subPropertyName))
            {
                return;
            }

            Type myself = typeof(TemplateElementPropertyReference <>);

            myself      = myself.MakeGenericType(property.PropertyType);
            subProperty = Activator.CreateInstance(myself, new object[] { subPropertyName }) as TemplateElement;
        }