{"content":{"title":"solidity -> type","body":"type\r\n--\r\n- type(x) ->返回x类型的对象信息\r\n- type(x).name ->合约的名字\r\n- type(x).creattionCode ->合约部署时的bytecode\r\n- type(x).runtimecode ->合约运行时的bytecode\r\n\r\n```\r\n/**\r\n * @title Storage\r\n * @dev Store & retrieve value in a variable\r\n */\r\ncontract Storage {\r\n    string public str;\r\n    constructor(string memory _str) {\r\n        str = _str;\r\n    }\r\n\r\n    uint256 number;\r\n\r\n\r\n    /**\r\n     * @dev Store value in variable\r\n     * @param num value to store\r\n     */\r\n    function store(uint256 num) public {\r\n        number = num;\r\n    }\r\n\r\n    /**\r\n     * @dev Return value \r\n     * @return value of 'number'\r\n     */\r\n    function retrieve() public view returns (uint256){\r\n        return number;\r\n    }\r\n\r\n    function getInfo() public pure returns(string memory name) {\r\n        name = type(Storage).name;\r\n        // creationCode 和runtimeCode不能在这个合约自己内部使用，防止会出现循环调用问题\r\n        // creationCode = type(Storage).creationCode;\r\n        // runtimeCode = new type(Storage).runtimeCode;\r\n    }\r\n}\r\n\r\ncontract TestStorage {\r\n    Storage s;\r\n    constructor(Storage _address) {\r\n        s = _address;\r\n    }\r\n\r\n    function getInfo() public view returns(bytes memory creationCode, bytes memory runtimeCode) {\r\n        creationCode = type(Storage).creationCode;\r\n        runtimeCode = type(Storage).runtimeCode;\r\n    }\r\n}\r\n```"},"author":{"user":"https://learnblockchain.cn/people/5348","address":null},"history":"QmX5WSCWSapkpJb5UnYbRe8U5P2wGAkfyijVTEVVHueGtn","timestamp":1668263796,"version":1}