Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ConstNode.cs
- XmlSchemaSimpleTypeUnion.cs
- WebBrowserContainer.cs
- Errors.cs
- ToolBarOverflowPanel.cs
- ZipPackagePart.cs
- ToolStripSystemRenderer.cs
- LabelAutomationPeer.cs
- _NegoStream.cs
- PageRanges.cs
- DeclarationUpdate.cs
- SoapIgnoreAttribute.cs
- ProxyElement.cs
- TextEndOfLine.cs
- PolygonHotSpot.cs
- ImpersonationContext.cs
- BaseValidator.cs
- List.cs
- QueryProcessor.cs
- DictionaryContent.cs
- ProcessHostServerConfig.cs
- HostSecurityManager.cs
- PassportIdentity.cs
- Internal.cs
- TableCellAutomationPeer.cs
- Attributes.cs
- ClientFormsIdentity.cs
- wmiprovider.cs
- TreeIterator.cs
- SubclassTypeValidator.cs
- StreamingContext.cs
- Rotation3DAnimationBase.cs
- EventLogSession.cs
- MimeAnyImporter.cs
- SchemaTableColumn.cs
- DataSpaceManager.cs
- ActionMismatchAddressingException.cs
- UriTemplateDispatchFormatter.cs
- AesCryptoServiceProvider.cs
- Point3DCollectionConverter.cs
- NetSectionGroup.cs
- EDesignUtil.cs
- PersonalizationState.cs
- TerminateSequenceResponse.cs
- TextMetrics.cs
- XslAstAnalyzer.cs
- DataGridHeaderBorder.cs
- DataSourceNameHandler.cs
- COM2Properties.cs
- Bits.cs
- configsystem.cs
- ToolStripDropDownMenu.cs
- TextSelectionHelper.cs
- ObjectViewQueryResultData.cs
- cookie.cs
- XPathNodeIterator.cs
- QilChoice.cs
- ZoomPercentageConverter.cs
- BaseTemplateParser.cs
- SmtpAuthenticationManager.cs
- DataSource.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- MetadataStore.cs
- DictionaryEditChange.cs
- TreeNodeStyleCollection.cs
- QueryContinueDragEvent.cs
- DataObjectCopyingEventArgs.cs
- DataGridViewRowsRemovedEventArgs.cs
- SqlClientWrapperSmiStreamChars.cs
- BrushConverter.cs
- ControlFilterExpression.cs
- PageAsyncTask.cs
- ExpiredSecurityTokenException.cs
- JapaneseLunisolarCalendar.cs
- InfoCardRSAPKCS1SignatureDeformatter.cs
- MetadataProperty.cs
- ListenerChannelContext.cs
- Roles.cs
- TypeGeneratedEventArgs.cs
- LocalBuilder.cs
- ValidatingReaderNodeData.cs
- SqlServer2KCompatibilityAnnotation.cs
- Scheduler.cs
- PasswordRecoveryDesigner.cs
- EmptyEnumerable.cs
- XmlUtf8RawTextWriter.cs
- TagNameToTypeMapper.cs
- SystemException.cs
- CalendarDateRangeChangingEventArgs.cs
- printdlgexmarshaler.cs
- wgx_commands.cs
- ProfileInfo.cs
- WebServiceData.cs
- VirtualizingStackPanel.cs
- ShaderEffect.cs
- HwndHost.cs
- SafeNativeMethods.cs
- ToolStripRendererSwitcher.cs
- RedirectionProxy.cs
- SeekStoryboard.cs