【c#-Simple Injector是否可以结合TypeFactoryContext向实例工厂注册?】教程文章相关的互联网学习教程文章

c# – 如何正确地在startup.cs的ConfigureServices方法中注入DbContext实例(ASP.net核心1.1)?【代码】

我已经为我的ASP.NET核心Web应用程序实现了EntityFrameworkFileProvider,我希望在构造函数中由ASP.NET核心DI框架注入ViewDbContext实例: (ViewDbContext是一个dbContext)public class EntityFrameworkFileProvider : IFileProvider {private ViewDbContext _context;public EntityFrameworkFileProvider(ViewDbContext context){ /* should be injected by asp.net core DI */_context = context;}public IDirectoryContents GetD...

c# – dependecy injection和unit testing – 静态帮助方法或私有实例方法【代码】

从单元测试和依赖注入的角度来看,在辅助方法方面通常采用的规范是什么? 这是我的示例情况:public class GoodiesController : Controller {private IMyContext _context;public GoodiesController(IMyContext context){_context = context}public async Task<IAction> GetThoseGoodies(){ if(YouLikeThemThisWay(Request.Path)){var result = await _context.GoGetThemThisWay()} else { }}我的问题是,我最好将YouLikeThemThisWay...

c# – 实例方法的’this’参数可以是无类型的(即System.Object)吗?【代码】

我正在使用System.Reflection.Emit的TypeBuilder来发出一堆带有实例方法的自定义.NET类.例如:public class EmittedClass {public bool TryGetName(out string value){...}public bool TryGetAge(out int value){...} }所有方法都遵循可由通用委托描述的相同签名:public delegate bool TryGetter<T>(out T value);当然,我希望能够在调用站点上显式指定目标实例,如下所示:var instance = InstanceFactory.CreateInstance(); var t...

c# – 用于获取表单实例的函数【代码】

我想构建一个构建一个函数,它确实返回一个已经存在的表单实例.fx = getForm(Form1);在这里我得到错误’FensterTest.Form1’是’类型’但是像’变量’一样使用所以我需要一些演员,但我不知道我需要演员.private Form getForm(Form f) {foreach (Form a in Application.OpenForms){if (a is f){f fx = (f)a;return fx;}}return null; }无论我在哪里使用“f”,我都会得到“找不到类型或命名空间名称’f’” “f fx =(f)a;”以前是“Fo...

C#从同一文件读取的多个程序实例【代码】

我编写了一个从文件读取的程序,根据给定的参数处理它并写入一些输出.我很好奇,如果用户创建我的程序的更多实例(通过打开可执行文件)并在同一文件上运行它们时可能会出现任何奇怪的问题. 我从文件中读取while循环和file.ReadLine().解决方法:I was curious, if there can occur any strange problems when user creates more instances of my program (by opening executable file) and runs them over the same file.这个措辞的方式...

c# – 查询Unity的所有接口实例【代码】

我需要在我的项目中找到所有实现名为IMyInterface的接口并在Unity容器中注册的类. 任何人都知道这样做的方法,没有统一创建在Unity注册的对象的实例?解决方法:您可以使用类似的查询在容器实例中查询注册var x = container.Registrations.Where(cm => cm.RegisteredType == typeof(IMyInterface));

c# – 静态类中的对象实例【代码】

我正在开发一个包含多个WCF服务引用的Web应用程序.目前,每次我们需要拨打服务时,我们都会执行以下操作(例如):Service.ServiceClient ServiceClient = new Service.ServiceClient(); ServiceClient.SomeMethod();如果一个静态类具有对每个Service的静态引用并改为调用该类,那么是否更好?从而避免每次我们想要调用它时创建ServiceClient对象的新实例? 例如:public static class Services {private static Service.ServiceClient ...

c# – Spring.NET – 未将对象引用设置为对象的实例【代码】

我是.NET的新手.我之前在Java中使用过Spring Framework和Hibernate,但这是我在.NET中的第一次.为了开发我的应用程序,我使用Spring.Northwind解决方案作为示例. hibernate映射是:<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"><class name="EMSApplication.Domain.Employee, EMSApplication.Domain" table="ems_Employees" proxy="EMSApplication.Domain.IEmployee, EMSApplication.Domain" lazy="true"><id name="Id" t...

c# – Reflection Emit创建类实例【代码】

我想使用Reflection Emit来创建具有任意构造函数参数的类的实例.这是我的代码的样子:public delegate object ObjectActivator(params object[] args); static void Main(string[] args) {var ao = new { ID = 10000, FName = "Sample", SName = "Name" };var t = ao.GetType();var info = t.GetConstructor(new Type[] { typeof(int), typeof(string), typeof(string) });var objActivatorEmit = GetActivatorEmit(info);var obj ...

c# – 具有PropertyInfo.PropertyType类型的通用类实例【代码】

我有以下课程:public class SampleClassToTest<T> {public static Fake<T> SomeMethod(string parameter){// some code}public static Fake<T> SomeMethod(string parameter, int anotherParameter){//some another code} }public class Fake<T> {// some code }我想以这种方式使用它们:SampleClassToTest<MyClass>.SomeMethod("some parameter");我遇到的问题如下:“MyClass”的类型我只能使用Reflection从PropertyInfo实例获取...

c# – 从GenericClass访问静态方法,其中T由Type实例给出【代码】

我有一个带静态方法的泛型类,该方法使用type参数:GenericClass<T> {public static void Method(){//takes info from typeof(T)} }现在,我需要访问该静态方法,但不是简单地使用GenericClass< KnownType> .Method().我需要有一个Type实例.所以:public void OutsiderMethod(Type T) {GenericClass<T>.Method() //it's clear this line won't compile, for T is a Type instance//but i want some way to have access to that static...

C# HTML解析工具HtmlAgilityPack使用实例(一)【代码】【图】

一、生成HTML字符串 //生成DOM字符串结构 HtmlNode container = HtmlNode.CreateNode("<div />"); HtmlNode title = HtmlNode.CreateNode("<h3 />"); title.InnerHtml = "张三丰"; HtmlNode link = HtmlNode.CreateNode("<a />"); link.InnerHtml = "点击进入"; link.SetAttributeValue("href", "http://wwww.gongjuji.net"); container.AppendChild(title).AppendChild(link); Console.WriteLine(container.OuterHtml);二、解析HT...

c# – 如何在ASP.NET中处置或关闭XML文档的实例【代码】

有一个问题,其中XML Document.Save导致进程无法访问该文件的错误,因为它正被另一个进程或语句使用无效的XML文档.我认为这是因为我没有处置XML Document对象在它的操作完成后.是否可以这样做.有解决方法吗?解决方法:这取决于您使用的Save方法的过载.如果直接传递文件名作为字符串,则应该没有问题.如果您传递流或xmlwriter,您需要确保它已正确处理:using (Stream stream = ...) {doc.Save(stream); }

C#实例拷贝/传递对象引用与Java不同?【代码】

class Player {private Location location;public Location getLocation(){return location;}public void setLocation(Location location){this.location = location;} }…class Location {int x,y,z;public Location(int x, int y, int z){this.x = x;this.y = y;this.z = z;}public Location(Location location){this.x = location.x;this.y = location.y;this.z = location.z;}public void updateLocation(Location location) /...

c# – Rhino Mocks返回时存储不同的实例【代码】

我正在寻找一种让我的测试更清晰的方法,这就是问题所在:public interface A {}public interface B {A GetA(); }现在,如果我想在B上使用存根,并且每次调用GetA时都需要一个新实例,我会这样做:[Test] public void TestName() {MockRepository mockery = new MockRepository();B b = mockery.Stub<B>();b.Stub(x => x.GetA()).Return(mockery.Stub<A>()).Repeat.Once();b.Stub(x => x.GetA()).Return(mockery.Stub<A>()).Repeat.Onc...

实例 - 相关标签