【发布时间】:2014-04-13 16:21:01
【问题描述】:
假设我在以下程序集中有以下类型:
组装1:
public struct DependencyStruct { }
public class DependencyClass { }
组装2:
public class UsingDependency
{
private DependencyStruct m_DependencyStruct; // having this will field will cause the loading of the DependencyStruct type (thus will cause an assembly binding request).
private DependencyClass m_DependencyClass; // having this field will **not** cause the loading of the DependencyClass type.
}
Assembly3(可执行文件)
public class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFrom("Assembly2.dll");
Type[] types = assembly.GetTypes();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
}
}
当我运行以下代码时,我会在程序集数组中找到 Assembly2 和 Assembly1。
如果我注释掉 m_DependencyStruct 声明,我会在程序集数组中找到只有 Assembly2。
请注意,我没有在这段代码中创建任何实例,只是加载类型。
我的问题是:
为什么具有值类型字段会导致 CLR 加载整个类型(而不是具有延迟加载的引用类型)?
有没有办法推迟值类型的加载?使用
Lazy<DependencyStruct> m_LazyDependencyStruct或创建另一个包装类都可以,但我很好奇是否有另一种方法可以在不改变实际类型的情况下做到这一点。
谢谢!
【问题讨论】:
标签: c# .net clr .net-assembly value-type