How can I create a generic method to convert lists of objects when conversion methods for single objects exist?

Yes if you modify your Create function to take a mapping between domain and views: public static List Create(IEnumerable values, Func mapFunc) { return values. Select(v => mapFunc(v)).ToList(); } You can then do the mapping using your ViewFactory methods: IEnumerable domAs = //whatever var aViews = Create(domAs, a => ViewFactory. Create(a)).

Thanks that should be exactly what I need. – Jedidja Mar 2 '10 at 16:30 Actually could I make the Func implicit? Given that it's always called Create?

I'd rather not have to pass it in every time. – Jedidja Mar 2 '10 at 16:32.

You should be able to use the ConvertAll and ToList Functions ConvertAll allows you to project your changes which would return an IEnumerable of T in your case and then a ToList would convert from the IEnumerable to the List So Stolen from the MSDN article List lpf = new List(); lpf. Add(new PointF(27.8F, 32.62F)); lpf. Add(new PointF(99.3F, 147.273F)); lpf.

Add(new PointF(7.5F, 1412.2F)); Console.WriteLine(); foreach( PointF p in lpf ) { Console. WriteLine(p); } List lp = lpf. ConvertAll( new Converter(PointFToPoint)); Console.WriteLine(); foreach( Point p in lp ) { Console.

WriteLine(p); } you should be able to go from U to T as they have gone from PointF to Point you may not even need the ToList.

You shouldn't need a custom method to create the lists. You can just do the following: List domains = GetDomainList(); List views = domains. ConvertAll(x => ViewFactory.

Create(x)).

Hmm, it looks to me like you need a more object-oriented model rather than generics: public abstract class Domain { public abstract View CreateView(); } public abstract class View { } public class DomainA : Domain { public int MyProperty { get; set; } public override View CreateView() { return new ViewA() { MyProperty = value. MyProperty }; } } public class ViewA : View { public int MyProperty { get; set; } } public class DomainB : Domain { public string MyProperty { get; set; } public override View CreateView() { return new ViewB() { MyProperty = value. MyProperty }; } } public class ViewB : View { public string MyProperty { get; set; } }.

I'd rather not mess with the class model. – Jedidja Mar 2 '10 at 16:30 You do get some advantages though, in case they ever have more 'custom' functionality. Or if you ever want to create a list of views, for example.

– Fiona Holder Mar 2 '10 at 17:01 @Jedidja It sounds like your trying to use a generic-programming hammer where an object-oriented-programming ratchet would be better suited. You have conceptually related to items; have both your Domain classes implement interface IViewable { T CreateView(); } and move on. – Robert Davis Mar 2 '10 at 17:01 There's no way the Domain can ever know about the View.

The Domain is defined in its own stand-alone assembly. And I'd rather not have the View know about the Domain either. That's why I wanted a third party to take of doing the conversion :) – Jedidja Mar 2 '10 at 21:07 I realise I didn't mention that in the original question; would have definitely given for a different response.

– Jedidja Mar 2 '10 at 21:08.

You can't create a generic method like you are attempting to do. In your example, there are only a few ways to set T and U which would work (e.g. Create(domains) would be meaningful, but Create(ints) wouldn't). The caller of a generic method decides at which types to call it, so generic methods must work for any type parameters, which your method does not.

Others have given several good suggestions for how to achieve the result you want.

Ultimately it looks like you want is a little thing called multiple dispatch. Which unfortunately for all of us is not implemented in C# 3. If you want to have your program automagically figure out which method to call, you're going to have to either use reflection or a virtual function using single dispatch.

Interface IViewFactory { TView Create(TDomain domain); } class ViewFactoryA : IViewFactory { ... } class ViewFactoryB : IViewFactory { ... } now your initialization code just needs to create the correct factory object.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions