【C# 构造函数中调用虚方法的问题】教程文章相关的互联网学习教程文章

c# – 如何使用Unity将Type而不是实例注入构造函数【代码】

是否可以将类型注入构造函数而不是实例?如果是这样,这是如何实现的?我想避免显式注册工厂方法或尽可能原位解析实例.public interface IJob { } public class TheJob : IJob { }public interface IService {string GetTypeDesc(); }public class Service : IService {private Type _jobType;public Service(Type jobType){_jobType = jobType;}public string GetTypeDesc(){return _jobType.ToString();} }即使在使用显式构造函数...

c# – 带构造函数参数的Moq【代码】

我有一个如下所示的工厂:public IFoo GetFoo(IFile file) {return _kernel.Get<IFoo>(new ConstructorArgument("file", file)); }它工作正常,直到我使用Moq模拟IFoo.在mock中没有名为file的构造函数参数,我得到一个Ninject.ActivationException. 我该怎么解决这个问题?解决方法:你应该在测试期间嘲笑你的工厂.希望“GetFoo”方法是工厂界面的一部分.模拟工厂,然后你可以设置工厂返回你想要的任何IFoo(测试IFoo或模拟IFoo).

c# – 表达式使用参数调用构造函数并设置其值【代码】

我试图从表达式调用参数化构造函数而不是使用默认的ctor.这是获取构造函数参数的代码:ConstructorInfo ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new[] { typeof(bool) }, new ParameterModifier[] { }); ParameterInfo[] paramsInfo = ci.GetParameters();//create a single param of type object[] ParameterExpression param = Expression.Parameter(typeof(...

c# – 为什么TcpClient与构造函数中的参数连接速度较慢?【代码】

我有System.Net.Sockets.TcpClient的问题. 一个简单的测试应用程序只是打开一个连接,发送一些数据,然后关闭.另一端有一个简单的服务器,性能很好. 代码看起来像这样:var client = new TcpClient("localhost", 1234); using (var stream = client.GetStream()) using (var writer = new StreamWriter(stream)) {writer.Write("foo");writer.flush(); } client.Close();它工作正常,但我注意到单元测试正在进行>运行1000ms.当我把它放...

从Type with Reflection获取类并使用Type in C#调用泛型构造函数【代码】

我正在使用Dapper,我想迭代我的模型类,并为任何具有ColumnAttribute修饰的字段的类设置类型映射.public class ColumnAttributeTypeMapper<T> : FallbackTypeMapper {public static readonly string ColumnAttributeName = "ColumnAttribute";public ColumnAttributeTypeMapper(): base(new SqlMapper.ITypeMap[]{new CustomPropertyTypeMap(typeof (T), SelectProperty),new DefaultTypeMap(typeof (T))}){}// implementation of S...

c# – 如何使用JustMock模拟构造函数?【代码】

我正在尝试测试以下代码:public ICollection<RawCatalog> ReadCatalog(string familyName) {// Root folder for the familystring familyFolder = this.GetFamilyFolder(familyName);DirectoryInfo familyFolderInfo = new DirectoryInfo(familyFolder);foreach (DirectoryInfo subFamilyFolderInfo in familyFolderInfo.EnumerateDirectories()){// Do stuff} }我希望这会奏效:// Arrange DirectoryInfo fakeDirectoryInfo = Mo...

从部分类c#调用Base实体构造函数【代码】

考虑以下情况, 我有DB First方法创建的实体,// auto-generated class public partial DBEntity : DBContext {public DBEntity (): base("name=DBEntity ") // gets connection string from api.config{} }我有时需要构建并传递连接字符串,所以我创建了这个,public partial class DBEntity {public DBEntity (String connectionString){if (string.IsNullOrEmpty(connectionString))new DBEntity ();elsebase(connectionString); /...

c# – 将依赖项作为方法参数而不是构造函数参数注入【代码】

我们可以使用MEF或Autofac将依赖项作为方法参数而不是构造函数参数注入吗? 谢谢解决方法:我不知道MEF,因为我从未使用它.你可以do it with Unity和with Autofac 统一 从MSFT文档.Unity instantiates dependent objects defined in parameters of methods that carry the InjectionMethod attribute within the scope of the target object. Then it calls the attributed method of the target object before returning the object...

c# – 为什么元组在`[Serializable]`时没有无参数构造函数?

标有[SerializableAttribute]的System.Tuple(T1,..)类(参见msdn).但它们没有无参数构造函数,因此XmlSerializer不能序列化,因为它在here之前已被清除.那么为什么不添加一个呢?解决方法:你不需要一个类型来使用[Serializable]属性来使用XmlSerializer来序列化它,并且一个类型具有该属性的事实并不意味着你可以使用XmlSerializer序列化它;这两者是无关的. [Serializable]仅用于二进制序列化(*)(不需要无参数构造函数). 把它们加起来:...

C# – 添加到现有(生成的)构造函数【代码】

我有一个生成代码中的构造函数.我不想更改生成的代码(因为它在重新生成时会被覆盖),但我需要为构造函数添加一些功能. 这是一些示例代码:// Generated file public partial class MyGeneratedClass {public MyGeneratedClass() {Does some generated stuff} }我能想出的唯一解决方案是:// My hand made file public partial class MyGeneratedClass {public MyGeneratedClass(bool useOtherConstructor):this(){do my added funct...

c# – ProtoBuf-net AsReference需要Activator.CreateInstance中的公共构造函数吗?【代码】

在我的两个类看起来像这样(最小)using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using ProtoBuf;namespace Sandbox {public partial class Form1 : Form{public Form1(){Family family = new Family();Child child1 = new Child(1);Child child2 = new Child(2);Parent parent = new Parent(new L...

c# – 为什么使用TransactionScope的默认构造函数有害?

为什么使用TranscationScope的默认构造函数会有害? 有人能指点我解释一下吗?解决方法:请在MSDN博客using new TransactionScope() Constructor上阅读这篇文章. 摘抄:The TransactionScope’s default constructor is, for the purposes of SQLSever database programming, broken. TransactionScope’s defaultconstructor defaults the isolation level to Serializable and thetimeout to 1 minute. IMO both these settings...

c# – 用于模拟的构造函数重载错误【代码】

我有3个程序集Presenter,Business和DataAccess.演示者引用引用DataAccess的业务和业务. 我有一个CustomerBusiness类,在Business程序集中,有2个单参数构造函数.第一个构造函数仅用于测试以模拟数据访问对象. DataAccess程序集中的ICustomerDataAccess.class CustomerBusiness(){private ICustomerDataAccess _data= null;public CustomerBusiness(ICustomerDataAccess data){_data = data;}public CustomerBusiness(string language...

C#GetConstructor()没有返回父构造函数【代码】

我正在尝试找到具有特定签名的构造函数.此构造函数不存在于当前类型中,但它在其父级中存在.为了显示:public class Base {public Base(){}public Base(string a1, string a2, string a3){...} }public class Child : Base {}问题是,我似乎无法使用带有.GetConstructor的字符串参数找到.ctor,甚至尝试如下:typeof(Child).GetConstructor(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, new ...

c# – 将构造函数参数传递给在InitializeComponent中创建的控件【代码】

InitializeComponent在表单上设置组件,但是对于我创建的Usercontrol,它调用默认构造函数但我想在usercontrol上调用我自己的构造函数(带参数).样板代码说不要编辑内容,那么最好的方法是什么?解决方法:您需要创建一个TypeConverter类,并使用TypeConverterAttribute(typeof(MyTypeConverter))装饰您的UserControl.类型转换器将告诉Visual Studio如何创建类型 – 允许您控制在InitializeComponent中放置的内容.你可以真正深入,并实际编...

构造函数 - 相关标签