Injection in Static Properties. Possible?

Jan 29, 2009 at 11:45 PM

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?

Jan 30, 2009 at 9:13 AM

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.