c# - 将创建多少类型的实例
问题描述
我有两个项目说BusinessLogic (BL)
和DataAccess (DL)
。现在我最终将类型作为参数从controller
toBL
和 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 DL
。SomeClassForTable
现在我想知道在整个过程中将在内存中创建多少个实例?会有三个实例(BL
,DL
和控制器中的那个)?
解决方案
您没有显示任何正在创建的实例 - 但将引用从一种方法传递到不会隐式创建新实例,不。它复制引用,而不是对象。方法是否在同一个程序集中都没有关系。
如果涉及用户定义的隐式转换,则可以在这种情况下隐式创建对象。例如:
public void Method1(string x)
{
Method2(x);
}
public void Method2(XNamespace ns)
{
}
这里对 to 的调用使用了用户定义的从toMethod2
的隐式转换,它可以创建一个新对象。但是如果参数类型和参数类型之间存在引用转换(例如,如果它们是相同类型,或者方法参数类型是参数类型的基类),那么引用将简单地复制为参数。string
XNamespace
如果涉及不同的 s,事情会变得更加复杂AppDomain
,但我怀疑你不是那种情况(幸运的是)。
推荐阅读
- git - 无法将 Github 与 Visual Studio 同步
- java - Java如何实现类似C++的函数模板
- regex - 记事本++中带有2个分号的正则表达式
- css - 将 css 转换放入变量中
- c# - 如何在c#winforms中将登录人用户名插入数据库?
- python - 如何在ODOO的自定义模板中显示产品名称、产品价格和产品图片?
- elasticsearch - 为什么有时 Elasticsearch 滚动或搜索会返回一组无法单独检索的文档 ID?
- r - 如何用视觉光谱颜色填充曲线下的区域
- node.js - node.js如何从远程服务器读取流中的文件
- php - 我可以将一个 wordpress 站点的所有帖子显示到另一个 wordpress 站点,并将单独的域作为所有帖子的前缀吗?