Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / 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; } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AnnotationStore.cs
- WindowsPen.cs
- AssemblyResourceLoader.cs
- ActivityExecutorSurrogate.cs
- NamedObject.cs
- OdbcConnectionFactory.cs
- DesignerAttribute.cs
- ToolStripPanelCell.cs
- XPathDescendantIterator.cs
- ChangePassword.cs
- IdnElement.cs
- GeometryHitTestResult.cs
- RightsManagementEncryptedStream.cs
- FragmentQueryKB.cs
- PropertyChangeTracker.cs
- ConnectionPoolManager.cs
- XmlWriter.cs
- GeneralTransform3DCollection.cs
- FixedSOMGroup.cs
- Stacktrace.cs
- CodeDelegateInvokeExpression.cs
- BigInt.cs
- FormsAuthenticationCredentials.cs
- EpmSyndicationContentDeSerializer.cs
- SqlTriggerContext.cs
- ProtocolsConfigurationEntry.cs
- XmlSchemaImporter.cs
- ListDataBindEventArgs.cs
- FormattedText.cs
- RequestQueue.cs
- WebBrowserProgressChangedEventHandler.cs
- EdmEntityTypeAttribute.cs
- WindowsListViewItem.cs
- ComponentEditorPage.cs
- WebBrowsableAttribute.cs
- SequentialUshortCollection.cs
- ObjectStateFormatter.cs
- SettingsProviderCollection.cs
- SchemaDeclBase.cs
- ConnectionModeReader.cs
- Convert.cs
- FileDialog.cs
- TemplateComponentConnector.cs
- GcSettings.cs
- DictionaryGlobals.cs
- MexServiceChannelBuilder.cs
- SiteMapSection.cs
- ExpressionBuilderCollection.cs
- AlphabetConverter.cs
- RotateTransform.cs
- WebPartHeaderCloseVerb.cs
- New.cs
- sqlser.cs
- DataGridViewCellLinkedList.cs
- DataGridViewCellContextMenuStripNeededEventArgs.cs
- PropertyStore.cs
- PropertyCondition.cs
- DBSqlParser.cs
- EntitySqlQueryState.cs
- StringResourceManager.cs
- OrderedDictionary.cs
- ServicePoint.cs
- GAC.cs
- RuntimeHandles.cs
- ClientTarget.cs
- DataSourceConverter.cs
- ToolBarButton.cs
- HorizontalAlignConverter.cs
- MaterialGroup.cs
- PointCollection.cs
- VScrollBar.cs
- ClientConvert.cs
- CompositeFontParser.cs
- TextTabProperties.cs
- future.cs
- DataGrid.cs
- ItemCollection.cs
- UnsafeNativeMethods.cs
- TypeForwardedToAttribute.cs
- DateTimeConstantAttribute.cs
- ProcessThread.cs
- unitconverter.cs
- Point4DConverter.cs
- TemplateField.cs
- FillErrorEventArgs.cs
- ImmComposition.cs
- RequestCacheValidator.cs
- DesignOnlyAttribute.cs
- FormViewUpdatedEventArgs.cs
- TextHidden.cs
- LogArchiveSnapshot.cs
- DrawingImage.cs
- FormsAuthenticationModule.cs
- ControlBuilder.cs
- SurrogateSelector.cs
- WindowsStatic.cs
- XPathExpr.cs
- ByteStreamGeometryContext.cs
- IncrementalHitTester.cs
- CapiSafeHandles.cs