简体   繁体   English

扩展T的扩展方法 - 糟糕的做法?

[英]Extension method that extends T - bad practice?

I've read that it is usually bad practice to extend System.Object, which I do agree with. 我已经读过,扩展System.Object通常是不好的做法,我同意这一点。

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, 它类似于扩展System.Object但不完全相同,

    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. 这实际上允许任何对象调用任何将该对象作为参数并返回R的函数,无论该函数是否属于该对象。 I think this could facilitate some interesting 'inversion of control', but not sure about it overall. 我认为这可能有助于一些有趣的“控制反转”,但总体上并不确定。

Thoughts? 思考?

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? 1)使用this T创建扩展方法是否是一个好主意,因此它将应用于所有类型?

2) Whether the particular extension method described is useful? 2)所描述的特定扩展方法是否有用?

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. 您可以将扩展方法应用于所有类,就像linq确保您选择适当的命名空间一样。 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. 我认为在System命名空间中创建这种类型的扩展方法是一个坏主意,但如果它更有针对性,那么它可能会有用。

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). 看一下然后对传递实例的方法的简单调用更自然和典型,但是如果你的扩展方法变得更加复杂,那么我回到我的第一点,它取决于上下文(这可能有助于封装) 。

All this does is that it gives you the ability to refer to a method as a parameter which is in fact what delegates already allow you in C#. 所有这一切都是因为它使您能够将方法作为参数引用,实际上代理已经允许您在C#中使用。

I don't see it being more useful (in case of IoC) than a delegate of type Func<T,R> in your case. 在你的情况下,我认为它不是更有用(在IoC的情况下)而不是类型为Func<T,R>的委托。 It's just another way of invoking it. 这只是调用它的另一种方式。

UPDATE UPDATE

As mentioned in the comments, I think this method only helps you in creating delegates more efficiently. 正如评论中所提到的,我认为这种方法只能帮助您更有效地创建委托。 But either way, you do not use the created delegate any further since you invoke it immediately. 但无论哪种方式,您都不会再使用创建的委托,因为您立即调用它。 So an extension method like this would make more sense to me: 所以像这样的扩展方法对我来说更有意义:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM