Skip to content
This repository was archived by the owner on Dec 18, 2017. It is now read-only.
This repository was archived by the owner on Dec 18, 2017. It is now read-only.

JsonLocalization: MvxTextProviderBuilder virtual call in constructor #137

@sgravel-dev

Description

@sgravel-dev

In it's constructor, MvxTextProviderBuilder calls its LoadResources method which refers to the virtual property ResourceFiles. This is an issue as we might access stuff that isn't completely initialized yet, as we well know.

My specific use-case is that I'm passing my default text type key as a parameter to my text provider class. I also use reflection to find all ViewModels and create the mappings for those classes. Of course, my default type key is null when trying to setup the dictionary. Ideally, the initial loading of text resources would be deferred.

        public TextProviderBuilder(string textFolder, string defaultTypeKey)
            : base(Mvx.Resolve<ResourceLocator>().ResourcesNamespace, textFolder, new MvxEmbeddedJsonDictionaryTextProvider(false))
        {
            _DefaultTypeKey = defaultTypeKey;
        }

        protected override IDictionary<string, string> ResourceFiles
        {
            get
            {
                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                IEnumerable<TypeInfo> localizedViewModelTypes = Mvx.Resolve<ResourceLocator>()
                                                                   .ResourcesAssembly.DefinedTypes.Where(
                                                                       t => t.IsSubclassOf(typeof(MvxLocalizedViewModel)));

                dictionary.Add(_DefaultTypeKey, _DefaultTypeKey);

                foreach(TypeInfo type in localizedViewModelTypes)
                {
                    string typeKey = type.FullName;

                    dictionary.Add(typeKey, typeKey);
                }

                return dictionary;
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions