0

I have the following code. I would like to wrap it in a method, where I could pass Resources.Home or Resources.Contact or Resources.Privacy, etc. into it where I currently have hard-coded references to Resources.Home. Each of these is a reference to a strongly-typed class. Is this possible?

Localization localization = new Localization();
FrameworkModel model = new FrameworkModel();
model.Page = new PageModel();
model.Page.Scripts = new PageModel.PageScripts();
model.Page.TwoLetterISOLanguageName = ((Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName) != null ? Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName : "en");
model.Page.CurrentUICultureName = ((Thread.CurrentThread.CurrentUICulture.Name) != null ? Thread.CurrentThread.CurrentUICulture.Name : "en-us").ToLower();
model.Page.Title = localization.LocalizeText(Resources.Home.Title);
model.Page.Keywords = localization.LocalizeText(Resources.Home.Keywords);
model.Page.Description = localization.LocalizeText(Resources.Home.Description);
model.Page.RSS = localization.LocalizeText(Resources.Home.RSS);
model.Page.Scripts.Header = localization.LocalizeText(Resources.Home.ScriptsHeader);
model.Page.Scripts.Footer = localization.LocalizeText(Resources.Home.ScriptsFooter);
model.Page.Body = localization.LocalizeText(Resources.Home.Body);
Blorgbeard
104k50 gold badges237 silver badges276 bronze badges
asked Jul 28, 2015 at 22:59
5
  • What is the type of Resources.Home? Commented Jul 28, 2015 at 23:09
  • A strongly-typed class. Commented Jul 28, 2015 at 23:10
  • 1
    That doesn't mean anything. Classes are types, they can't themselves be strongly-typed, or weakly-typed. Commented Jul 28, 2015 at 23:12
  • @siride It seems Microsoft thinks otherwise. The Intellisense specifically says "A strongly-typed resource class..." Commented Jul 29, 2015 at 5:31
  • You're misinterpreting it. The class provides strongly-typed properties that don't require casting or parsing of strings/streams. Commented Jul 29, 2015 at 16:21

4 Answers 4

1

I am a little confused by the question. If you have instantiated objects of the correct type then it is standard C#, right:

void YourFunction(TypeOfResourceHome home) {
 Localization localization = new Localization();
 FrameworkModel model = new FrameworkModel();
 model.Page = new PageModel();
 model.Page.Scripts = new PageModel.PageScripts();
 ...
 model.Page.Title = localization.LocalizeText(home.Title);
 model.Page.Keywords = localization.LocalizeText(home.Keywords);
 model.Page.Description = localization.LocalizeText(home.Description);
 model.Page.RSS = localization.LocalizeText(home.RSS);
 model.Page.Scripts.Header = localization.LocalizeText(home.ScriptsHeader);
 model.Page.Scripts.Footer = localization.LocalizeText(home.ScriptsFooter);
 model.Page.Body = localization.LocalizeText(home.Body);
}

Hence, I assume you want to pass in the "class" not an object and the access static members? If this is your plan then I would expect it can be done with reflection but it will be messy and perhaps you would be better to consider actually creating an instance of the class to achieve the same outcome?

answered Jul 28, 2015 at 23:11
Sign up to request clarification or add additional context in comments.

Comments

0

You mention that:

Each of these is a reference to a strongly-typed class

I would interpret that as

each of these is a separate (concrete) instance of a specific type of class

If my interpretation is correct, then you simply need to redefine your method to be

public void Method([specific type of your home/contact/privacy object] settings)
{
 model.Page.Title = localization.LocalizeText(settings.Title);
}

If the home/contact/privacy object instances are all of a different type, then you should refactor them if possible to inherit from a common base type - if that is not possible then use overloading to create a method that takes each specific type.

answered Jul 28, 2015 at 23:39

Comments

0

Yes you can do what you're asking... as to whether or not that is a good code design is open to debate ;)

void CreatePageModel<TTitle, TKeyword, TDescription, TRSS, TScriptsHeader, TScriptsFooter, TBody>()
{
 var model = new FrameworkModel();
 ...
 model.Page.Title = localization.LocalizeText(typeof(TTitle));
 model.Page.Keywords = localization.LocalizeText(typeof(TKeywords));
 model.Page.Description = localization.LocalizeText(typeof(TDescription));
 model.Page.RSS = localization.LocalizeText(typeof(TRSS));
 model.Page.Scripts.Header = localization.LocalizeText(typeof(TScriptsHeader));
 model.Page.Scripts.Footer = localization.LocalizeText(typeof(TScriptsFooter));
 model.Page.Body = localization.LocalizeText(typeof(TBody));
}

.. and calling the above code as

CreatePageModel<TitleClassName, KeywordClassName... etc

... but this is horrible, and I hope the reasons are obvious (if they aren't then I can elaborate :)

If I may be presumtuous enough to read into the problem I think you're trying to solve; it seems like you're looking for a nice strongly typed method of handling localisation where you can take a Razor syntax approach of referencing a resource string by referencing a property inside the Resources static model. This would be a fantastic way to deal with localisation as it is resilient to future breakages as code changes/gets refactored.

// NOTE: This could/should be automatically generated from the resources file
public class Resources 
{
 public class HomeResources
 {
 public string Title { get; set; }
 public string Keywords { get; set; }
 public string Description { get; set; }
 public string RSS { get; set; }
 public string ScriptsHeader { get; set; }
 public string ScriptsFooter { get; set; }
 public string Body { get; set; }
 }
 public HomeResources Home { get; set; }
 ...
 // Other categorisations of resources
 ...
}
public class Localisation
{
 private Resources _resources;
 public Localisation(Resources resources)
 {
 _resources = resources;
 }
 public LocaliseText<TProperty>(Expression<Func<Resources, TProperty>> expr)
 {
 // Borrow functionality from MVC to get the string representation
 // of the expression property reference e.g.
 // r => r.Home.Title => "Home.Title"
 var propertyName = ExpressionHelper.GetExpressionText(expr);
 //.. Do your string resource lookup
 }
}

Now you have the strong typed code to setup your localised page model as:

void CreatePageModel()
{
 var localization = new Localization();
 var model = new FrameworkModel();
 ...
 model.Page.Title = localization.LocalizeText(r => r.Home.Title);
 model.Page.Keywords = localization.LocalizeText(r => r.Home.Keywords);
 model.Page.Description = localization.LocalizeText(r => r.Home.Description);
 model.Page.RSS = localization.LocalizeText(r => r.Home.RSS);
 model.Page.Scripts.Header = localization.LocalizeText(r => r.Home.ScriptsHeader);
 model.Page.Scripts.Footer = localization.LocalizeText(r => r.Home.ScriptsFooter);
 model.Page.Body = localization.LocalizeText(r => r.Home.Body);
}
answered Jul 29, 2015 at 0:16

Comments

0

If I am correctly understanding your Question , you want to pass strongly typed class object with dynamic values as a parameter of method etc. You should be easily able to pass it as parameter :)

void YourFunction(TypeOfResourceHome Resources)
 {
 Localization localization = new Localization();
 FrameworkModel model = new FrameworkModel();
 model.Page = new PageModel();
 model.Page.Scripts = new PageModel.PageScripts();
 // .....
 model.Page.Title = localization.LocalizeText(Resources.Home.Title);
 model.Page.Keywords = localization.LocalizeText(Resources.Home.Keywords);
 // .....
 model.Page.Body = localization.LocalizeText(Resources.Home.Body);
 // ....
 }

However if I would be doing this , I would pass the class to constructor of FrameworkModel() class, i.e you can try it like this in your FrameworkModel class

public FrameworkModel(TypeOfResourceHome resources)
{
 page= new PageModel();
 page.Title = localization.LocalizeText(resources.home.Title);
 ///....
}

it will help me to reuse and generalize my data so that I don't have to right and assign values every . thanks hope it will help. Regards Fahad

answered Jul 29, 2015 at 0:16

5 Comments

I don't have TypeOfResourceHome anywhere in my code? :-(
this was for example. You can use your Page Model class directly.
I guess, thats the crux of my question. I can't figure out what I should use where you have "TypeOfResourceHome" that would still allow it to be dynamic.
ok. Can you briefly explain the point when you are generating that dynamic Resources ? like you showed in your code model.Page.Title = localization.LocalizeText(Resources.Home.Title);
All of the items starting with Resources. (i.e. Resources.Home, Resources.Privacy, etc.) are auto-generated by Visual Studio when changing the "Access Modifier" from "No code generation" to "Public".

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.