-1

I am attempting to create an extension method for type object that will create a new way of initializing an object and setting its properties. I have however run into a few problems. The extension looks like so:

public static class ObjectExtension
{
    public static TResult Select<TSource,TResult>(this TSource obj, Func<TSource, TResult> selector)
    {

        return selector.Invoke(obj);
    }
}

And this is the kind of result that I'm looking for:

return new Product().Select(p => p.Category = new Category(), p.Name = "something");

As you can likely tell perhaps my understanding is too little. I'm having trouble grasping the idea of Func and how to use them. My question is if this is possible and how/what direction do I need to go to accomplish this?

My inspiration came from this: Here

With the following code:

IEnumerable<int> squares =
Enumerable.Range(1, 10).Select(x => x * x);

I want to do something like that except not from a list and have the ability to set multiple properties.

Chris
  • 826
  • 10
  • 26
  • 4
    That delegate isn't a predicate. A predicate is a function that returns a boolean value. That's a selector. The method also shouldn't be called `Where`, because you're not doing any kind of filtering, you're projecting an item. In LINQ terminology that would be `Select`. As far as calling it; you'd call it just like you would a LINQ `Select` method. If you had a sequence, how would you `Select` it? Use the same lambda. All that said, there's no reason for a method to do this; you can just perform the operation from the lambda on the object directly; the method isn't getting you anything. – Servy May 26 '17 at 15:44
  • 4
    Something like `return new Product { Category = new Category(), Name = "something"};` would be cool. The good news is, it was added in C# 3.0 IIRC. – 15ee8f99-57ff-4f92-890c-b56153 May 26 '17 at 15:46
  • @Servy That's a good observation. I'll make those changes – Chris May 26 '17 at 15:47
  • @EdPlunkett I am aware of that. It's more so for proof of concept. – Chris May 26 '17 at 15:49
  • What's more what for what proof of what concept? I think the object initializer concept is proven by now. – 15ee8f99-57ff-4f92-890c-b56153 May 26 '17 at 15:50
  • *"not from a list"* -- Setting properties on one object doesn't have much to do with Linq. – 15ee8f99-57ff-4f92-890c-b56153 May 26 '17 at 15:54
  • There is fundamental difference between the "inspired" LINQ method and yours - the former is receiving one type and returning another type (usually called `Map`), while yours is unclear what is supposed to do. – Ivan Stoev May 26 '17 at 15:54
  • This would be something like what you want: `public static T Do(this T t, Action act) { act(t); return t; }` – 15ee8f99-57ff-4f92-890c-b56153 May 26 '17 at 16:03
  • 1
    Call like so: `new Product().Do(p => { p.Name = "blah"; p.Category = new Category(); });` -- the func is just a lambda. But I can't see that it adds anything to the object initializer syntax I mentioned. I can't see that it adds anything much to just setting the properties, frankly. Just adds some more punctuation marks. – 15ee8f99-57ff-4f92-890c-b56153 May 26 '17 at 16:04

1 Answers1

1

This will fix your code:

    return new Product().Select(p => 
             { p.Category = new Category(); p.Name = "something"; return p;});

Your problem was that the "comma as function parameter separator" took precedence over "comma as a weird way of join expressions in C that doesn't work in C# anyway". You need the full block lambda expression there.

But as Ed Plunkett pointed out in his comment, this can be simplified down to:

return new Product {Category = new Category(), Name = "something"};
James Curran
  • 101,701
  • 37
  • 181
  • 258