Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- SqlComparer.cs
- DialogResultConverter.cs
- RotateTransform3D.cs
- ReadOnlyDictionary.cs
- NumericUpDownAccelerationCollection.cs
- TextRunCacheImp.cs
- WebHttpSecurityElement.cs
- PropertyItemInternal.cs
- CompiledXpathExpr.cs
- InstanceKeyCollisionException.cs
- SymmetricKeyWrap.cs
- EventLogTraceListener.cs
- CannotUnloadAppDomainException.cs
- TaskFileService.cs
- SamlAttributeStatement.cs
- IItemContainerGenerator.cs
- UnionCqlBlock.cs
- CodeMemberProperty.cs
- SelectedCellsCollection.cs
- ControlPropertyNameConverter.cs
- FixUpCollection.cs
- FirstMatchCodeGroup.cs
- HttpRequestCacheValidator.cs
- LoginUtil.cs
- XmlAnyAttributeAttribute.cs
- SqlFlattener.cs
- SEHException.cs
- HtmlLink.cs
- ChannelPoolSettings.cs
- TreeViewItemAutomationPeer.cs
- BinaryFormatterWriter.cs
- NeutralResourcesLanguageAttribute.cs
- IntSecurity.cs
- HitTestDrawingContextWalker.cs
- Converter.cs
- HealthMonitoringSectionHelper.cs
- WindowsUpDown.cs
- EventLogStatus.cs
- AutomationAttributeInfo.cs
- SingleObjectCollection.cs
- wmiprovider.cs
- ClosableStream.cs
- Transform.cs
- BitmapCodecInfoInternal.cs
- MembershipUser.cs
- CodeMethodReturnStatement.cs
- FillErrorEventArgs.cs
- ObjectListShowCommandsEventArgs.cs
- ReferenceConverter.cs
- DataPagerFieldCommandEventArgs.cs
- MILUtilities.cs
- SynchronizedDispatch.cs
- SplitterPanel.cs
- Helpers.cs
- COM2IPerPropertyBrowsingHandler.cs
- TextTreePropertyUndoUnit.cs
- BitmapEffectCollection.cs
- SocketElement.cs
- TypeGeneratedEventArgs.cs
- UrlMappingsSection.cs
- XmlBinaryReader.cs
- CacheChildrenQuery.cs
- ReadOnlyCollectionBase.cs
- ServiceManager.cs
- GridProviderWrapper.cs
- UIPermission.cs
- AppModelKnownContentFactory.cs
- ServiceBehaviorAttribute.cs
- UnsafeNativeMethods.cs
- GeometryCollection.cs
- SqlGatherProducedAliases.cs
- URLString.cs
- FieldAccessException.cs
- ContentOperations.cs
- Debug.cs
- RawStylusInputCustomData.cs
- NamespaceQuery.cs
- TextRunProperties.cs
- GetCryptoTransformRequest.cs
- FontSizeConverter.cs
- ScrollItemPattern.cs
- ProxyWebPart.cs
- ObjectMemberMapping.cs
- CalendarAutomationPeer.cs
- ApplicationActivator.cs
- JsonEncodingStreamWrapper.cs
- WebPartCancelEventArgs.cs
- PersistChildrenAttribute.cs
- UmAlQuraCalendar.cs
- UrlAuthFailedErrorFormatter.cs
- ManipulationDelta.cs
- InheritanceService.cs
- RegularExpressionValidator.cs
- GeometryDrawing.cs
- TypeDelegator.cs
- TimeZone.cs
- HttpHandlerAction.cs
- RoleService.cs
- Label.cs
- ListViewDeleteEventArgs.cs