Monday 15 April 2013

generics - How do I write a C# method to return a Func based in an enum parameter? -


I have a set of methods, each of which has returned an obscene collection for different types of tee, and writing A factory method that gives these methods, depending on the value sent to them. For example, suppose I have the following highly realistic methods ...

  Public supervision & lt; Gribble & gt; GetGribbles () {New ObservableCollection & lt; Gribble & gt; (); } Public Supervision Collection & lt; Gribulator & gt; Generate GRUBUTERS () {New Observation Collection & Lt; Calculator & gt; (); } Public Observable Collection & lt; Knepple & gt; MakeKnepples () {New Observational Collection & lt; Knepple & gt; (); }   

... and the following enum ...

  public enum ListNames {Gribbles, Gribulators, Knepple}   < P> Then I want to be able to do this ...  
  Funk & lt; OverviewCollection & lt; Gribble & gt; & Gt; Some Grabbles = McFunk & lt; Gribble & gt; (ListNames.Gribbles);   

The reason for this is that I have a signature supporting class like this ...

  Public Zero GetList & lt; T & gt; (ListNames listName, Func & lt; ObservableCollection 
Service Call, Action & There is a list, and the handleList callback method is currently, wherever this method is called, the code has to pass the actual function which receives the list.

However, this is proving to be a bad design choice, because it means that if both of the two code codes call GetList for a particular list, they can pass in two separate service calls. Which can cause incompatible results. I want to finish the service call in the MakeFunc method, which will be called from GetList, which means that the code using GetList never sees the actual service call, and there is nothing wrong with it.

I have tried to do some things, but whoever compiles, is struggling to achieve something. The closest I have found is the following ...

  Public Functions & amp; Nbsp; Overview section & lt; T & gt; & Gt; MakeFunc & lt; T & gt; (ListNames listName) {switch (listName) {case ListNames.Gribbles: GetGribbles; Case list names Brittle: return generator; Case list name Note: Return the campbells; Default: throwing new argument options ("unknown list name"); }}   

... but with the " Getgribbles () incorrect return type " and tooltip lines are provided with compiler errors in the Makefunk method on GetGribbles Hover my mouse

'ObservableCollection & lt; T & gt; GetGribbles () ' signature

What should I do? If so, someone is able to convince me that what I did wrong?

I'm using C # 4.0 in Visual Studio 2010 if it makes a difference.

if Gribble , Gribulator , and < Code> Knepple class share a common base type or to implement a common interface, you can:

  public function & lt; OverviewCollection & lt; ICommonInterface & gt; & Gt; MakeFunc (ListNames listName) {...}   

If they do not have a common base type or interface, then you have to:

  public Fax & lt; Ikeing & gt; MakeFunc (ListNames listName) {...}   

Which is, of course, not a very good solution. Note: You can change the Icoding to INotifyCollectionChanged < / Code> or whatever general genetic interface is required.

In both cases, to specify the type of data to the caller (even if the caller enum value also specifies). So, you can get rid of these too, and can go for something like this:

  Public functions & lt; Overview section & lt; T & gt; & Gt; McFunk & lt; T & gt; () {If (typef (t) == typef (grosbeable) returns () => Getgreebles (as Observeable Collection & lt; T & gt; If (typef (t) == typef (grikulator) returns () => Generated as a GeneratriGrilector () collection & lt; T & gt; If (typef (t) == typef (napell)) returns () => Viewable collections as McKenpals () & lt; T & gt; Except new logic exception ("unknown list name"); }   

Then the caller can just write:

  var gribblesFunc = MakeFunc & lt; Gribble & gt; ();    

No comments:

Post a Comment