.Net Core 如何反射service呢?

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

...全文
557 6 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
换回老方法解决了 谢谢两位大佬 只是不知道还有没有更好的处理方式
This essential classic title provides a comprehensive foundation in the C# programming language and the frameworks it lives in. Now in its 8th edition, you’ll find all the very latest C# 7.1 and .NET 4.7 features here, along with four brand new chapters on Microsoft’s lightweight, cross-platform framework, .NET Core, up to and including .NET Core 2.0. Coverage of ASP.NET Core, Entity Framework (EF) Core, and more, sits alongside the latest updates to .NET, including Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and ASP.NET MVC. Dive in and discover why Pro C# has been a favorite of C# developers worldwide for over 15 years. Gain a solid foundation in object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies). With the help of this book you’ll have the confidence to put C# into practice and explore the .NET universe on your own terms. What You Will Learn Discover the latest C# 7.1 features, from tuples to pattern matching Hit the ground running with Microsoft’s lightweight, open source .NET Core platform, including ASP.NET Core MVC, ASP.NET Core web services, and Entity Framework Core Find complete coverage of XAML, .NET 4.7 and Visual Studio 2017 Understand the philosophy behind .NET and the new, cross-platform alternative, .NET Core Table of Contents Part I: Introducing C# and the .NET Platform Chapter 1: The Philosophy of .NET Chapter 2: Building C# Applications Part II: Core C# Programing Chapter 3: Core C# Programming Constructs, Part I Chapter 4: Core C# Programming Constructs, Part II Part III: Object-Oriented Programming with C# Chapter 5: Understanding Encapsulation Chapter 6: Understanding Inheritance and Polymorphism Chapter 7: Understanding Structured Exception Handling Chapter 8: Working with Interfaces Part IV: Advanced C# Programming Chapter 9: Collections and Generics

62,046

社区成员

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

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

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

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