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
- CompleteWizardStep.cs
- MdImport.cs
- ComponentCollection.cs
- RuntimeIdentifierPropertyAttribute.cs
- BinaryWriter.cs
- DynamicActionMessageFilter.cs
- XMLSyntaxException.cs
- XmlTextWriter.cs
- ListViewItemEventArgs.cs
- DataGridViewDataErrorEventArgs.cs
- SourceElementsCollection.cs
- UriPrefixTable.cs
- EncryptedData.cs
- Timeline.cs
- TreeViewCancelEvent.cs
- NumberFunctions.cs
- FontNamesConverter.cs
- linebase.cs
- ListDictionaryInternal.cs
- XPathNavigator.cs
- TiffBitmapDecoder.cs
- FixedFlowMap.cs
- TreeNodeStyle.cs
- CatalogPartCollection.cs
- InvalidDataContractException.cs
- QilInvokeLateBound.cs
- AcceleratedTokenProvider.cs
- XmlSchemaElement.cs
- CqlIdentifiers.cs
- ServiceOperation.cs
- WebPartConnectionsCancelVerb.cs
- TemplatedMailWebEventProvider.cs
- ContentPlaceHolder.cs
- NavigationProgressEventArgs.cs
- EasingFunctionBase.cs
- SchemaElementDecl.cs
- ThemeInfoAttribute.cs
- WindowsGraphics2.cs
- WriteLineDesigner.xaml.cs
- TransformerInfo.cs
- RegularExpressionValidator.cs
- WebPartDescription.cs
- StoreAnnotationsMap.cs
- EmbeddedObject.cs
- PeerTransportSecuritySettings.cs
- DataExpression.cs
- datacache.cs
- QilInvokeEarlyBound.cs
- SocketPermission.cs
- ControlBindingsCollection.cs
- _NetworkingPerfCounters.cs
- GraphicsState.cs
- XmlWellformedWriter.cs
- GlobalEventManager.cs
- Vertex.cs
- ListArgumentProvider.cs
- SafeTimerHandle.cs
- TaskScheduler.cs
- DetailsViewPageEventArgs.cs
- LinkButton.cs
- QueryRelOp.cs
- SqlFormatter.cs
- ExpressionConverter.cs
- ReadOnlyCollectionBase.cs
- FileDialog.cs
- ClientSponsor.cs
- AndMessageFilterTable.cs
- TimelineGroup.cs
- TableTextElementCollectionInternal.cs
- StringValidator.cs
- CrossContextChannel.cs
- XmlSchemaSimpleTypeUnion.cs
- ObjectDataSourceEventArgs.cs
- XmlSchemaSet.cs
- CatalogZoneBase.cs
- TextTabProperties.cs
- PhysicalOps.cs
- ConfigurationValidatorBase.cs
- Clipboard.cs
- StaticSiteMapProvider.cs
- SafeFileMappingHandle.cs
- ToolboxSnapDragDropEventArgs.cs
- WindowsContainer.cs
- DataSourceSelectArguments.cs
- SByte.cs
- WmlCalendarAdapter.cs
- EventTask.cs
- RoleServiceManager.cs
- Int32RectConverter.cs
- MeshGeometry3D.cs
- HttpListenerException.cs
- SingleSelectRootGridEntry.cs
- CompilerErrorCollection.cs
- BindingsCollection.cs
- AuthenticationException.cs
- RsaSecurityToken.cs
- BaseValidator.cs
- XamlRtfConverter.cs
- AuthenticationService.cs
- EdmType.cs