Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- ServiceContractDetailViewControl.cs
- Journal.cs
- XmlRootAttribute.cs
- EditCommandColumn.cs
- SignedInfo.cs
- TdsParserStateObject.cs
- Span.cs
- Error.cs
- StylusPointDescription.cs
- TraceListener.cs
- TransformedBitmap.cs
- UIElementAutomationPeer.cs
- MetaModel.cs
- CaretElement.cs
- CodeCatchClauseCollection.cs
- HopperCache.cs
- CapabilitiesRule.cs
- ClientBuildManagerCallback.cs
- MailAddress.cs
- HashCodeCombiner.cs
- ProviderSettingsCollection.cs
- RoleManagerEventArgs.cs
- DispatcherExceptionFilterEventArgs.cs
- DbgUtil.cs
- TypeSource.cs
- EmptyEnumerator.cs
- XpsS0ValidatingLoader.cs
- FixedElement.cs
- SelectionRange.cs
- OleDbPermission.cs
- SqlProvider.cs
- AuthenticateEventArgs.cs
- ButtonChrome.cs
- InitializationEventAttribute.cs
- SqlProcedureAttribute.cs
- IconHelper.cs
- SchemaElementDecl.cs
- RadioButtonRenderer.cs
- BooleanAnimationUsingKeyFrames.cs
- AsymmetricSignatureFormatter.cs
- ProgressBar.cs
- remotingproxy.cs
- SqlStream.cs
- FtpCachePolicyElement.cs
- ControlCachePolicy.cs
- ConnectionStringSettings.cs
- _SecureChannel.cs
- SchemaName.cs
- GroupItemAutomationPeer.cs
- WebRequestModulesSection.cs
- PartialCachingAttribute.cs
- WhiteSpaceTrimStringConverter.cs
- ScriptReference.cs
- JsonServiceDocumentSerializer.cs
- Win32Interop.cs
- ConnectionPoolManager.cs
- TransactionBridgeSection.cs
- PeerCustomResolverElement.cs
- StrokeDescriptor.cs
- StructuralObject.cs
- ToolConsole.cs
- DataGridViewCellParsingEventArgs.cs
- XmlWriterSettings.cs
- WindowsScrollBarBits.cs
- XmlDataFileEditor.cs
- PerCallInstanceContextProvider.cs
- TableCellsCollectionEditor.cs
- ProjectedSlot.cs
- formatstringdialog.cs
- PeerInvitationResponse.cs
- PropertyDescriptorGridEntry.cs
- DocumentXmlWriter.cs
- HttpProcessUtility.cs
- WpfGeneratedKnownProperties.cs
- PersistChildrenAttribute.cs
- PrtCap_Public.cs
- Rotation3DAnimation.cs
- DemultiplexingClientMessageFormatter.cs
- DataGridViewDataErrorEventArgs.cs
- Context.cs
- ClientConfigurationSystem.cs
- BrowsableAttribute.cs
- IfJoinedCondition.cs
- ConcurrencyMode.cs
- ListView.cs
- DesignTimeParseData.cs
- DesignerTransaction.cs
- SettingsProviderCollection.cs
- ExtensionWindow.cs
- SiteMapDataSource.cs
- IntSecurity.cs
- Selection.cs
- SAPICategories.cs
- TextEditorTables.cs
- ContentElementAutomationPeer.cs
- HelpEvent.cs
- DataListGeneralPage.cs
- ReaderWriterLockWrapper.cs
- LineVisual.cs
- SerialStream.cs