-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Today the reference assemblies for .NET assemblies strip private fields from structs. This has an observable, and potentially dangerous, impact on project that consume them:
- Allows pointers to be created for structs with fields of a reference type.
- Prevents the compiler from catching cyclic struct layout problems.
- Breaks C# definite assignment checking.
- Allows structs using
[FieldOffset]to verify when they should not. - Prevents developers from correctly planning for interop scenarios.
More details are available here and an example of the problems this can produce is here.
I understand part of the motivation for removing these fields is to keep the reference assemblies small. Keeping the fields necessitates keeping the type definitions for the types of the fields and this can cascade into many more types / members being included thus increasing size.
Cutting private fields for classes is fine as it's unobservable. Unfortunately for structs fields are observable irrespective of their accessibility and must be maintained in reference assemblies. The only action I think that can be done to curtail the number of types a struct brings in is the following:
A reference assembly can represent an inaccessible struct field which is a reference type as
object.
Essentially class, interface, delegate and generic type parameters constrained to class can be transformed to object.
// Implementation assembly
private interface IBigInterface { ... }
public struct S
{
private IBigInterface _field;
}
// Reference assembly
public struct S
{
private object _field;
}This is unobservable to the compiler and can help limit the number of types brought it.