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
- RIPEMD160Managed.cs
- Compilation.cs
- AssemblyNameProxy.cs
- CheckBoxList.cs
- _NegoState.cs
- FacetDescription.cs
- ToolBar.cs
- HMACSHA512.cs
- MissingSatelliteAssemblyException.cs
- CookieParameter.cs
- AccessedThroughPropertyAttribute.cs
- CodePageEncoding.cs
- PropagatorResult.cs
- EFAssociationProvider.cs
- ToolBarButton.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- CalculatedColumn.cs
- DeferredTextReference.cs
- DuplicateWaitObjectException.cs
- DiagnosticTraceSchemas.cs
- ManagedIStream.cs
- AnalyzedTree.cs
- ToolStripPanelRenderEventArgs.cs
- EditorZone.cs
- NativeCompoundFileAPIs.cs
- Matrix3DValueSerializer.cs
- ThicknessKeyFrameCollection.cs
- sqlinternaltransaction.cs
- HttpPostedFileWrapper.cs
- NetworkInterface.cs
- MenuItemAutomationPeer.cs
- SliderAutomationPeer.cs
- ButtonFieldBase.cs
- Point3DCollection.cs
- TreeView.cs
- TextPointer.cs
- GatewayDefinition.cs
- MessageFormatterConverter.cs
- PingReply.cs
- StrokeCollectionDefaultValueFactory.cs
- RightsManagementEncryptedStream.cs
- TriState.cs
- FormsAuthenticationUser.cs
- StringAnimationUsingKeyFrames.cs
- HttpTransportSecurityElement.cs
- ToolStripDropDownButton.cs
- PaintEvent.cs
- CompositeClientFormatter.cs
- GridToolTip.cs
- Vector3DCollectionConverter.cs
- BindingCompleteEventArgs.cs
- AdornerLayer.cs
- CheckBoxList.cs
- MobileControlsSectionHandler.cs
- TextSpan.cs
- WindowsButton.cs
- ToolstripProfessionalRenderer.cs
- InlineObject.cs
- LiteralTextParser.cs
- DocumentXPathNavigator.cs
- InkCanvasFeedbackAdorner.cs
- DataComponentNameHandler.cs
- FixedSOMPageConstructor.cs
- Padding.cs
- CompilerScope.cs
- EntityContainerEmitter.cs
- SR.cs
- ConfigXmlAttribute.cs
- SubstitutionList.cs
- GridErrorDlg.cs
- TemplateManager.cs
- MainMenu.cs
- IncrementalCompileAnalyzer.cs
- Win32.cs
- DataServiceException.cs
- DesignerHierarchicalDataSourceView.cs
- ArgumentDesigner.xaml.cs
- DecimalAnimation.cs
- XmlSubtreeReader.cs
- KeyValuePairs.cs
- NativeCppClassAttribute.cs
- ReaderContextStackData.cs
- ProfileEventArgs.cs
- Camera.cs
- TemplateContentLoader.cs
- UserControlBuildProvider.cs
- QuestionEventArgs.cs
- ProviderIncompatibleException.cs
- OSFeature.cs
- Subtree.cs
- ZipPackagePart.cs
- EngineSiteSapi.cs
- WebPartTransformerAttribute.cs
- TreeNodeStyle.cs
- RowToParametersTransformer.cs
- DeviceSpecificDesigner.cs
- ContentType.cs
- OrderedHashRepartitionEnumerator.cs
- AnnouncementEndpointElement.cs
- Property.cs