开发者

generic call of class in c#

开发者 https://www.devze.com 2023-02-05 22:03 出处:网络
SlovenianSearchQueryManager.ApplySloveniaQueries(rawQuery, page, pageSize, orderBy, out count); is it possible to get SlovenianSearchQueryManager class by concatinating HttpContext.Current.Session[\
SlovenianSearchQueryManager.ApplySloveniaQueries(rawQuery, page, pageSize, orderBy, out count);

is it possible to get SlovenianSearchQueryManager class by concatinating HttpContext.Current.Session["lang_name"] and "SearchQueryManager";

HttpContext.Current.Session["lang_开发者_如何学Pythonname"] is Slovenian so together is SlovenianSearchQueryManager

next time

HttpContext.Current.Session["lang_name"] is German so together is GermanSearchQueryManager

i try to make this generic instead of making

if (HttpContext.Current.Session["lang_name"] == "Slovenian)
{ 
SlovenianSearchQueryManager.ApplySloveniaQueries(rawQuery, page, pageSize, orderBy, out count);
} 
else ...

i want to make something like

(HttpContext.Current.Session["lang_name"] + "SearchQueryManager").ApplySloveniaQueries(...

is this even possible to make something like this and the type SlovenianSearchQueryManager for example exists, it's in a different class library.


You should make an ISearchQueryManager interface with implementations for each language, then use a Dictionary<string, ISearchQueryManager> to get the implementation for the desired language.


Use Type.GetType and Activator.CreateInstance:

var queryManagerType = Type.GetType(HttpContext.Current.Session["lang_name"] + "SearchQueryManager");
var queryManager = Activator.CreateInstance(queryManagerType);

Activator.CreateInstance returns an object. You might want to define an interface that you query managers will need to implement.

public interface ISearchQueryManager {
    void ApplyQueries(/* add parameters here */);
}

Then you can cast the object returned by CreateInstance to the interface:

var queryManagerType = Type.GetType(HttpContext.Current.Session["lang_name"] + "SearchQueryManager");
var queryManager = (ISearchQueryManager)Activator.CreateInstance(queryManagerType);
queryManager.ApplyQueries(rawQuery, page, pageSize, orderBy, out count);


I would recommend making two changes.

First, make all of your "SearchQueryManager" instances derive from a common interface (or base class), such as ISearchQueryManager.

Second, make a Dictionary<string, ISearchQueryManager>, and use it to lookup the appropriate one based on your type.

You could then do:

 ISearchQueryManager manager;
 if (!managers.TryGetValue(HttpContext.Current.Session["lang_name"], out manager))
 {
     // Unknown language - handle this!
 }    
 else
 {
       manager.ApplyQuery(...);
 }


You're looking at the factory pattern. Define an interface, like IQueries with the queries methods you need. Then define a factory class that takes your language and returns an IQueries. For slovenia, return your SlovenienSearchQueryManager. Etc for other languages.


It sounds like you're trying to work around creating a factory pattern, which would, IMO, be the correct way to solve this problem.

http://aspalliance.com/809


I would investigate using a factory class to create your query managers.

Pseudo-code:

var factory= new QueryManagerFactory();
var queryManager = factory.Create("Slovenian");

In your factory create method return the type of query manager you want based upon the input.

0

精彩评论

暂无评论...
验证码 换一张
取 消