1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
//------------------------------------------------------------------------------
// <copyright file="DataBinder.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Web.Util;
/// <devdoc>
/// <para> Provides design-time support for RAD designers to
/// generate and parse <see topic='cpconDatabindingExpressionSyntax'/> . This class cannot be inherited.</para>
/// </devdoc>
public sealed class DataBinder {
private static readonly char[] expressionPartSeparator = new char[] { '.' };
private static readonly char[] indexExprStartChars = new char[] { '[', '(' };
private static readonly char[] indexExprEndChars = new char[] { ']', ')' };
private static readonly ConcurrentDictionary<Type, PropertyDescriptorCollection> propertyCache = new ConcurrentDictionary<Type, PropertyDescriptorCollection>();
// By default the new caching behavior will be on.
private static bool enableCaching = true;
// Global overrride switch to enable old behavior.
public static bool EnableCaching {
get {
return enableCaching;
}
set {
enableCaching = value;
if (!value) {
// Clear the cache when caching is turned off.
propertyCache.Clear();
}
}
}
/// <internalonly/>
//
public DataBinder() {
}
/// <devdoc>
/// <para>Evaluates data binding expressions at runtime. While
/// this method is automatically called when you create data bindings in a RAD
/// designer, you can also use it declaratively if you want to simplify the casting
/// to a text string to be displayed on a browser. To do so, you must place the
/// <%# and %> tags, which are also used in standard ASP.NET data binding, around the data binding expression.</para>
/// <para>This method is particularly useful when data binding against controls that
/// are in a templated list.</para>
/// <note type="caution">
/// Since this method is called at runtime, it can cause performance
/// to noticeably slow compared to standard ASP.NET databinding syntax.
/// Use this method judiciously.
/// </note>
/// </devdoc>
public static object Eval(object container, string expression) {
if (expression == null) {
throw new ArgumentNullException("expression");
}
expression = expression.Trim();
if (expression.Length == 0) {
throw new ArgumentNullException("expression");
}
if (container == null) {
return null;
}
string[] expressionParts = expression.Split(expressionPartSeparator);
return DataBinder.Eval(container, expressionParts);
}
/// <devdoc>
/// </devdoc>
private static object Eval(object container, string[] expressionParts) {
Debug.Assert((expressionParts != null) && (expressionParts.Length != 0),
"invalid expressionParts parameter");
object prop;
int i;
for (prop = container, i = 0; (i < expressionParts.Length) && (prop != null); i++) {
string expr = expressionParts[i];
bool indexedExpr = expr.IndexOfAny(indexExprStartChars) >= 0;
if (indexedExpr == false) {
prop = DataBinder.GetPropertyValue(prop, expr);
}
else {
prop = DataBinder.GetIndexedPropertyValue(prop, expr);
}
}
return prop;
}
/// <devdoc>
/// <para> Evaluates data binding expressions at runtime and
/// formats the output as text to be displayed in the requesting browser. While this
/// method is automatically called when you create data bindings in a RAD designer,
/// you can also use it declaratively if you want to simplify the casting to a text
/// string to be displayed on a browser. To do so, you must place the <%# and %> tags, which are also used in standard ASP.NET data binding, around
/// the data binding expression.</para>
/// <para>This method is particularly useful when data binding against controls that
/// are in a templated list.</para>
/// <note type="caution">
/// Since this method is called at
/// runtime, it can cause performance to noticeably slow compared to standard ASP.NET
/// databinding syntax. Use this method judiciously, particularly when string
/// formatting is not required.
/// </note>
/// </devdoc>
public static string Eval(object container, string expression, string format) {
object value = DataBinder.Eval(container, expression);
if ((value == null) || (value == System.DBNull.Value)) {
return String.Empty;
}
else {
if (String.IsNullOrEmpty(format)) {
return value.ToString();
}
else {
return String.Format(format, value);
}
}
}
internal static PropertyDescriptorCollection GetPropertiesFromCache(object container) {
// We don't cache if the object implements ICustomTypeDescriptor.
if (EnableCaching && !(container is ICustomTypeDescriptor)) {
PropertyDescriptorCollection properties = null;
Type containerType = container.GetType();
if (!propertyCache.TryGetValue(containerType, out properties)) {
properties = TypeDescriptor.GetProperties(containerType);
propertyCache.TryAdd(containerType, properties);
}
return properties;
}
return TypeDescriptor.GetProperties(container);
}
/// <devdoc>
/// </devdoc>
public static object GetPropertyValue(object container, string propName) {
if (container == null) {
throw new ArgumentNullException("container");
}
if (String.IsNullOrEmpty(propName)) {
throw new ArgumentNullException("propName");
}
object prop = null;
// get a PropertyDescriptor using case-insensitive lookup
PropertyDescriptor pd = GetPropertiesFromCache(container).Find(propName, true);
if (pd != null) {
prop = pd.GetValue(container);
}
else {
throw new HttpException(SR.GetString(SR.DataBinder_Prop_Not_Found, container.GetType().FullName, propName));
}
return prop;
}
/// <devdoc>
/// </devdoc>
public static string GetPropertyValue(object container, string propName, string format) {
object value = DataBinder.GetPropertyValue(container, propName);
if ((value == null) || (value == System.DBNull.Value)) {
return string.Empty;
}
else {
if (String.IsNullOrEmpty(format)) {
return value.ToString();
}
else {
return string.Format(format, value);
}
}
}
/// <devdoc>
/// </devdoc>
public static object GetIndexedPropertyValue(object container, string expr) {
if (container == null) {
throw new ArgumentNullException("container");
}
if (String.IsNullOrEmpty(expr)) {
throw new ArgumentNullException("expr");
}
object prop = null;
bool intIndex = false;
int indexExprStart = expr.IndexOfAny(indexExprStartChars);
int indexExprEnd = expr.IndexOfAny(indexExprEndChars, indexExprStart + 1);
if ((indexExprStart < 0) || (indexExprEnd < 0) ||
(indexExprEnd == indexExprStart + 1)) {
throw new ArgumentException(SR.GetString(SR.DataBinder_Invalid_Indexed_Expr, expr));
}
string propName = null;
object indexValue = null;
string index = expr.Substring(indexExprStart + 1, indexExprEnd - indexExprStart - 1).Trim();
if (indexExprStart != 0)
propName = expr.Substring(0, indexExprStart);
if (index.Length != 0) {
if (((index[0] == '"') && (index[index.Length - 1] == '"')) ||
((index[0] == '\'') && (index[index.Length - 1] == '\''))) {
indexValue = index.Substring(1, index.Length - 2);
}
else {
if (Char.IsDigit(index[0])) {
// treat it as a number
int parsedIndex;
intIndex = Int32.TryParse(index, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedIndex);
if (intIndex) {
indexValue = parsedIndex;
}
else {
indexValue = index;
}
}
else {
// treat as a string
indexValue = index;
}
}
}
if (indexValue == null) {
throw new ArgumentException(SR.GetString(SR.DataBinder_Invalid_Indexed_Expr, expr));
}
object collectionProp = null;
if ((propName != null) && (propName.Length != 0)) {
collectionProp = DataBinder.GetPropertyValue(container, propName);
}
else {
collectionProp = container;
}
if (collectionProp != null) {
Array arrayProp = collectionProp as Array;
if (arrayProp != null && intIndex) {
prop = arrayProp.GetValue((int)indexValue);
}
else if ((collectionProp is IList) && intIndex) {
prop = ((IList)collectionProp)[(int)indexValue];
}
else {
PropertyInfo propInfo =
collectionProp.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { indexValue.GetType() }, null);
if (propInfo != null) {
prop = propInfo.GetValue(collectionProp, new object[] { indexValue });
}
else {
throw new ArgumentException(SR.GetString(SR.DataBinder_No_Indexed_Accessor, collectionProp.GetType().FullName));
}
}
}
return prop;
}
/// <devdoc>
/// </devdoc>
public static string GetIndexedPropertyValue(object container, string propName, string format) {
object value = DataBinder.GetIndexedPropertyValue(container, propName);
if ((value == null) || (value == System.DBNull.Value)) {
return String.Empty;
}
else {
if (String.IsNullOrEmpty(format)) {
return value.ToString();
}
else {
return string.Format(format, value);
}
}
}
/// <devdoc>
/// </devdoc>
public static object GetDataItem(object container) {
bool foundDataItem;
return DataBinder.GetDataItem(container, out foundDataItem);
}
/// <devdoc>
/// </devdoc>
public static object GetDataItem(object container, out bool foundDataItem) {
//
if (container == null) {
foundDataItem = false;
return null;
}
// If object implements IDataItemContainer, get value directly from interface
IDataItemContainer dataItemContainer = container as IDataItemContainer;
if (dataItemContainer != null) {
foundDataItem = true;
return dataItemContainer.DataItem;
}
// Otherwise, look for a property named "DataItem"
string dataItemPropertyName = "DataItem";
// Check whether the property exists
PropertyInfo propInfo = container.GetType().GetProperty(dataItemPropertyName,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
// If not, return null
//
if (propInfo == null) {
foundDataItem = false;
return null;
}
// If so, get the value
foundDataItem = true;
return propInfo.GetValue(container, null);
}
// Returns true for types that can be automatically databound in controls such as
// GridView and DetailsView. Bindable types are simple types, such as primitives, strings, enums
// and nullable primitives.
public static bool IsBindableType(Type type) {
//Note that for supporting AutoGenerateEnums property, our default Column Generators for GridView / DetailsView
//are not calling the DataBinder.IsBindableType but rather calling the below method.
//So any new types to be considered as Bindable types should be added in the below method rather than here directly.
return DataBoundControlHelper.IsBindableType(type, enableEnums: true);
}
/// <devdoc>
/// Returns true if the value is null, DBNull, or INullableValue.HasValue is false.
/// </devdoc>
internal static bool IsNull(object value) {
if (value == null || Convert.IsDBNull(value)) {
return true;
}
return false;
}
}
}
|