简体   繁体   English

Unity 开发人员如何限制从 Object 派生的泛型类型参数?

[英]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.

相关问题 如何将泛型类型的抽象类约束到派生类的接口? - How to constrain generic type of abstract class to interface of derived class? 是否可以将泛型参数类型限制于此? - Is it possible to constrain a generic parameter type to this? 将方法上的泛型类型约束为从抽象泛型基类的任何变体形式派生的任何类 - Constrain generic type on a method to be any class that is derived from any variant form of an abstract generic base class 如何约束泛型扩展的类型 - How to constrain type of generic extension 是否可以将泛型类型参数约束为String OR Array - Is it possible to constrain a generic type parameter to String OR Array 是否可以将泛型参数约束为当前对象的子类型? - Is it possible to constrain a generic parameter to be a subtype of the current object? 是否可以将C#泛型方法类型参数约束为“可从”包含类的类型参数“赋值”? - Is it possible to constrain a C# generic method type parameter as “assignable from” the containing class' type parameter? 无法将 C# 基类构造函数约束为仅接受使用派生类型作为泛型类型的类型的注入对象 - Cannot constrain C# base class constructor to only accept injected object of a type that uses the derived type as generic type “this”类型为派生类的泛型参数 - “This” type in generic parameter for derived classes 传递具有从所需类型派生的类型的通用 object - Passing a generic object with type that is derived from the required type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM