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
- XmlSchemaSimpleContent.cs
- LinqDataSourceContextEventArgs.cs
- SharedPerformanceCounter.cs
- SizeAnimationClockResource.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- ExpressionList.cs
- _UriTypeConverter.cs
- DrawToolTipEventArgs.cs
- GlyphsSerializer.cs
- ToolStripSystemRenderer.cs
- DesignerValidatorAdapter.cs
- Renderer.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- FixedPage.cs
- XmlParser.cs
- SmtpNtlmAuthenticationModule.cs
- AspCompat.cs
- DataGridColumnHeaderItemAutomationPeer.cs
- Focus.cs
- ExpressionCopier.cs
- Message.cs
- ContractsBCL.cs
- StringInfo.cs
- XdrBuilder.cs
- ControlParameter.cs
- TextBounds.cs
- PassportPrincipal.cs
- TripleDESCryptoServiceProvider.cs
- XhtmlTextWriter.cs
- UrlPath.cs
- RotateTransform3D.cs
- SqlDataSourceEnumerator.cs
- DataControlFieldCell.cs
- UxThemeWrapper.cs
- BamlLocalizerErrorNotifyEventArgs.cs
- PointConverter.cs
- SqlParameterCollection.cs
- WriteFileContext.cs
- BezierSegment.cs
- EpmCustomContentDeSerializer.cs
- _NestedSingleAsyncResult.cs
- ToolStripPanelCell.cs
- SqlDataSourceCache.cs
- ProviderBase.cs
- ElementProxy.cs
- PanningMessageFilter.cs
- SelectedDatesCollection.cs
- DesignerAutoFormatStyle.cs
- WebPartCatalogAddVerb.cs
- NativeMethodsOther.cs
- HandleRef.cs
- LocalBuilder.cs
- AsymmetricKeyExchangeDeformatter.cs
- InputScopeNameConverter.cs
- Highlights.cs
- RolePrincipal.cs
- ValidationSummary.cs
- WebZone.cs
- MsmqIntegrationBindingElement.cs
- HttpModulesSection.cs
- Color.cs
- X509UI.cs
- RijndaelManaged.cs
- AudioFormatConverter.cs
- MarkupCompilePass2.cs
- HttpPostedFile.cs
- HostingEnvironment.cs
- NativeMethods.cs
- COMException.cs
- List.cs
- RoleGroupCollection.cs
- InlineUIContainer.cs
- IDReferencePropertyAttribute.cs
- EntitySetBaseCollection.cs
- UriExt.cs
- ExpressionBindingCollection.cs
- DataColumnMappingCollection.cs
- DodSequenceMerge.cs
- AffineTransform3D.cs
- FixedTextPointer.cs
- HttpUnhandledOperationInvoker.cs
- WindowsRichEditRange.cs
- PeerCustomResolverSettings.cs
- RtfFormatStack.cs
- ListViewItem.cs
- DispatchWrapper.cs
- FreezableOperations.cs
- PropertyEmitter.cs
- MailBnfHelper.cs
- DocumentGridContextMenu.cs
- SoapBinding.cs
- RegexNode.cs
- PropertyBuilder.cs
- ExpressionPrinter.cs
- DbFunctionCommandTree.cs
- DynamicRendererThreadManager.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- FormatSettings.cs
- COM2PictureConverter.cs
- TypeName.cs