.Net Core 如何反射service呢?

Logerlink 2020-07-21 11:23:31
.Net Core 如何反射service呢?
如下图,当我想反射Service调用Add函数时,此时的db依旧为null,该如何初始化db呢?

...全文
580 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Logerlink 2020-07-21
  • 打赏
  • 举报
回复
引用 3 楼 wanghui0380 的回复:
用容器管理的,就在容器的里找,不要随便乱反射 host.Services.GetService<T>()
嗯 如果我不用反射的话 那么我每个Controller都要写单独写一个Action来处理 现在我的想法是,统一调用一个Controller-Action 然后通过反射去调用各个Service里面的方法
Logerlink 2020-07-21
  • 打赏
  • 举报
回复
引用 1 楼 正怒月神 的回复:
1 没看出来哪里反射了。
没贴完 这里是用到反射的 是在一个工具类的Controller里面
wanghui0380 2020-07-21
  • 打赏
  • 举报
回复
用容器管理的,就在容器的里找,不要随便乱反射 host.Services.GetService<T>()
正怒月神 版主 2020-07-21
  • 打赏
  • 举报
回复
而你既然是构造注入的db。 那直接在方法中使用这个db就可以了。不用new
正怒月神 版主 2020-07-21
  • 打赏
  • 举报
回复
1 没看出来哪里反射了。 2 这个应该是IOC注入框架 那么就不能new DataContext。 3 我看上面是通过构造函数拿到的。 那么就只能直接使用db这个对象了。 拓展一下:因为普遍的ioc框架会通过注入,帮你调用一个被代理的构造函数。也就是说,他会在构造函数中添加一些其他的框架逻辑。 所以普遍情况下,是不能让你自己new实例化的。
Logerlink 2020-07-21
  • 打赏
  • 举报
回复
换回老方法解决了 谢谢两位大佬 只是不知道还有没有更好的处理方式
Book Description to Finelybook sorting Learn how to build web applications from three Microsoft MVPs. After building the data application layer using Entity Framework Core and a RESTful service using ASP.NET Core, you will then build the client side web application three ways: first, using ASP.NET Core, then using Angular 2, and, finally, using React. You will be able to compare and contrast these UI frameworks and select the best one for your needs. .NET Core is a complete rewrite of the popular .NET and its related frameworks. While many concepts are similar between .NET Core and the .NET 4.6 framework, there are revolutionary changes as well, including updates to Entity Framework Core and ASP.NET Core. The first section of this book covers the three main parts of building applications with C#: Entity Framework, ASP.NET Core Services, and ASP.NET Core Web Applications. There is also an explosion in popularity of JavaScript frameworks for client side development, and the authors cover two of the most popular UI frameworks. Start with TypeScript for developing clean JavaScript, along with a client side build tool such as Gulp, Grunt, and WebPack. Using the same data access layer and RESTful service from the .NET Core application, you can rebuild the UI using Angular 2. Then, repeat the process using React, for a true comparison of building client side applications using ASP.NET Core, Angular 2, and React. What You’ll Learn Understand the fundamentals of .NET Core and what that means to the traditional .NET developer Build a data access layer with Entity Framework Core, a RESTful service with ASP.NET Core MVC, and a website with ASP.NET Core MVC and Bootstrap Automate many build tasks with client side build utilities Who This Book Is For Intermediate to advanced .NET developers Contents Part I: Visual Studio 2017 and .NET Core Chapter 1: Introducing Entity Framework Core Chapter 2: Building the Data Access Layer with Entity Framework Core Chapter 3: Building the RES

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧