Type Forwarding to .Net: should the redirected class inherit from the Type class?

If you want to redirect a class reference to another assembly using Forward Forwarding, should this class inherit from Type?

I guess I really am - What does this mean or what is indicated by the word "Type" in the phrase and the term "Type Forwarding".

+1
source share
2 answers

If you want to redirect a class reference to another assembly using Forward Forwarding, should this class inherit from Type?

No.

I guess I really am - What does this mean or what is indicated by the word "Type" in the phrase and the term "Type Forwarding".

Suppose you have a Foo type in the Alpha assembly, and in the next version you realize that Foo really should have been in the Bravo assembly. You cannot move this type because all of your clients that have dependencies on Foo located in Alpha will be broken.

The solution is to move the Foo type to Bravo, and then send a new version of Alpha that contains a type forwarder that tells Alpha users "if you are looking for Foo, it is now found in Bravo." This way you will not break anyone who depends on how they are used to being.

I think I'm missing here, it is that the definition of Type refers to the concept of Type Forwarding. What qualifies as a type?

The following are type definitions:

  • non-generic or non-constructed generic classes, structures, interfaces, and delegates
  • transfers

The links to types are listed below (they all refer to another type, none of them defines something new.)

  • constructed common classes, structures, interfaces, and delegates
  • arrays
  • Pointers
  • nullables

(And there is one type that does not fall into any category, which is the return type of "void".)

Of all these types, only type definitions can be redirected. The purpose of a type forwarder is to indicate that "the type that was previously defined by this assembly is now defined by this assembly," so it makes sense to redirect the type definition. You cannot redirect type MyStruct<int>[] ; it makes no sense. You can forward MyStruct<T> .

what do you mean by "unconstructed generic classes? Does this mean only a general definition, and not a general definition that was created with the specified type?

Right.

And can you tell me where you found the information for "type references" and "type definitions"?

These are not concepts from the C # language specification; rather, these are concepts from a basic system such as Common Language Infrastructure. For a detailed technical analysis of how the CLI differs between defined and reference types, read the ECI 335 CLI specification, in particular, look for sections in the metadata tables for TypeDef and TypeRef.

+11
source

This is a bit confusing topic, so here's a step-by-step example - now using the names from Eric's answer to help maintain consistency. We are going to start with one library (Alpha.dll) and create an application (Test.exe), which depends on Alpha. Then we move on to the type that Test.exe depends on (Foo) in another library (Bravo.dll) without recompiling Test.exe.

  • Create the following files:

    Foo.cs

     using System; public class Foo { public static void Report() { Console.WriteLine("Foo.Report"); } } 

    Test.cs

     class Test { static void Main() { Foo.Report(); } } 
  • Build Alpha.dll:

     csc /target:library /out:Alpha.dll Foo.cs 
  • Assembly Test.exe

     csc /r:Alpha.dll Test.cs 
  • Run Test.exe - you should get the obvious output

  • Bravo.dll assembly:

     csc /target:library /out:Bravo.dll Foo.cs 
  • Create a new Forwarding.cs file:

     using System.Runtime.CompilerServices; [assembly:TypeForwardedTo(typeof(Foo))] 
  • Recompile Alpha.dll:

     csc /r:Bravo.dll /target:library /out:Alpha.dll Forwarding.cs 

    Please note that we no longer include code for Foo in Alpha.

  • Run Test.exe - it will still work, despite the fact that Test.exe requests a link to Foo inside Alpha.dll ... the CLR simply redirects this to Bravo.dll.

    If you look in Test.exe, it will still reference Alpha. If you look in Alpha.dll, you will find that the code for type Foo is no more ... it is only through type redirection that it all hangs together.

+6
source

Source: https://habr.com/ru/post/1233345/


All Articles