简体   繁体   中英

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?

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:

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?

That's because the Object constraint isn't System.Object , it's UnityEngine.Object , the class you're look at.

For example...

//public class Object { }

public class MyClass<T> where T : Object { }

Fails to compile, with error:

CS0702 Constraint cannot be special class 'object'

…but will succeed if you uncomment the first line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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