首页 > 解决方案 > 将创建多少类型的实例

问题描述

我有两个项目说BusinessLogic (BL)DataAccess (DL)。现在我最终将类型作为参数从controllertoBL和 to传递DL。下面是代码。

控制器

public ActionResult SomeAction (SomeClassForTable table)
{
    bool result = new ServiceInBL.DoSomeStuffWithParameter(table);
}

提单

public class ServiceInBL
{
    bool DoSomeStuffWithParameter (SomeClassForTable classForTable)
    {
        MethodForCrudInDL dl = new MethodForCrudInDL();
        return dl.DoSomeStuffWithParameter(classForTable);
    }
}

深度学习

public class MethodForCrudInDL
{
    public bool DoSomeStuffWithParameter (SomeClassForTable classForTable)
    {
        return true;
    }
}

某类

public class SomeClassForTable
{
    // type members
}

从我的控制器,我调用方法 inBL和 from BL,调用方法 in DLSomeClassForTable现在我想知道在整个过程中将在内存中创建多少个实例?会有三个实例(BL,DL和控制器中的那个)?

标签: c#.netclassinstances

解决方案


您没有显示任何正在创建的实例 - 但将引用从一种方法传递到不会隐式创建新实例,不。它复制引用,而不是对象。方法是否在同一个程序集中都没有关系。

如果涉及用户定义的隐式转换,则可以在这种情况下隐式创建对象。例如:

public void Method1(string x)
{
    Method2(x);
}

public void Method2(XNamespace ns)
{
}

这里对 to 的调用使用了用户定义的从toMethod2的隐式转换,它可以创建一个新对象。但是如果参数类型和参数类型之间存在引用转换(例如,如果它们是相同类型,或者方法参数类型是参数类型的基类),那么引用将简单地复制为参数。stringXNamespace

如果涉及不同的 s,事情会变得更加复杂AppDomain,但我怀疑你不是那种情况(幸运的是)。


推荐阅读