2 // RouteValueExpressionBuilder.cs
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2010 Novell, Inc (http://novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.ComponentModel;
34 using System.Reflection;
36 using System.Web.Routing;
38 namespace System.Web.Compilation
40 [ExpressionEditor ("System.Web.UI.Design.RouteValueExpressionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
41 [ExpressionPrefix ("Routes")]
42 public class RouteValueExpressionBuilder : ExpressionBuilder
44 public override bool SupportsEvaluate { get { return true; } }
46 public RouteValueExpressionBuilder ()
50 // This method is used only from within pages that aren't compiled
51 public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
53 // Mono doesn't use this, so let's leave it like that for now
54 throw new NotImplementedException ();
57 public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
60 throw new NullReferenceException (".NET emulation (entry == null)");
62 var ret = new CodeMethodInvokeExpression ();
63 ret.Method = new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (typeof (RouteValueExpressionBuilder)), "GetRouteValue");
65 var thisref = new CodeThisReferenceExpression ();
66 CodeExpressionCollection parameters = ret.Parameters;
67 parameters.Add (new CodePropertyReferenceExpression (thisref, "Page"));
68 parameters.Add (new CodePrimitiveExpression (entry.Expression));
69 parameters.Add (new CodeTypeOfExpression (new CodeTypeReference (entry.DeclaringType)));
70 parameters.Add (new CodePrimitiveExpression (entry.Name));
75 public static object GetRouteValue (Page page, string key, Type controlType, string propertyName)
77 RouteData rd = page != null ? page.RouteData : null;
78 if (rd == null || String.IsNullOrEmpty (key))
81 object value = rd.Values [key];
85 if (controlType == null || String.IsNullOrEmpty (propertyName) || !(value is string))
88 PropertyDescriptorCollection pcoll = TypeDescriptor.GetProperties (controlType);
89 if (pcoll == null || pcoll.Count == 0)
92 PropertyDescriptor pdesc = pcoll [propertyName];
96 TypeConverter cvt = pdesc.Converter;
97 if (cvt == null || !cvt.CanConvertFrom (typeof (string)))
100 return cvt.ConvertFrom (value);