Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / xsp / System / Web / Compilation / AppSettingsExpressionBuilder.cs / 1 / AppSettingsExpressionBuilder.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Compilation { using System; using System.Security.Permissions; using System.CodeDom; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.Web.UI; [ExpressionPrefix("AppSettings")] [ExpressionEditor("System.Web.UI.Design.AppSettingsExpressionEditor, " + AssemblyRef.SystemDesign)] [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class AppSettingsExpressionBuilder : ExpressionBuilder { public override bool SupportsEvaluate { get { return true; } } public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { if (entry.DeclaringType == null || entry.PropertyInfo == null) { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim())); } else { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name)); } } public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { return GetAppSetting(entry.Expression, target.GetType(), entry.PropertyInfo.Name); } public static object GetAppSetting(string key) { string value = ConfigurationManager.AppSettings[key]; if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } public static object GetAppSetting(string key, Type targetType, string propertyName) { string value = ConfigurationManager.AppSettings[key]; if (targetType != null) { PropertyDescriptor propDesc = TypeDescriptor.GetProperties(targetType)[propertyName]; if (propDesc != null) { if (propDesc.PropertyType != typeof(string)) { TypeConverter converter = propDesc.Converter; if (converter.CanConvertFrom(typeof(string))) { return converter.ConvertFrom(value); } else { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_convertible, value, propDesc.PropertyType.Name, propDesc.Name)); } } } } if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Compilation { using System; using System.Security.Permissions; using System.CodeDom; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.Web.UI; [ExpressionPrefix("AppSettings")] [ExpressionEditor("System.Web.UI.Design.AppSettingsExpressionEditor, " + AssemblyRef.SystemDesign)] [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class AppSettingsExpressionBuilder : ExpressionBuilder { public override bool SupportsEvaluate { get { return true; } } public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { if (entry.DeclaringType == null || entry.PropertyInfo == null) { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim())); } else { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name)); } } public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { return GetAppSetting(entry.Expression, target.GetType(), entry.PropertyInfo.Name); } public static object GetAppSetting(string key) { string value = ConfigurationManager.AppSettings[key]; if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } public static object GetAppSetting(string key, Type targetType, string propertyName) { string value = ConfigurationManager.AppSettings[key]; if (targetType != null) { PropertyDescriptor propDesc = TypeDescriptor.GetProperties(targetType)[propertyName]; if (propDesc != null) { if (propDesc.PropertyType != typeof(string)) { TypeConverter converter = propDesc.Converter; if (converter.CanConvertFrom(typeof(string))) { return converter.ConvertFrom(value); } else { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_convertible, value, propDesc.PropertyType.Name, propDesc.Name)); } } } } if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XPathQilFactory.cs
- Section.cs
- PlatformNotSupportedException.cs
- LocalizableAttribute.cs
- SystemWebSectionGroup.cs
- AsymmetricSignatureFormatter.cs
- MobileControlDesigner.cs
- DrawingDrawingContext.cs
- IItemContainerGenerator.cs
- ComMethodElementCollection.cs
- SafePointer.cs
- TextEmbeddedObject.cs
- StatusBarAutomationPeer.cs
- BindingExpressionUncommonField.cs
- wgx_render.cs
- PropertyEmitterBase.cs
- ImageAutomationPeer.cs
- Floater.cs
- SessionPageStateSection.cs
- Inline.cs
- Hex.cs
- ViewManager.cs
- LocalizableAttribute.cs
- DesignerVerbCollection.cs
- ServiceAuthorizationBehavior.cs
- DataSourceBooleanViewSchemaConverter.cs
- LogicalExpr.cs
- Resources.Designer.cs
- RotateTransform.cs
- CodeCatchClause.cs
- XmlNavigatorStack.cs
- __Filters.cs
- LinkedResourceCollection.cs
- TemplateBamlTreeBuilder.cs
- PathTooLongException.cs
- BreadCrumbTextConverter.cs
- CompareInfo.cs
- HttpHandlersSection.cs
- RouteUrlExpressionBuilder.cs
- OleDbParameter.cs
- SqlProcedureAttribute.cs
- LicFileLicenseProvider.cs
- DATA_BLOB.cs
- InfoCardRequestException.cs
- SmiConnection.cs
- SimpleApplicationHost.cs
- OperationContractGenerationContext.cs
- WithParamAction.cs
- TextChange.cs
- DispatcherHookEventArgs.cs
- BuildProviderUtils.cs
- LayoutUtils.cs
- XmlSerializerNamespaces.cs
- Rotation3DAnimation.cs
- ManagedCodeMarkers.cs
- URL.cs
- HandlerBase.cs
- BoolExpr.cs
- SizeLimitedCache.cs
- SelectionEditingBehavior.cs
- ExeConfigurationFileMap.cs
- DesigntimeLicenseContextSerializer.cs
- BindToObject.cs
- SaveFileDialog.cs
- SafeLocalMemHandle.cs
- DesignerOptionService.cs
- ScriptManagerProxy.cs
- Collection.cs
- XmlDownloadManager.cs
- UserInitiatedRoutedEventPermissionAttribute.cs
- BinHexEncoding.cs
- ColorConverter.cs
- LOSFormatter.cs
- ToolStripCustomTypeDescriptor.cs
- DataGridViewRowPostPaintEventArgs.cs
- DataGridPageChangedEventArgs.cs
- PanelDesigner.cs
- MiniAssembly.cs
- AsymmetricKeyExchangeFormatter.cs
- ListCollectionView.cs
- Lease.cs
- ConfigurationProperty.cs
- ColorMap.cs
- DataTemplate.cs
- URLAttribute.cs
- ServiceInfo.cs
- ColorMap.cs
- HMACSHA1.cs
- ToolBarButtonClickEvent.cs
- FixUp.cs
- CopyAttributesAction.cs
- TextRangeEditTables.cs
- TypeNameConverter.cs
- FtpRequestCacheValidator.cs
- PersonalizationState.cs
- TextEditorCopyPaste.cs
- ConfigXmlCDataSection.cs
- __ComObject.cs
- ImageKeyConverter.cs
- ExpandableObjectConverter.cs