There are many questions in .NET about supporting non-empty reference types in .NET. The big hope was code contracts, but it was limited to checking the runtime for those with a limited budget.
Regarding approaches other than code contracts, John Skeet wrote a blog post about this a few years ago, and one of the commentators provided a useful view of NonNull struct in which IL was modified to disable the default constructor. This seems like a great approach, and I can assume that it expands it to provide all kinds of fuzzy microtypes . The manipulation of IL can be a post-assembly step, initiated by a structure attribute, for example.
[NoDefaultConstructor]
public struct Age
{
public Age(int age)
{
}
}
Before I explore this further, I would like to ask if anyone knows about any problems that may arise. I didn’t think about anything.
Akash source
share