Generic method to map objects of different types

前端 未结 3 1522
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 09:30

I would like to write Generic Method that would map List to new list, similar to JS\'s map method. I would then use this method like this:

var words= new Lis         


        
3条回答
  •  野的像风
    2021-01-14 09:35

    The problem with your code is that Predicate is a delegate that returns a boolean, which you're then trying to add to a List.

    Using a Func is probably what you're looking for.

    That being said, that code smells bad:

    • Converting to object is less than useful
    • Passing a delegate that maps T to an anonymous type won't help - you'll still get an object back which has no useful properties.
    • You probably want to add a TResult generic type parameter to your method, and take a Func as an argument.

    提交回复
    热议问题