简体   繁体   English

为什么我们不能使用动态方法代替Generic(T)方法

[英]Why can't we use dynamic methods instead for Generic(T) methods

I have Two functions defined as follows. 我有两个定义如下的函数。 Method 1 will return any type dynamically, method 2 will return generic types: 方法1将动态返回任何类型,方法2将返回通用类型:

Method 1 方法1

public dynamic dynamicFun(int n)
        {
            switch (n)
            {
                case 0:
                    return 0;
                case 1:
                    return "N";
                case 2:
                    return 1.3;
                default:
                    return 'v';
            }
        }

Method 2: 方法2:

  public static T genericFun<T>(int n)
        {
            switch (n)
            {
                case 0:
                    return (T)System.Convert.ChangeType(0, Type.GetTypeCode(typeof(T)));
                case 1:
                    return (T)System.Convert.ChangeType("N", Type.GetTypeCode(typeof(T)));
                case 2:
                    return (T)System.Convert.ChangeType(1.3, Type.GetTypeCode(typeof(T)));
                default:
                    return (T)System.Convert.ChangeType('V', Type.GetTypeCode(typeof(T)));
            }
        }

Both are doing the same functionality. 两者都在执行相同的功能。 The function call will be described in the image shown below: 功能调用将在下图中显示: 在此处输入图片说明

Now my question is that ; 现在我的问题是;

  • What is the difference between these two methods? 这两种方法有什么区别?
  • Where we can use dynamic methods and where we can use generics? 在哪里可以使用动态方法,在哪里可以使用泛型?
  • is there any advantage for generic over dynamic? 相对于动态,泛型有什么优势吗?

Why can't we use dynamic methods instead [of] Generic(T) methods? 为什么我们不能使用动态方法代替Generic(T)方法?

It seems to me you can, proven by your examples and the runtime values show . 在我看来,可以通过示例证明,运行时值show可以证明。 But there's an inherent error in your thinking with the examples: you know what type is being returned in your generic method, so you're instantiating it that way. 但是在示例中,您的思维存在一个固有的错误:您知道泛型方法中返回的是哪种类型,因此您需要以这种方式实例化它。 If I did var genericCase1 = genericFun<int>(1) , what would be returned? 如果我做了var genericCase1 = genericFun<int>(1) ,将返回什么? An exception would probably be thrown because "N" is not an integer. 因为“ N”不是整数,可能会引发异常。

What is the difference between these two methods? 这两种方法有什么区别?

Dynamics are determined at runtime whereas generics are resolved at compile time . 动态是在运行时确定的而泛型是在编译时确定的

Where we can use dynamic methods and where we can use generics? 在哪里可以使用动态方法,在哪里可以使用泛型?

I would guess anywhere you could use a method. 我猜想任何地方都可以使用一种方法。 The reason you would use one over another is really based upon the situation you're in. If you do not know the exact Type that will be returned to you at the time of writing, then go the dynamic route. 您之所以会使用另一种类型,实际上是基于您所处的情况。如果您不知道在撰写本文时将返回给您的确切Type,那么请采用动态路由。 You can use this when you know your dealing with a few different base/super classes that have a particular method, and you want to hit that method, but you don't care which derived class is returned from the dynamic method. 当您知道要处理具有特定方法的几个不同的基类/超级类,并且想要使用该方法时,可以使用此方法,而不必关心动态方法返回的派生类。

Generics could be used to avoid writing a lot of overloads for the same functionality. 泛型可用于避免为相同功能编写大量重载。 If you know that you want to perform the same actions on many different things, then create a generic method, and pass the types you want to do stuff with during your writing. 如果您知道要对许多不同的事物执行相同的操作,请创建一个通用方法,并在编写过程中传递要使用的类型。 Using Type Constraints is really great too, because now you can limit what Types are able to call this generic method, and realize any issues at compile time. 使用类型约束也确实很棒,因为现在您可以限制什么类型可以调用此通用方法,并在编译时实现任何问题。

is there any advantage for generic over dynamic? 相对于动态,泛型有什么优势吗?

As others have said in the comments, there are time concerns, one method may take longer than another, but I suppose pieces of that would be determined by the methods implemented, and the amount of work the JIT vs compiler has to do. 正如其他人在评论中所说的那样,这是时间上的担忧,一种方法可能要比另一种方法花费更长的时间,但是我想其中的一部分将由实现的方法以及JIT与编译器必须完成的工作量确定。

Generics would also allow type safety and reduction of errors since they are resolved at compile time. 泛型还将允许类型安全,并减少错误,因为它们是在编译时解决的。 Generics may also be easier to maintain as there is not as much "magic" going on. 由于没有太多“魔术”,泛型也可能更易于维护。

You should probably use dynamics sparingly, because although they are powerful, used incorrectly could provide many sleepless nights debugging. 您可能应该谨慎使用动力学,因为尽管动力学功能强大,但使用不正确可能会导致许多不眠之夜的调试。


If I'm wrong on any of this, please someone correct me. 如果我在任何一个方面有误,请有人纠正我。 As most of us out there, I am learning too, and would appreciate feedback on any issues with my current understanding. 正如我们大多数人一样,我也在学习,并且希望以我目前的理解对任何问题提供反馈。

泛型的一大优点是您可以指定TEg的类型void doSomething<T> () where T : abstractParentClass

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

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