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
- MetadataArtifactLoader.cs
- ListMarkerLine.cs
- SqlGatherConsumedAliases.cs
- HMACSHA256.cs
- ListenerPerfCounters.cs
- SoapElementAttribute.cs
- MetadataArtifactLoaderCompositeFile.cs
- DesignerView.xaml.cs
- Mouse.cs
- PeerNameRecordCollection.cs
- TypeFieldSchema.cs
- WebPartVerbCollection.cs
- MobileErrorInfo.cs
- StreamingContext.cs
- ExpressionConverter.cs
- NamespaceEmitter.cs
- InlineUIContainer.cs
- DataIdProcessor.cs
- RightsManagementInformation.cs
- TypefaceCollection.cs
- GridViewItemAutomationPeer.cs
- StrongNameMembershipCondition.cs
- Item.cs
- DiscardableAttribute.cs
- WebServiceClientProxyGenerator.cs
- ArcSegment.cs
- Literal.cs
- ServiceDescriptionSerializer.cs
- DesignerHierarchicalDataSourceView.cs
- SortDescriptionCollection.cs
- OutputCacheSettingsSection.cs
- VirtualPathProvider.cs
- FullTextLine.cs
- HttpAsyncResult.cs
- ShaperBuffers.cs
- TagPrefixInfo.cs
- DbConnectionOptions.cs
- ConfigurationLocation.cs
- WebPartMinimizeVerb.cs
- MetabaseReader.cs
- QilInvokeLateBound.cs
- SqlCacheDependencyDatabaseCollection.cs
- PropertyValueChangedEvent.cs
- IntellisenseTextBox.designer.cs
- StringValidatorAttribute.cs
- ElapsedEventArgs.cs
- ColumnHeaderConverter.cs
- DataTableNewRowEvent.cs
- GeneralTransform3D.cs
- RelationshipNavigation.cs
- BindingExpressionUncommonField.cs
- RootProjectionNode.cs
- HybridObjectCache.cs
- ColumnResizeUndoUnit.cs
- CompilerTypeWithParams.cs
- FormattedTextSymbols.cs
- TextOutput.cs
- Switch.cs
- FrameworkContextData.cs
- documentsequencetextview.cs
- OleDbConnection.cs
- CorrelationKey.cs
- SiteMapHierarchicalDataSourceView.cs
- CodeGeneratorOptions.cs
- SystemIPInterfaceStatistics.cs
- RegexNode.cs
- SimplePropertyEntry.cs
- MimeXmlImporter.cs
- SystemUnicastIPAddressInformation.cs
- GeometryHitTestResult.cs
- SimpleWebHandlerParser.cs
- VerbConverter.cs
- CollectionViewGroupRoot.cs
- DSACryptoServiceProvider.cs
- X509CertificateInitiatorServiceCredential.cs
- ClaimTypes.cs
- XmlNamespaceManager.cs
- recordstate.cs
- GridViewColumn.cs
- ValidationRule.cs
- EvidenceBase.cs
- SmtpLoginAuthenticationModule.cs
- SafeHandle.cs
- DesignerDataRelationship.cs
- ExtensionQuery.cs
- XmlDictionaryWriter.cs
- Types.cs
- TextRangeEditLists.cs
- RegionInfo.cs
- ImageList.cs
- SamlAttributeStatement.cs
- SchemaNamespaceManager.cs
- MessagePartProtectionMode.cs
- UrlPath.cs
- SqlClientWrapperSmiStream.cs
- DesignerLabelAdapter.cs
- Bezier.cs
- PasswordBoxAutomationPeer.cs
- UpDownBase.cs
- Compress.cs