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
- BufferedGraphicsManager.cs
- FieldBuilder.cs
- TextRunProperties.cs
- UiaCoreApi.cs
- SimpleApplicationHost.cs
- ModelMemberCollection.cs
- DashStyles.cs
- FormatConvertedBitmap.cs
- SafeArrayTypeMismatchException.cs
- SqlCommandAsyncResult.cs
- XmlSchemaSet.cs
- ScriptBehaviorDescriptor.cs
- PaperSource.cs
- HtmlTableCell.cs
- baseshape.cs
- QuadraticEase.cs
- Figure.cs
- AddressHeader.cs
- TemplateXamlParser.cs
- _CommandStream.cs
- AuthorizationContext.cs
- HwndHost.cs
- Transform3DCollection.cs
- AuthenticationManager.cs
- DynamicField.cs
- ReceiveCompletedEventArgs.cs
- EventWaitHandle.cs
- DecoratedNameAttribute.cs
- InteropExecutor.cs
- Vector3DCollection.cs
- ProtocolsConfiguration.cs
- X509Certificate2.cs
- ServiceKnownTypeAttribute.cs
- RemotingSurrogateSelector.cs
- X509DefaultServiceCertificateElement.cs
- DataGridRow.cs
- VirtualizingPanel.cs
- ProcessingInstructionAction.cs
- SendActivityEventArgs.cs
- ThreadSafeMessageFilterTable.cs
- DataListCommandEventArgs.cs
- RegexMatchCollection.cs
- SqlBuilder.cs
- CreationContext.cs
- IgnoreFileBuildProvider.cs
- ExtendedProtectionPolicyTypeConverter.cs
- ConvertEvent.cs
- WindowsHyperlink.cs
- RootBrowserWindowAutomationPeer.cs
- SvcMapFile.cs
- XamlFilter.cs
- BinHexEncoding.cs
- ResourceReferenceKeyNotFoundException.cs
- MergablePropertyAttribute.cs
- GrammarBuilderWildcard.cs
- BitmapEffectRenderDataResource.cs
- InputQueue.cs
- JavaScriptString.cs
- AdvancedBindingPropertyDescriptor.cs
- RemotingAttributes.cs
- SelectionUIService.cs
- Cursor.cs
- OletxResourceManager.cs
- ProfileServiceManager.cs
- WindowsIdentity.cs
- DataGridViewCellEventArgs.cs
- HttpCacheVary.cs
- Vector3D.cs
- FormClosedEvent.cs
- BufferBuilder.cs
- MenuItem.cs
- Attribute.cs
- IxmlLineInfo.cs
- QueryCursorEventArgs.cs
- DataGridViewUtilities.cs
- Pool.cs
- InvokeMethod.cs
- EventsTab.cs
- OleDbWrapper.cs
- XmlSerializerObjectSerializer.cs
- ProtocolsConfigurationEntry.cs
- AdvancedBindingEditor.cs
- HasCopySemanticsAttribute.cs
- PackagePartCollection.cs
- ISAPIWorkerRequest.cs
- EntityDataSourceReferenceGroup.cs
- DispatcherExceptionEventArgs.cs
- SchemaTableColumn.cs
- TaskFormBase.cs
- ExtendedProtectionPolicyTypeConverter.cs
- TraceUtils.cs
- MessageBox.cs
- AccessibleObject.cs
- HttpContextServiceHost.cs
- Comparer.cs
- CssClassPropertyAttribute.cs
- TextTreeTextBlock.cs
- InputScopeConverter.cs
- ManipulationStartedEventArgs.cs
- ForEachDesigner.xaml.cs