Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- FullTextLine.cs
- UpdateException.cs
- GeometryModel3D.cs
- FontFamilyIdentifier.cs
- hresults.cs
- SizeChangedEventArgs.cs
- WindowsListViewScroll.cs
- OleDbConnectionInternal.cs
- Errors.cs
- DropDownButton.cs
- util.cs
- DivideByZeroException.cs
- PcmConverter.cs
- CommandBindingCollection.cs
- ProxyHwnd.cs
- ControlParser.cs
- BrowserDefinition.cs
- PropertyTab.cs
- LogPolicy.cs
- NativeMethodsOther.cs
- DataControlCommands.cs
- RegexInterpreter.cs
- ButtonBase.cs
- XmlTextReaderImpl.cs
- HttpRequestWrapper.cs
- ConversionContext.cs
- ViewUtilities.cs
- ApplicationSettingsBase.cs
- HttpChannelFactory.cs
- XPathSelfQuery.cs
- FixedTextSelectionProcessor.cs
- CodeCommentStatement.cs
- connectionpool.cs
- UnmanagedMarshal.cs
- XmlDeclaration.cs
- XmlDataLoader.cs
- IPAddressCollection.cs
- RoleGroup.cs
- UriSection.cs
- Int16Animation.cs
- GuidelineCollection.cs
- WeakReferenceKey.cs
- FormViewInsertedEventArgs.cs
- ParameterCollection.cs
- LineMetrics.cs
- ConfigurationManagerInternalFactory.cs
- EdmComplexTypeAttribute.cs
- TdsParserStaticMethods.cs
- X509SubjectKeyIdentifierClause.cs
- ComboBoxDesigner.cs
- DataControlFieldCell.cs
- StickyNoteHelper.cs
- CompilerError.cs
- BmpBitmapEncoder.cs
- SqlInternalConnection.cs
- XmlAttributeOverrides.cs
- Section.cs
- TemplateInstanceAttribute.cs
- WebPartPersonalization.cs
- MenuItemBinding.cs
- ObjectConverter.cs
- SHA512Cng.cs
- Signature.cs
- TouchEventArgs.cs
- ByeMessageCD1.cs
- AstTree.cs
- NegatedCellConstant.cs
- TextEffectCollection.cs
- BamlRecordWriter.cs
- BinaryObjectInfo.cs
- PassportAuthenticationEventArgs.cs
- OdbcRowUpdatingEvent.cs
- BitmapEffect.cs
- DataControlReference.cs
- StyleCollection.cs
- XamlReaderHelper.cs
- SQLDateTimeStorage.cs
- SafeFileMappingHandle.cs
- PrintPreviewDialog.cs
- PropVariant.cs
- GridViewUpdateEventArgs.cs
- basecomparevalidator.cs
- TextRangeProviderWrapper.cs
- GZipDecoder.cs
- ISessionStateStore.cs
- XmlSchemaComplexContent.cs
- DataGridHelper.cs
- AbandonedMutexException.cs
- CacheAxisQuery.cs
- RegexCompiler.cs
- TemporaryBitmapFile.cs
- lengthconverter.cs
- OletxTransactionHeader.cs
- EventLogEntryCollection.cs
- Closure.cs
- ISO2022Encoding.cs
- SplineKeyFrames.cs
- QueryResult.cs
- RelationshipConverter.cs
- GridViewCommandEventArgs.cs