Manage cyclic type dependencies
osman-turan opened this issue · comments
During working #9, I realized a major problem in the code architecture. All serialization logic works in-order. With latest fixes serializing a type works like that:
- Serialize all fields of the type
- Serialize all properties of the type
- Serialize all methods of the type
- Serialize all constructors of the type
- Serialize the type
Meaning that, a simple code which uses singleton pattern cannot be serialized:
public class MyClass {
private static MyClass _instance;
public static MyClass GetInstance() {
if (_instance == null) {
_instance = new MyClass();
}
return _instance;
}
}
Because, during serializing the GetInstance
method (see 3rd step), MyClass
type is not added into _typeHandles
dictionary which is used for type resolution.
The following code is the root of the problem:
ILPack/src/AssemblyGenerator.Types.cs
Lines 80 to 119 in bc83341
Relevant unit test can be found in self-reference-unit-test
branch. I didn't merge the changes into develop
branch incase you set up CI pipeline which relies on all tests passed.
A major refactoring is required to support cyclic-references with placeholder classes to "reserve" metadata tokens. This refactoring will increase the coverage of generic support as well such as:
public class Vector: IEquatable<Vector> {
// ...
}
Relevant unit test was added to master branch.