diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-06-01 18:46:38 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-06-02 16:58:05 -0700 |
commit | a054f5d3eaf59f4f144753d065ea4f73e42acf16 (patch) | |
tree | 49f7b9b002fd7b368c2ba2f94ee2190d78296774 /src | |
parent | 01a526749236d80cf6d51e9a5c171211505b858c (diff) | |
download | DotNetOpenAuth-a054f5d3eaf59f4f144753d065ea4f73e42acf16.zip DotNetOpenAuth-a054f5d3eaf59f4f144753d065ea4f73e42acf16.tar.gz DotNetOpenAuth-a054f5d3eaf59f4f144753d065ea4f73e42acf16.tar.bz2 |
Added XAML source to TypeConfigurationElement.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth/Configuration/TypeConfigurationCollection.cs | 5 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Configuration/TypeConfigurationElement.cs | 33 | ||||
-rw-r--r-- | src/DotNetOpenAuth/DotNetOpenAuth.csproj | 3 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Strings.Designer.cs | 11 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Strings.resx | 3 |
5 files changed, 52 insertions, 3 deletions
diff --git a/src/DotNetOpenAuth/Configuration/TypeConfigurationCollection.cs b/src/DotNetOpenAuth/Configuration/TypeConfigurationCollection.cs index f8813b5..d928c87 100644 --- a/src/DotNetOpenAuth/Configuration/TypeConfigurationCollection.cs +++ b/src/DotNetOpenAuth/Configuration/TypeConfigurationCollection.cs @@ -46,7 +46,7 @@ namespace DotNetOpenAuth.Configuration { internal IEnumerable<T> CreateInstances(bool allowInternals) { Contract.Ensures(Contract.Result<IEnumerable<T>>() != null); return from element in this.Cast<TypeConfigurationElement<T>>() - where element.CustomType != null + where !element.IsEmpty select element.CreateInstance(default(T), allowInternals); } @@ -69,7 +69,8 @@ namespace DotNetOpenAuth.Configuration { /// </returns> protected override object GetElementKey(ConfigurationElement element) { Contract.Assume(element != null); // this should be Contract.Requires in base class. - return ((TypeConfigurationElement<T>)element).TypeName; + TypeConfigurationElement<T> typedElement = (TypeConfigurationElement<T>)element; + return !string.IsNullOrEmpty(typedElement.TypeName) ? typedElement.TypeName : typedElement.XamlSource; } } } diff --git a/src/DotNetOpenAuth/Configuration/TypeConfigurationElement.cs b/src/DotNetOpenAuth/Configuration/TypeConfigurationElement.cs index 022ef40..24113ac 100644 --- a/src/DotNetOpenAuth/Configuration/TypeConfigurationElement.cs +++ b/src/DotNetOpenAuth/Configuration/TypeConfigurationElement.cs @@ -7,7 +7,10 @@ namespace DotNetOpenAuth.Configuration { using System; using System.Configuration; + using System.IO; using System.Reflection; + using System.Web; + using System.Windows.Markup; using DotNetOpenAuth.Messaging; /// <summary> @@ -22,6 +25,11 @@ namespace DotNetOpenAuth.Configuration { private const string CustomTypeConfigName = "type"; /// <summary> + /// The name of the attribute whose value is the path to the XAML file to deserialize to obtain the type. + /// </summary> + private const string XamlReaderSourceConfigName = "xaml"; + + /// <summary> /// Initializes a new instance of the TypeConfigurationElement class. /// </summary> public TypeConfigurationElement() { @@ -39,6 +47,15 @@ namespace DotNetOpenAuth.Configuration { } /// <summary> + /// Gets or sets the path to the XAML file to deserialize to obtain the instance. + /// </summary> + [ConfigurationProperty(XamlReaderSourceConfigName)] + public string XamlSource { + get { return (string)this[XamlReaderSourceConfigName]; } + set { this[XamlReaderSourceConfigName] = value; } + } + + /// <summary> /// Gets the type described in the .config file. /// </summary> public Type CustomType { @@ -46,6 +63,13 @@ namespace DotNetOpenAuth.Configuration { } /// <summary> + /// Gets a value indicating whether this type has no meaningful type to instantiate. + /// </summary> + public bool IsEmpty { + get { return this.CustomType == null && string.IsNullOrEmpty(this.XamlSource); } + } + + /// <summary> /// Creates an instance of the type described in the .config file. /// </summary> /// <param name="defaultValue">The value to return if no type is given in the .config file.</param> @@ -69,6 +93,15 @@ namespace DotNetOpenAuth.Configuration { ErrorUtilities.VerifyArgument((this.CustomType.Attributes & TypeAttributes.Public) != 0, Strings.ConfigurationTypeMustBePublic, this.CustomType.FullName); } return (T)Activator.CreateInstance(this.CustomType); + } else if (!string.IsNullOrEmpty(this.XamlSource)) { + string source = this.XamlSource; + if (source.StartsWith("~/", StringComparison.Ordinal)) { + ErrorUtilities.VerifyHost(HttpContext.Current != null, Strings.ConfigurationXamlReferenceRequiresHttpContext, this.XamlSource); + source = HttpContext.Current.Server.MapPath(source); + } + using (Stream xamlFile = File.OpenRead(source)) { + return (T)XamlReader.Load(xamlFile); + } } else { return defaultValue; } diff --git a/src/DotNetOpenAuth/DotNetOpenAuth.csproj b/src/DotNetOpenAuth/DotNetOpenAuth.csproj index 12b68f2..7a28841 100644 --- a/src/DotNetOpenAuth/DotNetOpenAuth.csproj +++ b/src/DotNetOpenAuth/DotNetOpenAuth.csproj @@ -130,6 +130,9 @@ <SpecificVersion>False</SpecificVersion> <HintPath>..\..\lib\Microsoft.Contracts.dll</HintPath> </Reference> + <Reference Include="PresentationFramework"> + <RequiredTargetFramework>3.0</RequiredTargetFramework> + </Reference> <Reference Include="System" /> <Reference Include="System.configuration" /> <Reference Include="System.Core"> diff --git a/src/DotNetOpenAuth/Strings.Designer.cs b/src/DotNetOpenAuth/Strings.Designer.cs index eea4675..43fec22 100644 --- a/src/DotNetOpenAuth/Strings.Designer.cs +++ b/src/DotNetOpenAuth/Strings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. -// Runtime Version:2.0.50727.3521 +// Runtime Version:2.0.50727.4918 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -68,5 +68,14 @@ namespace DotNetOpenAuth { return ResourceManager.GetString("ConfigurationTypeMustBePublic", resourceCulture); } } + + /// <summary> + /// Looks up a localized string similar to The configuration XAML reference to {0} requires a current HttpContext to resolve.. + /// </summary> + internal static string ConfigurationXamlReferenceRequiresHttpContext { + get { + return ResourceManager.GetString("ConfigurationXamlReferenceRequiresHttpContext", resourceCulture); + } + } } } diff --git a/src/DotNetOpenAuth/Strings.resx b/src/DotNetOpenAuth/Strings.resx index c42347b..bbfa162 100644 --- a/src/DotNetOpenAuth/Strings.resx +++ b/src/DotNetOpenAuth/Strings.resx @@ -120,4 +120,7 @@ <data name="ConfigurationTypeMustBePublic" xml:space="preserve"> <value>The configuration-specified type {0} must be public, and is not.</value> </data> + <data name="ConfigurationXamlReferenceRequiresHttpContext" xml:space="preserve"> + <value>The configuration XAML reference to {0} requires a current HttpContext to resolve.</value> + </data> </root>
\ No newline at end of file |