Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Compilation / AppSettingsExpressionBuilder.cs / 1305376 / 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)] 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)] 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
- PathSegment.cs
- DataControlExtensions.cs
- ZipIOFileItemStream.cs
- StatementContext.cs
- MetadataPropertyCollection.cs
- XmlnsCompatibleWithAttribute.cs
- ChannelManagerBase.cs
- RSAOAEPKeyExchangeDeformatter.cs
- SQLDouble.cs
- MissingMethodException.cs
- ReflectionUtil.cs
- ServiceProviders.cs
- LineUtil.cs
- MembershipPasswordException.cs
- MergeLocalizationDirectives.cs
- nulltextnavigator.cs
- ControlEvent.cs
- PackageDigitalSignatureManager.cs
- Debugger.cs
- __Error.cs
- ElementsClipboardData.cs
- SkipQueryOptionExpression.cs
- TiffBitmapEncoder.cs
- DynamicValueConverter.cs
- XmlArrayAttribute.cs
- Pair.cs
- GeneralTransform.cs
- FieldTemplateFactory.cs
- DoubleAnimationBase.cs
- DataGridViewRow.cs
- ReflectionUtil.cs
- ConfigUtil.cs
- namescope.cs
- DetailsViewUpdateEventArgs.cs
- ProvidersHelper.cs
- StrokeDescriptor.cs
- EntityContainer.cs
- InstanceContextManager.cs
- StrongNameUtility.cs
- DetailsViewRow.cs
- FragmentQueryKB.cs
- DispatcherEventArgs.cs
- PlainXmlDeserializer.cs
- ScriptingProfileServiceSection.cs
- TabletDevice.cs
- QueueAccessMode.cs
- WindowsStartMenu.cs
- Soap.cs
- DiffuseMaterial.cs
- AmbientLight.cs
- ImageAutomationPeer.cs
- PinnedBufferMemoryStream.cs
- SchemaObjectWriter.cs
- _Connection.cs
- XPathScanner.cs
- SqlErrorCollection.cs
- ThaiBuddhistCalendar.cs
- IpcClientChannel.cs
- XmlUnspecifiedAttribute.cs
- SpAudioStreamWrapper.cs
- AutomationIdentifier.cs
- WebPartCollection.cs
- MetadataSerializer.cs
- AuthenticationModulesSection.cs
- LineSegment.cs
- GeneralTransform3DTo2DTo3D.cs
- DataGridTable.cs
- HttpCacheVaryByContentEncodings.cs
- RijndaelCryptoServiceProvider.cs
- DataService.cs
- HtmlTableRow.cs
- ConstraintCollection.cs
- ContainerControlDesigner.cs
- CryptoKeySecurity.cs
- RotateTransform3D.cs
- WmlPageAdapter.cs
- WebBrowsableAttribute.cs
- CaseCqlBlock.cs
- CommunicationObject.cs
- EntityDesignerUtils.cs
- StringResourceManager.cs
- ChangeInterceptorAttribute.cs
- TrackBar.cs
- ServerType.cs
- SkipQueryOptionExpression.cs
- XmlCompatibilityReader.cs
- CompressedStack.cs
- SmtpDigestAuthenticationModule.cs
- ColorContextHelper.cs
- CustomTrackingQuery.cs
- SchemaAttDef.cs
- NCryptSafeHandles.cs
- ImageListDesigner.cs
- CompilerGlobalScopeAttribute.cs
- WebBrowserProgressChangedEventHandler.cs
- PersistenceMetadataNamespace.cs
- COAUTHIDENTITY.cs
- WsiProfilesElement.cs
- RectangleGeometry.cs
- X509RawDataKeyIdentifierClause.cs