Skip to content

Proposal: Static Linking #162

@stephentoub

Description

@stephentoub

(Note: this proposal was briefly discussed in #98, the C# design notes for Jan 21, 2015. It's a very rough idea at the moment.)

Background

C# today supports dynamic linking, such that one assembly can reference another and with the latter being available to the former at run time, loaded at run time to be used by the former, etc.

Problem

Helper functionality, and in particular extension methods, often get put into helper libraries that other assemblies can use. It's often the case that these helper libraries grow significantly in size but that a given project which uses that library only needs a few of the helpers it contains, yet the only real options available to a developer today are to either expose the helpers as source that can be selectively compiled into the consuming assembly, or distribute the helper library as a DLL with the project, even though only a small portion of it is being used.

Solution

We should consider adding some form of static linking to the C# compiler. When project A adds a static reference to assembly/project B, rather than actually adding a reference to the resulting assembly, the compiler would pull copy the IL from the referenced assembly into the consuming assembly. As a result, the referenced assembly would not need to be distributed with the referencing assembly, and only the functionality the referencing assembly used would be brought in.

There are of course complications with this. For example, if a method being used via static linking uses a static field, what becomes of that field, especially if multiple assemblies in the same project statically link against the same library? Such issues would need to be explored. A potential approach would be to have a new attribute, e.g. [StaticallyLinkable], which would need to be applied to any type/method/field/etc. that supported being statically linked against, essentially forcing the developer of the library to opt-in to being statically linked such that s/he designed it with static linking in mind. When consuming functionality via a static link, the compiler would verify that all entities reachable from the functionality being referenced are either themselves [StaticallyLinkable] or are dynamically referenced by the consuming assembly. Etc.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions