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
- DataChangedEventManager.cs
- RequestQueryParser.cs
- SchemaElementLookUpTable.cs
- TextEndOfLine.cs
- BaseResourcesBuildProvider.cs
- HttpClientCertificate.cs
- SqlDependencyListener.cs
- XmlSchemaSearchPattern.cs
- WpfGeneratedKnownTypes.cs
- RegexMatchCollection.cs
- ColorBlend.cs
- BypassElementCollection.cs
- CodeTypeReferenceCollection.cs
- TimeSpanValidatorAttribute.cs
- AtomMaterializer.cs
- Vector3D.cs
- InternalCache.cs
- ContextMarshalException.cs
- Lease.cs
- DesignerSerializationManager.cs
- Opcode.cs
- IPAddressCollection.cs
- FindSimilarActivitiesVerb.cs
- NavigationProgressEventArgs.cs
- ListSourceHelper.cs
- BrushValueSerializer.cs
- OperationAbortedException.cs
- PtsCache.cs
- ComplexType.cs
- StylusSystemGestureEventArgs.cs
- DesignerCommandAdapter.cs
- MessageBox.cs
- CodePageEncoding.cs
- TemplateComponentConnector.cs
- PageWrapper.cs
- HttpValueCollection.cs
- Parameter.cs
- WpfGeneratedKnownTypes.cs
- SystemEvents.cs
- EntityDataSourceWrapper.cs
- FontFamily.cs
- Repeater.cs
- RealizationContext.cs
- DataTableMappingCollection.cs
- HtmlLink.cs
- DataBindEngine.cs
- FixedTextSelectionProcessor.cs
- WebServiceAttribute.cs
- GlyphCollection.cs
- BitmapImage.cs
- UnsettableComboBox.cs
- DATA_BLOB.cs
- Screen.cs
- EntityStoreSchemaFilterEntry.cs
- CryptoConfig.cs
- DataObjectCopyingEventArgs.cs
- NewArrayExpression.cs
- Matrix3DValueSerializer.cs
- Image.cs
- ObjectParameter.cs
- FilterEventArgs.cs
- CharStorage.cs
- MemberAccessException.cs
- BaseCAMarshaler.cs
- AliasedSlot.cs
- TypeHelpers.cs
- StoreAnnotationsMap.cs
- ScaleTransform.cs
- BinaryUtilClasses.cs
- FieldAccessException.cs
- Variable.cs
- NetDataContractSerializer.cs
- TabControlCancelEvent.cs
- QuaternionAnimation.cs
- DriveNotFoundException.cs
- NullExtension.cs
- SharedDp.cs
- XpsLiterals.cs
- Component.cs
- DelegatingTypeDescriptionProvider.cs
- PropertyConverter.cs
- TableItemStyle.cs
- ImageFormatConverter.cs
- PropertyFilterAttribute.cs
- ExceptionNotification.cs
- NamedPipeActivation.cs
- DomainUpDown.cs
- LayoutManager.cs
- SystemColors.cs
- LocatorPart.cs
- SqlDataSourceConnectionPanel.cs
- CounterSample.cs
- EntityType.cs
- HotCommands.cs
- Rotation3D.cs
- UInt16Storage.cs
- SearchForVirtualItemEventArgs.cs
- UIPermission.cs
- Input.cs
- ProxyGenerationError.cs