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
- StringArrayConverter.cs
- SignatureHelper.cs
- TextSelectionProcessor.cs
- WebPartMinimizeVerb.cs
- RepeaterItem.cs
- ZipIOModeEnforcingStream.cs
- BamlLocalizationDictionary.cs
- StandardBindingReliableSessionElement.cs
- Rotation3D.cs
- DupHandleConnectionReader.cs
- RecognizedAudio.cs
- StringOutput.cs
- DataPagerField.cs
- LocalBuilder.cs
- Schema.cs
- CodeTypeMemberCollection.cs
- HttpException.cs
- WindowsTreeView.cs
- OutputCacheProfile.cs
- Transform3D.cs
- TextEditorCopyPaste.cs
- BackgroundWorker.cs
- SpellerStatusTable.cs
- MatrixTransform3D.cs
- WebConfigurationHost.cs
- MatrixStack.cs
- RadioButton.cs
- SymbolTable.cs
- ModuleConfigurationInfo.cs
- CollectionViewGroupRoot.cs
- Schema.cs
- EmbossBitmapEffect.cs
- GatewayIPAddressInformationCollection.cs
- ForeignKeyFactory.cs
- KeyboardNavigation.cs
- TextFindEngine.cs
- CompositeActivityTypeDescriptorProvider.cs
- BuildProvider.cs
- WebPartHelpVerb.cs
- AliasExpr.cs
- TextServicesLoader.cs
- XamlSerializerUtil.cs
- SqlConnectionHelper.cs
- AdRotatorDesigner.cs
- SubpageParagraph.cs
- SafeFileHandle.cs
- MobileUserControlDesigner.cs
- EmptyElement.cs
- HtmlShimManager.cs
- GacUtil.cs
- FixedSOMElement.cs
- PostBackTrigger.cs
- DatagridviewDisplayedBandsData.cs
- SafeFileHandle.cs
- XmlElementList.cs
- SpeechEvent.cs
- RequestCachingSection.cs
- WebScriptMetadataInstanceContextProvider.cs
- Emitter.cs
- GeometryHitTestResult.cs
- MatchingStyle.cs
- RowParagraph.cs
- ObjectQueryState.cs
- IsolatedStorageFilePermission.cs
- DispatcherTimer.cs
- EntityDesignPluralizationHandler.cs
- DNS.cs
- InlineObject.cs
- CompositeControl.cs
- NavigateEvent.cs
- TextBounds.cs
- _IPv4Address.cs
- BuildProvider.cs
- AuthenticationConfig.cs
- IListConverters.cs
- XmlDataSourceView.cs
- RefreshEventArgs.cs
- MemoryMappedFile.cs
- FilteredSchemaElementLookUpTable.cs
- CodeDOMProvider.cs
- NativeMethods.cs
- LockRecursionException.cs
- CngProperty.cs
- DataGridTextBox.cs
- DiscoveryDocument.cs
- IDispatchConstantAttribute.cs
- Literal.cs
- PlaceHolder.cs
- PriorityBinding.cs
- UrlAuthFailedErrorFormatter.cs
- GeometryHitTestResult.cs
- DocumentOutline.cs
- EnumType.cs
- UdpDuplexChannel.cs
- NavigationCommands.cs
- SqlCacheDependencyDatabaseCollection.cs
- StatusBarItemAutomationPeer.cs
- SecUtil.cs
- TypeLoadException.cs
- ServiceOperationListItem.cs