[英]How were Unity developers able to constrain a generic type parameter on being derived from Object?
How were Unity developers able to constrain a generic type parameter on being derived from Object? Unity 开发人员如何限制从 Object 派生的泛型类型参数?
While looking for an answer to the question I come over the question .在寻找问题的答案时,我遇到了这个问题。 There is the answer which tells the following:有答案说明以下内容:
There is no difference between the two constraints, except for that one is disallowed for being useless to explicitly state.这两个约束之间没有区别,除了一个是不允许明确声明的。
Proving that it should be impossible to constrain on Object.证明应该不可能约束对象。 While in Unity we can see the following method:在 Unity 中,我们可以看到以下方法:
public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation) where T : Object;
Does it happen because Unity is using Mono, but not CLR?这是因为 Unity 使用的是 Mono 而不是 CLR 吗?
That's because the Object
constraint isn't System.Object
, it's UnityEngine.Object
, the class you're look at.那是因为Object
约束不是System.Object
,而是UnityEngine.Object
,您正在查看的类。
For example...例如...
//public class Object { }
public class MyClass<T> where T : Object { }
Fails to compile, with error:编译失败,报错:
CS0702 Constraint cannot be special class 'object' CS0702 约束不能是特殊类“对象”
…but will succeed if you uncomment the first line. ...但是如果你取消注释第一行就会成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.