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
- Exceptions.cs
- MyContact.cs
- ImageSourceValueSerializer.cs
- HotSpotCollection.cs
- InvalidPropValue.cs
- parserscommon.cs
- OutOfProcStateClientManager.cs
- ObjectHelper.cs
- XmlTextReaderImplHelpers.cs
- ViewSimplifier.cs
- DashStyle.cs
- WebConfigurationFileMap.cs
- AssemblyUtil.cs
- WebBrowserUriTypeConverter.cs
- RootBuilder.cs
- TdsParserHelperClasses.cs
- BaseCollection.cs
- EntityDataSourceUtil.cs
- ClientFormsIdentity.cs
- SignedPkcs7.cs
- ExtentKey.cs
- Freezable.cs
- SingleQueryOperator.cs
- ThreadAttributes.cs
- DataGridSortCommandEventArgs.cs
- ActivityExecutorSurrogate.cs
- GenericAuthenticationEventArgs.cs
- NavigationExpr.cs
- LambdaCompiler.Binary.cs
- DashStyle.cs
- ServiceDocument.cs
- AlphaSortedEnumConverter.cs
- ObjectComplexPropertyMapping.cs
- regiisutil.cs
- TcpDuplicateContext.cs
- RoutedUICommand.cs
- TraceUtils.cs
- SizeAnimationUsingKeyFrames.cs
- MsmqIntegrationSecurityElement.cs
- XhtmlMobileTextWriter.cs
- SqlException.cs
- PointLight.cs
- CompilationRelaxations.cs
- TemplatedWizardStep.cs
- ReadOnlyDataSourceView.cs
- SqlTypesSchemaImporter.cs
- TransformationRules.cs
- ObjectDataSourceEventArgs.cs
- DockingAttribute.cs
- GenericEnumConverter.cs
- cookiecontainer.cs
- ExpandedWrapper.cs
- UnsafeNativeMethods.cs
- ProgressBar.cs
- SqlMethodCallConverter.cs
- ManualResetEventSlim.cs
- Fx.cs
- DesignerActionPanel.cs
- Screen.cs
- DrawingContextWalker.cs
- KeyFrames.cs
- SQLRoleProvider.cs
- ProxyBuilder.cs
- Int32KeyFrameCollection.cs
- WebPartVerbsEventArgs.cs
- ActivationServices.cs
- RuntimeArgumentHandle.cs
- UnsignedPublishLicense.cs
- UrlParameterReader.cs
- ItemPager.cs
- KeyValueConfigurationCollection.cs
- FlowPanelDesigner.cs
- MenuItemBinding.cs
- SqlStream.cs
- SoapAttributes.cs
- OleStrCAMarshaler.cs
- PeerApplicationLaunchInfo.cs
- InstanceLockTracking.cs
- CodeThrowExceptionStatement.cs
- HitTestFilterBehavior.cs
- DataGridTable.cs
- XmlDataDocument.cs
- ColorTransformHelper.cs
- ExtendedTransformFactory.cs
- HttpCacheParams.cs
- UIElementAutomationPeer.cs
- QueryableDataSourceEditData.cs
- XmlSiteMapProvider.cs
- DrawingBrush.cs
- String.cs
- PropertyTabChangedEvent.cs
- SmtpReplyReaderFactory.cs
- ServiceDeploymentInfo.cs
- GridViewItemAutomationPeer.cs
- HttpConfigurationSystem.cs
- CultureMapper.cs
- WebBrowserPermission.cs
- ConditionBrowserDialog.cs
- PersonalizationStateInfo.cs
- MethodToken.cs