I was looking at the example of the article below https://devblogs.microsoft.com/dotnet/evolving-the-reflection-api/ and still don't quite understand it, below is the quote:
Suppose that we are using a static analysis tool that is implemented with the new reflection model. We are looking for all types in an app that derive from the UIControl class. We want to be able to run this tool on workstations on which the UIControl assembly (which contains the UIControl class) does not exist. In this example, let’s assume that we open an assembly that contains a class that derives from the UIControl class:
class MyClass : UIControl
In the .NET Framework 4 reflection model, the Type object (incorporating both reference and definition) that represents MyClass would create a Type object for the base class, which is UIControl. On machines that don’t have the UIControl assembly, the request to construct the UIControl Type object would fail, and so too would the request to create a Type object for MyClass, as a result.
Here you see what the reference/definition split achieves. In the new model, MyClass is a TypeInfo (definition); however, BaseType is a Type (reference), and will contain only the information about UIControl that the (MyClass) assembly contains, without requiring finding its actual definition.
Type baseType = myClassTypeInfo.BaseType;
so I did some experiments, I created an console application and two class libraries, one contains UIControl.dll and the other contains MyClass.dll, and MyClass.dll references UIControl.dll (via Visual Studio's Add Project Reference) then I copied the MyClass.dll into a new location (so CLR can't find UIControl.dll) and make console application references to this MyClass.dll simulate that "UIControl assembly (which contains the UIControl class) does not exist on workstations" as the article mentioned.
but when I tried to use Type api or TypeInfo api, it always throwed FileNotFoundException saying it couldnot load UIControl assembly:
// Console app that reference MyClass.dll
// note that this dll is coped into a new location
// so CLR can't find its referencing UIControl.dll
internal class Program
{
Type type = typeof(MyClass); // throw FileNotFoundException
TypeInfo typeInfo = typeof(MyClass).GetTypeInfo(); // throw FileNotFoundException
}
so how does the author gets the TypeInfo
instance (myClassTypeInfo) without throwing FileNotFoundException?
Aucun commentaire:
Enregistrer un commentaire