Is it possible to pass a strongly-typed class as a parameter of a method in C#?

时间:2015-07-28 22:59:13

标签: c#

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);

4 个答案:

答案 0 :(得分: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?

答案 1 :(得分: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.

答案 2 :(得分:0)

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

ReDim a(objList.Count - 1) As String
Dim i As Long

For i = 0 To objList.Count - 1
    a(i) = objList.GetKey(i)
Next

' Combine strings into the format: {"string1", "string2", "stringN"}
Sheet1.Range("A1").Value = "{""" & Join(a, """, """) & """}"

.. and calling the above code as

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));
}

... 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.

CreatePageModel<TitleClassName, KeywordClassName... etc

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

// 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
    }
}

答案 3 :(得分: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 :)

let maxXValue: UInt32 = 200
let maxYValue: UInt32 = 200

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

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);
            //       ....
        }

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