Reputation: 1659
We have a WPF application using Prism (7.2.0.1422) and Unity as the DI container. I have the following class where I am attempting to use Unity Property injection
public class LocalizedDescriptionAttribute : DescriptionAttribute
{
[Dependency]
IStringResource _stringResource { get; set; }
string _resourceKey;
public LocalizedDescriptionAttribute(string resourceKey)
{
_resourceKey = resourceKey;
}
public override string Description
{
get
{
string description = _stringResource.GetString(_resourceKey);
return string.IsNullOrWhiteSpace(description) ? string.Format("[[{ 0}]]", _resourceKey) : description;
}
}
}
_stringResource is always null. I have registered the type as a singleton like this in RegisterTypes
containerRegistry.RegisterSingleton<IStringResource, StringResource>();
Anyone any ideas. Thanks
Upvotes: 1
Views: 141
Reputation: 10863
Based on the name of the class, I assume it's an actual attribute? That cannot have anything injected by Unity
, because the container can only inject into instances it creates itself.
You can, however, use the container from the attribute's code through a detour: the CommonServiceLocator
. That's a static class that you only use if you have to, and this may be one of the rare cases where it's a good idea. You can use it to resolve the IStringResource
from the container at runtime.
Upvotes: 1