Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / 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. //------------------------------------------------------------------------------ //// 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
- FilterQueryOptionExpression.cs
- BufferModeSettings.cs
- OutputCacheProfileCollection.cs
- RoleManagerSection.cs
- WsdlWriter.cs
- SQLDateTimeStorage.cs
- NonVisualControlAttribute.cs
- DataFormats.cs
- FixUp.cs
- ThicknessAnimationBase.cs
- ParentUndoUnit.cs
- SafeHandles.cs
- ProfileService.cs
- Sql8ConformanceChecker.cs
- Bits.cs
- AnimatedTypeHelpers.cs
- CommonRemoteMemoryBlock.cs
- CodeObjectCreateExpression.cs
- NamedElement.cs
- Rect3D.cs
- ObjectItemCollection.cs
- SafeUserTokenHandle.cs
- StylusDevice.cs
- XamlTreeBuilder.cs
- TrackingMemoryStreamFactory.cs
- AttachedAnnotation.cs
- FrameDimension.cs
- DirtyTextRange.cs
- Point3DAnimationUsingKeyFrames.cs
- DataSourceXmlClassAttribute.cs
- MobileRedirect.cs
- DocumentOrderComparer.cs
- ChtmlPageAdapter.cs
- EventItfInfo.cs
- InputQueueChannelAcceptor.cs
- COAUTHIDENTITY.cs
- MiniLockedBorderGlyph.cs
- ToolBarButtonClickEvent.cs
- CompositeScriptReference.cs
- XDeferredAxisSource.cs
- ActivityTrace.cs
- SystemEvents.cs
- Literal.cs
- ConditionCollection.cs
- DecimalAnimationBase.cs
- PrintController.cs
- SortedList.cs
- StorageEntityTypeMapping.cs
- SwitchAttribute.cs
- DrawingVisual.cs
- PathSegmentCollection.cs
- PenLineCapValidation.cs
- ListItemCollection.cs
- ActivityExecutionContextCollection.cs
- ModuleBuilder.cs
- EventLogInformation.cs
- QueryConverter.cs
- Internal.cs
- ShapingWorkspace.cs
- CommandField.cs
- DocumentOrderComparer.cs
- ComponentCollection.cs
- Formatter.cs
- AnimationClock.cs
- Configuration.cs
- DataColumnMappingCollection.cs
- ControlTemplate.cs
- PageHandlerFactory.cs
- PersonalizationStateInfo.cs
- IImplicitResourceProvider.cs
- TextEndOfParagraph.cs
- DesigntimeLicenseContext.cs
- EmptyEnumerable.cs
- ToolboxItemAttribute.cs
- DataBoundLiteralControl.cs
- Guid.cs
- XmlCustomFormatter.cs
- TextTreeTextElementNode.cs
- MatchNoneMessageFilter.cs
- CodeAccessSecurityEngine.cs
- SoapSchemaExporter.cs
- Grid.cs
- Application.cs
- PeerCollaboration.cs
- TextInfo.cs
- SQLUtility.cs
- HttpPostServerProtocol.cs
- EventBuilder.cs
- SyndicationPerson.cs
- SymbolEqualComparer.cs
- StylusSystemGestureEventArgs.cs
- EraserBehavior.cs
- BitmapFrameEncode.cs
- FlowDocumentPaginator.cs
- GestureRecognizer.cs
- ObjectStateEntryOriginalDbUpdatableDataRecord.cs
- TransformFinalBlockRequest.cs
- KeyPressEvent.cs
- AutoGeneratedField.cs
- ReachIDocumentPaginatorSerializerAsync.cs