Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-Forwarding

Tags:

c#

.net

Can anyone provide a real life senario where Type Forward is use?

like image 240
Brandon Michael Hunter Avatar asked Nov 16 '09 21:11

Brandon Michael Hunter


2 Answers

The BCL libraries commonly use the TypeForwardedTo attribute when different versions of the framework move a type between assemblies. For example, the Func<> style delegates were moved from System.Core in the 3.5 framework to mscorlib in the 4.0 framework.

You can view real world uses of this by opening System.Core.dll from 4.0 in ildasm, double clicking on the Manifest node and looking for all of the lines similar to the following

.class extern forwarder System.Func`1
{
  .assembly extern mscorlib
}
.class extern forwarder System.Func`2
{
  .assembly extern mscorlib
}
.class extern forwarder System.Func`3
{
  .assembly extern mscorlib
}
like image 176
JaredPar Avatar answered Sep 24 '22 18:09

JaredPar


From msdn:

For example, suppose an application uses the Example class in an assembly named Utility.dll. The developers of Utility.dll might decide to refactor the assembly, and in the process they might move the Example class to another assembly. If the old version of Utility.dll is replaced by the new version of Utility.dll and its companion assembly, the application that uses the Example class fails because it cannot locate the Example class in the new version of Utility.dll.

The developers of Utility.dll can avoid this by forwarding requests for the Example class, using the TypeForwardedToAttribute attribute. If the attribute has been applied to the new version of Utility.dll, requests for the Example class are forwarded to the assembly that now contains the class. The existing application continues to function normally, without recompilation.

like image 21
Aaron Avatar answered Sep 22 '22 18:09

Aaron