|
using System;
using System.Windows;
using System.Windows.Markup;
namespace XamlIoc
{
/// <summary>Markup Extension generating objects using <see cref="ObjectFactory"/>.</summary>
[ContentProperty("Key")]
public class ObjectFactoryExtension : MarkupExtension
{
private string key;
/// <summary>Default constructor.</summary>
public ObjectFactoryExtension()
{
}
/// <summary>Constructor using a key.</summary>
public ObjectFactoryExtension(string key)
{
this.key = key;
}
/// <summary>Object key to pass to <see cref="ObjectFactory"/>.</summary>
[ConstructorArgument("key")]
public string Key
{
get { return key; }
set { key = value; }
}
/// <summary>Returns an instance from <see cref="ObjectFactory"/>.</summary>
/// <param name="serviceProvider"></param>
/// <returns></returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Key == null)
{
throw new ArgumentNullException("Key");
}
return ObjectFactory.GetObject(Key);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Vincent-Philippe is a Senior Solution Architect working in Montreal (Quebec, Canada).
His main interests are Windows Azure, .NET Enterprise suite (e.g. SharePoint 2013, Biztalk Server 2010) & the new .NET 4.5 platforms.