How to create extension methods for Types

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:26:54

The short answer is it cannot be done; extension methods need to work on an instance of something.

To use the extension method, you would have to do:

var instance = typeof(MyClass).ParseJson(text);

The token "MyClass" is not a Type instamce intself, but using typeof will get you a Type to operate on. But how is this any better than:

var instance = JsonUtility.ParseJson<MyClass>(text);

Edit: Actually, the code for the extension method still would not do what you wanted. It will always return a "Type" object, not an instance of that Type.

You can't create extension methods that apply to the type itself. They can only be called on instances of a type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!