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
- Signature.cs
- WindowCollection.cs
- DataListItem.cs
- DecimalKeyFrameCollection.cs
- HtmlTable.cs
- InternalControlCollection.cs
- ValidateNames.cs
- EncoderNLS.cs
- DictionaryBase.cs
- UndoUnit.cs
- WizardStepBase.cs
- OracleParameter.cs
- ProxyWebPartManager.cs
- StaticDataManager.cs
- NeutralResourcesLanguageAttribute.cs
- JpegBitmapDecoder.cs
- SessionStateItemCollection.cs
- CodeAssignStatement.cs
- CompilationUtil.cs
- CustomTypeDescriptor.cs
- Convert.cs
- DBConcurrencyException.cs
- OdbcUtils.cs
- MemoryRecordBuffer.cs
- Interfaces.cs
- MaskedTextProvider.cs
- RegionData.cs
- SingleAnimation.cs
- XmlComment.cs
- DecimalAnimationUsingKeyFrames.cs
- UnsafeCollabNativeMethods.cs
- BackgroundFormatInfo.cs
- ServicePointManagerElement.cs
- PermissionAttributes.cs
- BinaryParser.cs
- TypeLibraryHelper.cs
- AutomationElementIdentifiers.cs
- TdsValueSetter.cs
- TextDecoration.cs
- ComplexLine.cs
- Qualifier.cs
- URI.cs
- TextTrailingCharacterEllipsis.cs
- HorizontalAlignConverter.cs
- WebBrowserDesigner.cs
- CuspData.cs
- PersistenceTypeAttribute.cs
- LinkedList.cs
- GridViewRowCollection.cs
- returneventsaver.cs
- XmlRawWriter.cs
- ValidationError.cs
- EntityCommandExecutionException.cs
- ScriptingAuthenticationServiceSection.cs
- SqlProcedureAttribute.cs
- ChildDocumentBlock.cs
- Message.cs
- GeneratedCodeAttribute.cs
- DrawTreeNodeEventArgs.cs
- DynamicValueConverter.cs
- FixedTextBuilder.cs
- RadialGradientBrush.cs
- BitConverter.cs
- TypedAsyncResult.cs
- UserMapPath.cs
- MapPathBasedVirtualPathProvider.cs
- FixedSOMPage.cs
- WsdlEndpointConversionContext.cs
- SessionStateModule.cs
- RtfControlWordInfo.cs
- ConfigXmlText.cs
- SQLInt32Storage.cs
- NonPrimarySelectionGlyph.cs
- M3DUtil.cs
- Utils.cs
- HttpCachePolicy.cs
- DateTimePicker.cs
- DetailsViewPagerRow.cs
- SmtpLoginAuthenticationModule.cs
- PartitionResolver.cs
- LineInfo.cs
- ProfileEventArgs.cs
- BrowserInteropHelper.cs
- SqlServices.cs
- ReflectEventDescriptor.cs
- ButtonFlatAdapter.cs
- ActiveXSite.cs
- ScrollItemProviderWrapper.cs
- DefaultSerializationProviderAttribute.cs
- DiscreteKeyFrames.cs
- ZeroOpNode.cs
- figurelengthconverter.cs
- Expressions.cs
- ListControlConvertEventArgs.cs
- IChannel.cs
- ContainerFilterService.cs
- FileDialogCustomPlaces.cs
- SimpleWorkerRequest.cs
- UIAgentInitializationException.cs
- Int32Collection.cs