public void Deny_Unrestricted () { TemplateBuilder tb = new TemplateBuilder (); tb.Text = "mono"; Assert.AreEqual ("mono", tb.Text, "Text"); tb.InstantiateIn (new Control ()); tb.Init (new PageParser (), new ControlBuilder (), null, null, null, null); Assert.IsFalse (tb.NeedsTagInnerText (), "NeedsTagInnerText"); tb.SetTagInnerText ("mono"); Assert.IsNotNull (tb.BuildObject (), "BuildObject"); }
/// <include file='doc\ControlBuilder.uex' path='docs/doc[@for="ControlBuilder.AppendSubBuilder"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public virtual void AppendSubBuilder(ControlBuilder subBuilder) { // Tell the sub builder that it's about to be appended to its parent subBuilder.OnAppendToParentBuilder(this); if (FChildrenAsProperties) { // Don't allow code blocks when properties are expected (ASURT 97838) if (subBuilder is CodeBlockBuilder) { throw new HttpException( HttpRuntime.FormatResourceString(SR.Code_not_supported_on_not_controls)); } // If there is a default property, delegate to its builder if (_defaultPropBuilder != null) { _defaultPropBuilder.AppendSubBuilder(subBuilder); return; } // The tagname is the property name string propName = subBuilder.TagName; if (subBuilder is TemplateBuilder) // The subBuilder is for building a template { TemplateBuilder tplBuilder = (TemplateBuilder)subBuilder; if (_templateSetter == null) { _templateSetter = new PropertySetter(_ctrlType, InDesigner || NonCompiledPage); } // Add TemplateBuilder to the template setter. _templateSetter.AddTemplateProperty(propName, tplBuilder); return; } if (subBuilder is CollectionBuilder) // The subBuilder is for building a collection // If there are no items in the collection, we're done { if (subBuilder.SubBuilders == null || subBuilder.SubBuilders.Count == 0) { return; } IEnumerator subBuilders = subBuilder.SubBuilders.GetEnumerator(); while (subBuilders.MoveNext()) { ControlBuilder builder = (ControlBuilder)subBuilders.Current; subBuilder.ComplexAttribSetter.AddCollectionItem(builder); } subBuilder.SubBuilders.Clear(); ComplexAttribSetter.AddComplexProperty(propName, subBuilder); return; } ComplexAttribSetter.AddComplexProperty(propName, subBuilder); return; } CodeBlockBuilder codeBlockBuilder = subBuilder as CodeBlockBuilder; if (codeBlockBuilder != null) { // Don't allow code blocks inside non-control tags (ASURT 76719) if (ControlType != null && !typeof(Control).IsAssignableFrom(ControlType)) { throw new HttpException( HttpRuntime.FormatResourceString(SR.Code_not_supported_on_not_controls)); } // Is it a databinding expression? <%# ... %> if (codeBlockBuilder.BlockType == CodeBlockType.DataBinding) { if (InDesigner) { // In the designer, don't use the fancy multipart DataBoundLiteralControl, // which breaks a number of things (ASURT 82925,86738). Instead, use the // simpler DesignerDataBoundLiteralControl, and do standard databinding // on its Text property. IDictionary attribs = new SortedList(); attribs.Add("Text", "<%#" + codeBlockBuilder.Content + "%>"); subBuilder = CreateBuilderFromType( Parser, this, typeof(DesignerDataBoundLiteralControl), null, null, attribs, codeBlockBuilder.Line, codeBlockBuilder.SourceFileName); } else { // Get the last builder, and check if it's a DataBoundLiteralControlBuilder object lastBuilder = GetLastBuilder(); DataBoundLiteralControlBuilder dataBoundBuilder = lastBuilder as DataBoundLiteralControlBuilder; // If not, then we need to create one. Otherwise, just append to the // existing one bool fNewDataBoundLiteralControl = false; if (dataBoundBuilder == null) { dataBoundBuilder = new DataBoundLiteralControlBuilder(); dataBoundBuilder.Init(_parser, this, typeof(DataBoundLiteralControl), null, null, null); fNewDataBoundLiteralControl = true; // If the previous builder was a string, add it as the first // entry in the composite control. string s = lastBuilder as string; if (s != null) { _subBuilders.RemoveAt(_subBuilders.Count - 1); dataBoundBuilder.AddLiteralString(s); } } dataBoundBuilder.AddDataBindingExpression(codeBlockBuilder); if (!fNewDataBoundLiteralControl) { return; } subBuilder = dataBoundBuilder; } } else { // Set a flag if there is at least one block of ASP code _fHasAspCode = true; } } if (FIsNonParserAccessor) { throw new HttpException( HttpRuntime.FormatResourceString(SR.Children_not_supported_on_not_controls)); } AddSubBuilder(subBuilder); }
string CreateExtractValuesMethod (TemplateBuilder builder) { CodeMemberMethod method = new CodeMemberMethod (); method.Name = "__ExtractValues_" + builder.ID; method.Attributes = MemberAttributes.Private | MemberAttributes.Final; method.ReturnType = new CodeTypeReference (typeof(IOrderedDictionary)); CodeParameterDeclarationExpression arg = new CodeParameterDeclarationExpression (); arg.Type = new CodeTypeReference (typeof (Control)); arg.Name = "__container"; method.Parameters.Add (arg); mainClass.Members.Add (method); CodeObjectCreateExpression newTable = new CodeObjectCreateExpression (); newTable.CreateType = new CodeTypeReference (typeof(OrderedDictionary)); method.Statements.Add (new CodeVariableDeclarationStatement (typeof(OrderedDictionary), "__table", newTable)); CodeVariableReferenceExpression tableExp = new CodeVariableReferenceExpression ("__table"); if (builder.Bindings != null) { Hashtable hash = new Hashtable (); foreach (TemplateBinding binding in builder.Bindings) { CodeConditionStatement sif; CodeVariableReferenceExpression control; CodeAssignStatement assign; if (hash [binding.ControlId] == null) { CodeVariableDeclarationStatement dec = new CodeVariableDeclarationStatement (binding.ControlType, binding.ControlId); method.Statements.Add (dec); CodeVariableReferenceExpression cter = new CodeVariableReferenceExpression ("__container"); CodeMethodInvokeExpression invoke = new CodeMethodInvokeExpression (cter, "FindControl"); invoke.Parameters.Add (new CodePrimitiveExpression (binding.ControlId)); assign = new CodeAssignStatement (); control = new CodeVariableReferenceExpression (binding.ControlId); assign.Left = control; assign.Right = new CodeCastExpression (binding.ControlType, invoke); method.Statements.Add (assign); sif = new CodeConditionStatement (); sif.Condition = new CodeBinaryOperatorExpression (control, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression (null)); method.Statements.Add (sif); hash [binding.ControlId] = sif; } sif = (CodeConditionStatement) hash [binding.ControlId]; control = new CodeVariableReferenceExpression (binding.ControlId); assign = new CodeAssignStatement (); assign.Left = new CodeIndexerExpression (tableExp, new CodePrimitiveExpression (binding.FieldName)); assign.Right = new CodePropertyReferenceExpression (control, binding.ControlProperty); sif.TrueStatements.Add (assign); } } method.Statements.Add (new CodeMethodReturnStatement (tableExp)); return method.Name; }
ControlBuilder CreatePropertyBuilder (string propName, TemplateParser parser, IDictionary atts) { int idx; string propertyName; if ((idx = propName.IndexOf (':')) >= 0) propertyName = propName.Substring (idx + 1); else propertyName = propName; PropertyInfo prop = type.GetProperty (propertyName, FlagsNoCase); if (prop == null) { string msg = String.Format ("Property {0} not found in type {1}", propertyName, type); throw new HttpException (msg); } Type propType = prop.PropertyType; ControlBuilder builder = null; if (typeof (ICollection).IsAssignableFrom (propType)) { builder = new CollectionBuilder (); } else if (typeof (ITemplate).IsAssignableFrom (propType)) { builder = new TemplateBuilder (prop); } else if (typeof (string) == propType) { builder = new StringPropertyBuilder (prop.Name); } else { builder = CreateBuilderFromType (parser, parentBuilder, propType, prop.Name, null, atts, line, fileName); builder.isProperty = true; if (idx >= 0) builder.originalTagName = propName; return builder; } builder.Init (parser, this, null, prop.Name, null, atts); builder.fileName = fileName; builder.line = line; builder.isProperty = true; if (idx >= 0) builder.originalTagName = propName; return builder; }
private ControlBuilder GetChildPropertyBuilder(string tagName, IDictionary attribs, ref Type childType, TemplateParser templateParser, bool defaultProperty) { Debug.Assert(FChildrenAsProperties, "ChildrenAsProperties"); // Parse the device filter if any // The child is supposed to be a property, so look for it PropertyInfo pInfo = TargetFrameworkUtil.GetProperty(_controlType, tagName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.IgnoreCase); if (pInfo == null) { throw new HttpException(SR.GetString(SR.Type_doesnt_have_property, _controlType.FullName, tagName)); } // Get its type childType = pInfo.PropertyType; ControlBuilder builder = null; // If it's a collection, return the collection builder if (typeof(ICollection).IsAssignableFrom(childType)) { // Check whether the prop has an IgnoreUnknownContentAttribute IgnoreUnknownContentAttribute attr = (IgnoreUnknownContentAttribute)Attribute.GetCustomAttribute(pInfo, typeof(IgnoreUnknownContentAttribute), true); builder = new CollectionBuilder(attr != null /*ignoreUnknownContent*/); } else if (typeof(ITemplate).IsAssignableFrom(childType)) { bool useBindableTemplate = false; bool allowMultipleInstances = true; object[] containerAttrs = pInfo.GetCustomAttributes(typeof(TemplateContainerAttribute), /*inherits*/ false); if ((containerAttrs != null) && (containerAttrs.Length > 0)) { Debug.Assert(containerAttrs[0] is TemplateContainerAttribute); useBindableTemplate = (((TemplateContainerAttribute)containerAttrs[0]).BindingDirection == BindingDirection.TwoWay); } allowMultipleInstances = Util.IsMultiInstanceTemplateProperty(pInfo); if (useBindableTemplate) { // If it's a bindable template, return the bindable template builder builder = new BindableTemplateBuilder(); } else { // If it's a template, return the template builder builder = new TemplateBuilder(); } if (builder is TemplateBuilder) { ((TemplateBuilder)builder).AllowMultipleInstances = allowMultipleInstances; // If we're in the designer, set a reference to the designer host // so we can get to a filter resolution service later if (InDesigner) { ((TemplateBuilder)builder).SetDesignerHost(templateParser.DesignerHost); } } } else if (childType == typeof(string)) { PersistenceModeAttribute persistenceAttr = (PersistenceModeAttribute)Attribute.GetCustomAttribute(pInfo, typeof(PersistenceModeAttribute), true); if (((persistenceAttr == null) || (persistenceAttr.Mode == PersistenceMode.Attribute)) && !defaultProperty) { // If the property is supposed to be declared as an attribute on a control tag, throw if it was declared as an inner property // We don't throw if we are simply building the DefaultPropertyBuilder. throw new HttpException(SR.GetString(SR.ControlBuilder_CannotHaveComplexString, _controlType.FullName, tagName)); } builder = new StringPropertyBuilder(); } if (builder != null) { builder.Line = Line; builder.VirtualPath = VirtualPath; // Initialize the builder builder.Init(Parser, (ControlBuilder)this, null, tagName, null, attribs); return builder; } // Otherwise, simply return the builder for the property builder = CreateBuilderFromType(Parser, this, childType, tagName, null, attribs, Line, PageVirtualPath); return builder; }
/// <devdoc> /// </devdoc> private void AddTemplateProperty(string filter, string name, TemplateBuilder builder) { /* Do not ignore template properties since we do want to generate IDs for controls defined inside SingleInstanceTemplates. VSWhidbey 243341 if (IgnoreControlProperty) { return; } */ Debug.Assert(!String.IsNullOrEmpty(name)); Debug.Assert(builder != null); // Look for a MemberInfo string objectModelName = String.Empty; MemberInfo memberInfo = PropertyMapper.GetMemberInfo(_controlType, name, out objectModelName); // Setup a template entry bool bindableTemplate = builder is BindableTemplateBuilder; TemplatePropertyEntry entry = new TemplatePropertyEntry(bindableTemplate); entry.Builder = builder; entry.Filter = filter; entry.Name = objectModelName; Type memberType = null; if (memberInfo != null) { if (memberInfo is PropertyInfo) { PropertyInfo propInfo = ((PropertyInfo)memberInfo); entry.PropertyInfo = propInfo; ValidatePersistable(propInfo, false, false, false, filter); // Check the attribute on the property to see if it has a ContainerType TemplateContainerAttribute templateAttrib = (TemplateContainerAttribute)Attribute.GetCustomAttribute(propInfo, typeof(TemplateContainerAttribute), false); if (templateAttrib != null) { if (!typeof(INamingContainer).IsAssignableFrom(templateAttrib.ContainerType)) { throw new HttpException(SR.GetString(SR.Invalid_template_container, name, templateAttrib.ContainerType.FullName)); } // If it had one, make sure the builder knows what type it is builder.SetControlType(templateAttrib.ContainerType); } entry.Type = propInfo.PropertyType; } else { Debug.Assert(memberInfo is FieldInfo); memberType = ((FieldInfo)memberInfo).FieldType; } entry.Type = memberType; } else { throw new HttpException(SR.GetString(SR.Type_doesnt_have_property, _controlType.FullName, name)); } // Add it to the template entries AddEntry(TemplatePropertyEntriesInternal, entry); }
private ControlBuilder GetChildPropertyBuilder(string tagName, IDictionary attribs, ref Type childType, TemplateParser templateParser, bool defaultProperty) { PropertyInfo element = TargetFrameworkUtil.GetProperty(this._controlType, tagName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase); if (element == null) { throw new HttpException(System.Web.SR.GetString("Type_doesnt_have_property", new object[] { this._controlType.FullName, tagName })); } childType = element.PropertyType; ControlBuilder builder = null; if (typeof(ICollection).IsAssignableFrom(childType)) { IgnoreUnknownContentAttribute attribute = (IgnoreUnknownContentAttribute) Attribute.GetCustomAttribute(element, typeof(IgnoreUnknownContentAttribute), true); builder = new CollectionBuilder(attribute != null); } else if (typeof(ITemplate).IsAssignableFrom(childType)) { bool flag = false; bool flag2 = true; object[] customAttributes = element.GetCustomAttributes(typeof(TemplateContainerAttribute), false); if ((customAttributes != null) && (customAttributes.Length > 0)) { flag = ((TemplateContainerAttribute) customAttributes[0]).BindingDirection == BindingDirection.TwoWay; } flag2 = System.Web.UI.Util.IsMultiInstanceTemplateProperty(element); if (flag) { builder = new BindableTemplateBuilder(); } else { builder = new TemplateBuilder(); } if (builder is TemplateBuilder) { ((TemplateBuilder) builder).AllowMultipleInstances = flag2; if (this.InDesigner) { ((TemplateBuilder) builder).SetDesignerHost(templateParser.DesignerHost); } } } else if (childType == typeof(string)) { PersistenceModeAttribute attribute2 = (PersistenceModeAttribute) Attribute.GetCustomAttribute(element, typeof(PersistenceModeAttribute), true); if (((attribute2 == null) || (attribute2.Mode == PersistenceMode.Attribute)) && !defaultProperty) { throw new HttpException(System.Web.SR.GetString("ControlBuilder_CannotHaveComplexString", new object[] { this._controlType.FullName, tagName })); } builder = new StringPropertyBuilder(); } if (builder != null) { builder.Line = this.Line; builder.VirtualPath = this.VirtualPath; builder.Init(this.Parser, this, null, tagName, null, attribs); return builder; } return CreateBuilderFromType(this.Parser, this, childType, tagName, null, attribs, this.Line, this.PageVirtualPath); }
private void AddTemplateProperty(string filter, string name, TemplateBuilder builder) { string nameForCodeGen = string.Empty; MemberInfo info = PropertyMapper.GetMemberInfo(this._controlType, name, out nameForCodeGen); bool bindableTemplate = builder is BindableTemplateBuilder; TemplatePropertyEntry entry = new TemplatePropertyEntry(bindableTemplate) { Builder = builder, Filter = filter, Name = nameForCodeGen }; Type fieldType = null; if (info == null) { throw new HttpException(System.Web.SR.GetString("Type_doesnt_have_property", new object[] { this._controlType.FullName, name })); } if (info is PropertyInfo) { PropertyInfo propInfo = (PropertyInfo) info; entry.PropertyInfo = propInfo; this.ValidatePersistable(propInfo, false, false, false, filter); TemplateContainerAttribute attribute = (TemplateContainerAttribute) Attribute.GetCustomAttribute(propInfo, typeof(TemplateContainerAttribute), false); if (attribute != null) { if (!typeof(INamingContainer).IsAssignableFrom(attribute.ContainerType)) { throw new HttpException(System.Web.SR.GetString("Invalid_template_container", new object[] { name, attribute.ContainerType.FullName })); } builder.SetControlType(attribute.ContainerType); } entry.Type = propInfo.PropertyType; } else { fieldType = ((FieldInfo) info).FieldType; } entry.Type = fieldType; this.AddEntry(this.TemplatePropertyEntriesInternal, entry); }
ControlBuilder CreatePropertyBuilder(string propName, TemplateParser parser, IDictionary atts) { int idx; string propertyName; if ((idx = propName.IndexOf(':')) >= 0) { propertyName = propName.Substring(idx + 1); } else { propertyName = propName; } PropertyInfo prop = type.GetProperty(propertyName, FlagsNoCase); if (prop == null) { string msg = String.Format("Property {0} not found in type {1}", propertyName, type); throw new HttpException(msg); } Type propType = prop.PropertyType; ControlBuilder builder = null; if (typeof(ICollection).IsAssignableFrom(propType)) { builder = new CollectionBuilder(); } else if (typeof(ITemplate).IsAssignableFrom(propType)) { builder = new TemplateBuilder(prop); } else if (typeof(string) == propType) { builder = new StringPropertyBuilder(prop.Name); } else { builder = CreateBuilderFromType(parser, parentBuilder, propType, prop.Name, null, atts, line, fileName); builder.isProperty = true; builder.isPropertyWritable = prop.CanWrite; if (idx >= 0) { builder.originalTagName = propName; } return(builder); } builder.Init(parser, this, null, prop.Name, null, atts); builder.fileName = fileName; builder.line = line; builder.isProperty = true; builder.isPropertyWritable = prop.CanWrite; if (idx >= 0) { builder.originalTagName = propName; } return(builder); }
/* * Add a template property to the setter */ internal void AddTemplateProperty(string name, TemplateBuilder builder) { AddPropertyInternal(name, null /*value*/, builder, false /*fItemProp*/); }