Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- FacetValues.cs
- ModelUtilities.cs
- TimeStampChecker.cs
- MetafileEditor.cs
- Span.cs
- BamlWriter.cs
- TableHeaderCell.cs
- AssociationSet.cs
- ProviderCommandInfoUtils.cs
- RichTextBoxConstants.cs
- UndoManager.cs
- XmlSerializableWriter.cs
- SystemUnicastIPAddressInformation.cs
- JpegBitmapEncoder.cs
- AnonymousIdentificationSection.cs
- TableFieldsEditor.cs
- listitem.cs
- PropertyGeneratedEventArgs.cs
- PathSegmentCollection.cs
- SystemSounds.cs
- TypeGenericEnumerableViewSchema.cs
- RadioButtonDesigner.cs
- WebPartConnectionsDisconnectVerb.cs
- Parser.cs
- RayMeshGeometry3DHitTestResult.cs
- Decoder.cs
- ManagedCodeMarkers.cs
- MetafileEditor.cs
- InvokeProviderWrapper.cs
- _RequestLifetimeSetter.cs
- CommandTreeTypeHelper.cs
- ContentFilePart.cs
- SqlCacheDependency.cs
- WindowsListViewGroup.cs
- QilIterator.cs
- HttpProfileBase.cs
- LogAppendAsyncResult.cs
- CharEnumerator.cs
- DataServiceQueryException.cs
- MSHTMLHost.cs
- ImageAnimator.cs
- safemediahandle.cs
- DataStorage.cs
- PowerModeChangedEventArgs.cs
- validationstate.cs
- WindowsPrincipal.cs
- errorpatternmatcher.cs
- CDSsyncETWBCLProvider.cs
- AppDomain.cs
- FileLogRecordHeader.cs
- cache.cs
- ObjectAnimationUsingKeyFrames.cs
- TextFragmentEngine.cs
- EntityClientCacheKey.cs
- GroupStyle.cs
- ProxySimple.cs
- KnownTypesHelper.cs
- PropertyRef.cs
- WindowsListViewItem.cs
- FontWeight.cs
- FixUpCollection.cs
- BamlVersionHeader.cs
- Accessible.cs
- CodeGotoStatement.cs
- FormClosedEvent.cs
- ActiveDocumentEvent.cs
- PKCS1MaskGenerationMethod.cs
- MsdtcClusterUtils.cs
- MasterPageBuildProvider.cs
- ExceptionUtil.cs
- PropertyGeneratedEventArgs.cs
- DataGridColumnCollection.cs
- UnhandledExceptionEventArgs.cs
- RC2CryptoServiceProvider.cs
- SerializationHelper.cs
- UrlPath.cs
- StringValueSerializer.cs
- CurrentChangedEventManager.cs
- WorkItem.cs
- PanelDesigner.cs
- PagesSection.cs
- RSAProtectedConfigurationProvider.cs
- Claim.cs
- DataGridViewCellCancelEventArgs.cs
- ComponentResourceKeyConverter.cs
- MessageDecoder.cs
- TreeViewImageIndexConverter.cs
- UnknownWrapper.cs
- SessionStateContainer.cs
- MemberMaps.cs
- Button.cs
- WebPartRestoreVerb.cs
- DbModificationCommandTree.cs
- CompilationUtil.cs
- EventManager.cs
- SerializationAttributes.cs
- precedingsibling.cs
- Canonicalizers.cs
- TreeNodeClickEventArgs.cs
- ValidatorCollection.cs