Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension method that extends T - bad practice?

Tags:

I've read that it is usually bad practice to extend System.Object, which I do agree with.

I am curious, however, if the following would be considered a useful extension method, or is it still bad practice?

It is similar to extending System.Object but not exactly,

    public static R InvokeFunc<T, R>(this T input, Func<T, R> func)
    {
        return func.Invoke(input);
    }

This essentially allows any object to invoke any function that takes that object as a parameter and returns R, whether that function belongs to the object or not. I think this could facilitate some interesting 'inversion of control', but not sure about it overall.

Thoughts?

like image 427
Sean Thoman Avatar asked Apr 14 '11 22:04

Sean Thoman


People also ask

Are extension methods good practice?

Adding extension methods to any type is a great way to improve productivity and simplify code. You should do this wherever it feels beneficial to you, without worrying about any of these details.

When would you not use an extension method?

When you're not sure which Type is the one to extend, don't use extension methods. For example, to build a house from brick and mortar I can extend brick with brick.

What is correct extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What are extension methods explain with an example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.


1 Answers

Well there are really two points here:

1) Whether it is a good idea to create an extension method with this T so it will be applied to all types?

2) Whether the particular extension method described is useful?

For the 1st question the answer is sometimes but depends on the context. You can have an extension method apply to all classes just like linq does ensuring that you pick an appropriate namespace. I would think creating this type of extension method within the System namespace a bad idea but if it were more targeted then perhaps it would be useful.

For the 2nd since the invoke is immediate then the choice of syntax is as follows

    int res = other.InvokeFunc<Other, int>(Callback);

    var res2 = (new Func<Other, int>(Callback))(other);

    var res3 = Callback(other);

Looking at that then a simple call to the method passing the instance in is more natural and typical, however if your extension method becomes more elaborate then I go back to my first point on that it depends on the context (which could help with encapsulation).

like image 139
aqwert Avatar answered Nov 06 '22 07:11

aqwert