c# - Why does loading a class that contains a Value-Type field enforces the CLR to load that value type? -
suppose have following types in following assemblies:
assembly1:
public struct dependencystruct { } public class dependencyclass { }
assembly2:
public class usingdependency { private dependencystruct m_dependencystruct; // having field cause loading of dependencystruct type (thus cause assembly binding request). private dependencyclass m_dependencyclass; // having field **not** cause loading of dependencyclass type. }
assembly3 (executable)
public class program { static void main(string[] args) { assembly assembly = assembly.loadfrom("assembly2.dll"); type[] types = assembly.gettypes(); assembly[] assemblies = appdomain.currentdomain.getassemblies(); } }
when run following code, find assembly2 , assembly1 in assemblies array.
if comment out m_dependencystruct declaration, find assembly2 in assemblies array.
note i'm not creating instances in code, loading types.
my questions are:
why having value type field cause clr load entire type (as opposed reference type has deferred loading)?
is there way defer value-type loading? using
lazy<dependencystruct> m_lazydependencystruct
or creating wrapper class work, i'm curious if there's way of doing without changing actual type.
thanks!
this pretty hard nail down, code in clr loads types complicated mass of c++ code. 1 thing see being done in methodtablebuilder::initializefielddescs() method calculates offsets of fields in class.
which requires knowing how storage required each field. that's of course simple fields of reference type, size of pointer. not fields of value type, requires loading type of field, recursing necessary through fields calculate size. of course has side effect see assembly contains value type loaded well.
just educated guess. can have look-see @ class.cpp source code file in sscli20 distribution yourself. strong implementation detail, make sure never care this.
Comments
Post a Comment