C#泛型实现
C# generics implementation
我是 C# 新手,周末想写一个 SNTP 服务器。
在这个开发过程中,我最终遇到了与这个问题完全相同的问题:How to use generics to pass argument to a non-generic method?
这里重复的问题是:"How to use generics to pass argument to a non-generic method?"这个问题的关键答案是所讨论的非泛型方法没有接受对象的重载。
现在我的问题是一个后续问题:为什么泛型是这样实现的?或者换句话说,为什么需要约束?
到目前为止,我的理解是泛型有助于保持编译时类型安全,这意味着编译器知道在编译时正在处理哪些类型。
为什么没有实现 C#(或者这个问题应该与 CLR 相关),以便编译器可以接受这样一个事实,即正在创建泛型 class/method,其中可以提供可以提供的参数并非在所有情况下都可接受。然后,当通用 class/method get 被调用时,编译器可以看到问题并在那个时候 抱怨。
这是技术限制吗?
不能创建泛型方法来包装具有多个重载的非泛型方法,这似乎是一个真正的遗憾。除非我选择将类型检查推迟到 运行 时间,这是上述问题的解决方案,否则我将不得不用一套方法包装这个重载方法,每个签名一个方法,即使其中的代码看起来完全相同的。这将是利用通用方法的理想场所。
最能解释这一点的人是 Eric Lippert,他在 What’s the difference, part one: Generics are not templates:
中做到了
We do the overload resolution once and bake in the result. We do not change it at runtime when someone, possibly in an entirely different assembly, uses string as a type argument to the method. The IL we’ve generated for the generic type already has the method its going to call picked out. The jitter does not say “well, I happen to know that if we asked the C# compiler to execute right now with this additional information then it would have picked a different overload. Let me rewrite the generated code to ignore the code that the C# compiler originally generated...” The jitter knows nothing about the rules of C#.
[...]
Now, if you do want overload resolution to be re-executed at runtime based on the runtime types of the arguments, we can do that for you; that’s what the new “dynamic” feature does in C# 4.0. Just replace “object” with “dynamic” and when you make a call involving that object, we’ll run the overload resolution algorithm at runtime and dynamically spit code that calls the method that the compiler would have picked, had it known all the runtime types at compile time.
为什么不呢:因为运行时不知道如何重新生成所需的代码。
关于设计理念,您的代码应该尽早失败,最好是在编译期间,但我现在找不到那句话。
我是 C# 新手,周末想写一个 SNTP 服务器。 在这个开发过程中,我最终遇到了与这个问题完全相同的问题:How to use generics to pass argument to a non-generic method?
这里重复的问题是:"How to use generics to pass argument to a non-generic method?"这个问题的关键答案是所讨论的非泛型方法没有接受对象的重载。
现在我的问题是一个后续问题:为什么泛型是这样实现的?或者换句话说,为什么需要约束?
到目前为止,我的理解是泛型有助于保持编译时类型安全,这意味着编译器知道在编译时正在处理哪些类型。
为什么没有实现 C#(或者这个问题应该与 CLR 相关),以便编译器可以接受这样一个事实,即正在创建泛型 class/method,其中可以提供可以提供的参数并非在所有情况下都可接受。然后,当通用 class/method get 被调用时,编译器可以看到问题并在那个时候 抱怨。
这是技术限制吗?
不能创建泛型方法来包装具有多个重载的非泛型方法,这似乎是一个真正的遗憾。除非我选择将类型检查推迟到 运行 时间,这是上述问题的解决方案,否则我将不得不用一套方法包装这个重载方法,每个签名一个方法,即使其中的代码看起来完全相同的。这将是利用通用方法的理想场所。
最能解释这一点的人是 Eric Lippert,他在 What’s the difference, part one: Generics are not templates:
中做到了We do the overload resolution once and bake in the result. We do not change it at runtime when someone, possibly in an entirely different assembly, uses string as a type argument to the method. The IL we’ve generated for the generic type already has the method its going to call picked out. The jitter does not say “well, I happen to know that if we asked the C# compiler to execute right now with this additional information then it would have picked a different overload. Let me rewrite the generated code to ignore the code that the C# compiler originally generated...” The jitter knows nothing about the rules of C#.
[...]
Now, if you do want overload resolution to be re-executed at runtime based on the runtime types of the arguments, we can do that for you; that’s what the new “dynamic” feature does in C# 4.0. Just replace “object” with “dynamic” and when you make a call involving that object, we’ll run the overload resolution algorithm at runtime and dynamically spit code that calls the method that the compiler would have picked, had it known all the runtime types at compile time.
为什么不呢:因为运行时不知道如何重新生成所需的代码。
关于设计理念,您的代码应该尽早失败,最好是在编译期间,但我现在找不到那句话。