Injection in Static Properties. Possible?

Jan 29, 2009 at 10:45 PM
Hi,

I have a class that is responsable for Localization, it has an internal collection to store the cached keywords that it has resolved. I've been using this class as a singleton managed by Unity, and injected in every class that I need it to have Localization.
This afternoon I tried to do an extension method for the string class so that I can get a localized string very easily, but I need the methods to be static and since my Class is not static I need Unity to inject something like LocalizationService.Current, where "Current" is a Static property of the service and its type is ILocalizationService.

Is it possible for Unity to inject static properties?

Thanks!
Jan 30, 2009 at 8:13 AM
Hi,

Yes. I tried it like this.


            IUnityContainer container = new UnityContainer();

            //register the type of the property

            container.RegisterType<ILocalizationService, LocalizationService>();

            //register the class with the static property

            container.RegisterType(typeof(Foo), new InjectionProperty("Current"));

            Foo foo = container.Resolve<Foo>();


 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com