If you want to forward a class reference to another assembly with Type Forwarding, does that class need to inherit from Type?
I guess what I am really after is - What does it mean, or what is indicated, by the word "Type" in the phrase and concept of Type Forwarding.
It's a slightly confusing topic, so here's a step-by-step example - now using the names from Eric's answer to help keep things consistent. We're going to start off with one library (Alpha.dll) and build an application (Test.exe) which depends on Alpha. We're then going to a type which Test.exe depends on (Foo) into a different library (Bravo.dll) without recompiling Test.exe.
Create the following files:
Foo.cs
Test.cs
Build Alpha.dll:
Build Test.exe
Run Test.exe - you should get the obvious output
Build Bravo.dll:
Create a new file, Forwarding.cs:
Recompile Alpha.dll:
Note how we're not including the code for Foo in Alpha any more.
Run Test.exe - it will still work, despite the fact that Test.exe asks for a reference to Foo within Alpha.dll... the CLR simply redirects that to Bravo.dll.
If you look in Test.exe, it will still refer to Alpha. If you look in Alpha.dll, you'll find that the code for the Foo type isn't there any more... it's only through type forwarding that it all hangs together.
No.
Suppose you have a type Foo in assembly Alpha, and in your next version you realize that Foo really should have been in assembly Bravo. You can't move the type because all your customers who have dependencies on Foo being in Alpha will be broken.
The solution is to move the type Foo into Bravo, and then ship a new version of Alpha that contains a type forwarder that tells users of Alpha "if you're looking for Foo, it's now found in Bravo". That way you don't break anyone who depends on things being the way they used to be.
The following things are type definitions:
The following things are type references (they all refer to another type; none of these define something new.)
(And there is one type that does not fall into either category, which is the return type "void".)
Of all those types, only the type definitions can be forwarded. The purpose of a type forwarder is to say "the type that used to be defined by this assembly is now defined by that assembly", so it only makes sense to forward a type definition. You can't forward the type
MyStruct<int>[]
; that doesn't make any sense. You can forwardMyStruct<T>
.Correct.
These are not concepts from the C# language specification; rather, these are concepts from the underlying Common Language Infrastructure type system. For an extensive technical look at how the CLI differs between defined and referenced types, read the ECMA 335 CLI specification, particularly looking for the sections on the metadata tables for TypeDef and TypeRef.