Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PersonalizationProviderCollection.cs
- RequestResizeEvent.cs
- XmlFormatExtensionPointAttribute.cs
- SettingsPropertyIsReadOnlyException.cs
- BitmapEffectInput.cs
- Content.cs
- XmlTextReader.cs
- Window.cs
- CommandHelper.cs
- WebRequestModulesSection.cs
- QuadTree.cs
- ItemDragEvent.cs
- DrawingCollection.cs
- DataRow.cs
- JavaScriptObjectDeserializer.cs
- GridViewCommandEventArgs.cs
- RequiredAttributeAttribute.cs
- MsdtcClusterUtils.cs
- FastEncoderStatics.cs
- ButtonFieldBase.cs
- OdbcException.cs
- XamlInt32CollectionSerializer.cs
- DataGridViewCellStyleConverter.cs
- basenumberconverter.cs
- XmlSerializerOperationBehavior.cs
- DataGridViewCellStyle.cs
- TypeNameConverter.cs
- Update.cs
- FlowDocumentPage.cs
- _UriTypeConverter.cs
- GridViewCommandEventArgs.cs
- MappingException.cs
- HttpFormatExtensions.cs
- EmbeddedObject.cs
- MdiWindowListItemConverter.cs
- WindowManager.cs
- Pair.cs
- AccessKeyManager.cs
- AncillaryOps.cs
- WebPartConnectionsEventArgs.cs
- InvokePattern.cs
- StrokeIntersection.cs
- StatusBarAutomationPeer.cs
- HtmlInputReset.cs
- StringFormat.cs
- SqlTransaction.cs
- ObjectStateFormatter.cs
- localization.cs
- nulltextcontainer.cs
- StringArrayConverter.cs
- SelectionItemPattern.cs
- DataGridViewRowHeightInfoPushedEventArgs.cs
- AssemblySettingAttributes.cs
- PlatformCulture.cs
- ColumnCollection.cs
- SourceFileBuildProvider.cs
- X509SubjectKeyIdentifierClause.cs
- HttpRuntime.cs
- MultipartIdentifier.cs
- CorrelationResolver.cs
- ProviderManager.cs
- XmlArrayAttribute.cs
- BrowserTree.cs
- InstanceHandleConflictException.cs
- MessageQueuePermissionAttribute.cs
- CodePageEncoding.cs
- ValidateNames.cs
- SpotLight.cs
- CodeThrowExceptionStatement.cs
- Set.cs
- cache.cs
- Int16Animation.cs
- CultureInfo.cs
- _NetworkingPerfCounters.cs
- KeyValueConfigurationCollection.cs
- HtmlWindow.cs
- TextRangeEditTables.cs
- VarRefManager.cs
- FillRuleValidation.cs
- TreeViewImageKeyConverter.cs
- HandlerFactoryCache.cs
- PersistenceProvider.cs
- Currency.cs
- HealthMonitoringSection.cs
- GetWorkflowTree.cs
- ServerValidateEventArgs.cs
- SqlRemoveConstantOrderBy.cs
- FileAuthorizationModule.cs
- DataGridSortCommandEventArgs.cs
- EncryptedPackage.cs
- ConstrainedGroup.cs
- IntegerFacetDescriptionElement.cs
- ColumnReorderedEventArgs.cs
- FormViewInsertedEventArgs.cs
- ComponentCache.cs
- KeyGestureValueSerializer.cs
- DuplexChannelBinder.cs
- HttpRuntime.cs
- RetriableClipboard.cs
- EqualityComparer.cs