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
- ResXResourceSet.cs
- RowUpdatedEventArgs.cs
- ManifestResourceInfo.cs
- Int32AnimationUsingKeyFrames.cs
- Html32TextWriter.cs
- BitmapEffectGroup.cs
- Membership.cs
- ListDesigner.cs
- EndpointNotFoundException.cs
- UserInitiatedNavigationPermission.cs
- QilChoice.cs
- PointLight.cs
- OrthographicCamera.cs
- FormatterServices.cs
- InputLanguageCollection.cs
- ObjectNavigationPropertyMapping.cs
- RenderDataDrawingContext.cs
- InputLanguageSource.cs
- FormViewInsertedEventArgs.cs
- DataGridItem.cs
- _AutoWebProxyScriptWrapper.cs
- DataGridRowDetailsEventArgs.cs
- AdapterUtil.cs
- CapabilitiesRule.cs
- DataServiceExpressionVisitor.cs
- DataRecordObjectView.cs
- EntityDescriptor.cs
- EntityDescriptor.cs
- SafeHandle.cs
- UnsafeNativeMethods.cs
- TextStore.cs
- UnaryExpression.cs
- AuthorizationPolicyTypeElement.cs
- BamlLocalizableResourceKey.cs
- RouteTable.cs
- MenuItemCollection.cs
- ControlBindingsCollection.cs
- XmlSubtreeReader.cs
- CodeConstructor.cs
- PropertyInfo.cs
- Point3DValueSerializer.cs
- ThreadStartException.cs
- WsatEtwTraceListener.cs
- DispatchWrapper.cs
- _DisconnectOverlappedAsyncResult.cs
- ChannelDispatcherBase.cs
- SignatureResourcePool.cs
- RecordsAffectedEventArgs.cs
- XsdBuildProvider.cs
- TabItemAutomationPeer.cs
- TextBlockAutomationPeer.cs
- TreeNodeCollection.cs
- ToolBarButtonDesigner.cs
- LogFlushAsyncResult.cs
- NotifyCollectionChangedEventArgs.cs
- XmlNavigatorFilter.cs
- OptimizedTemplateContentHelper.cs
- AssemblyBuilder.cs
- MimeMultiPart.cs
- CreateUserWizardStep.cs
- Stack.cs
- HashMembershipCondition.cs
- IPGlobalProperties.cs
- ParameterModifier.cs
- SmtpAuthenticationManager.cs
- DataGridTextBoxColumn.cs
- DependencyProperty.cs
- ExpandCollapsePattern.cs
- SHA1.cs
- XamlReader.cs
- PathGeometry.cs
- FilterableAttribute.cs
- SmiRequestExecutor.cs
- TCPListener.cs
- TrackBarRenderer.cs
- TextEditorDragDrop.cs
- DetailsViewInsertedEventArgs.cs
- WebConfigurationHostFileChange.cs
- DesignerSerializerAttribute.cs
- webeventbuffer.cs
- Int32Collection.cs
- Hash.cs
- FormConverter.cs
- TypedOperationInfo.cs
- TabControlToolboxItem.cs
- ChangePassword.cs
- Unit.cs
- TypeElement.cs
- AttributeTable.cs
- DifferencingCollection.cs
- TypeConverterMarkupExtension.cs
- ApplicationActivator.cs
- WinFormsComponentEditor.cs
- SystemColors.cs
- KeyNotFoundException.cs
- DataGridViewRow.cs
- Pair.cs
- RawAppCommandInputReport.cs
- WebServiceTypeData.cs
- InstallerTypeAttribute.cs