【c#-返回属性的另一个类有什么好处,而不是在构造函数中设置该属性?】教程文章相关的互联网学习教程文章

c# – 如何构造函数 – 注入仅在运行时已知的字符串? (温莎城堡)【代码】

我有一个依赖于字符串的类:public class Person {private readonly string _name;public Person(string name){if (name == null) throw new ArgumentNullException("name");_name = name;} }该字符串’name’仅在运行时才知道,例如.它在配置中定义.所以我有这个提供这个字符串的接口:public interface IConfiguration {string Name { get; } }这两种类型,Person和IConfiguration(其实现在这里并不重要)都在Windsor容器中注册. 问...

c# – 当构造函数使用1个参数时发生了什么,但base关键字使用2个参数【代码】

我有一些代码,它将演示Liskov替换,但我很困惑base关键字在2个参数上做了什么.谁能解释一下?class Rectangle {public Rectangle(int width, int height){Width = width;Height = height;}public virtual int Height {get;set;}public virtual int Width {get;set;}public int Area{get { return Height*Width } }现在,对于使用2个参数继承基类的square类.我也很好奇为什么下一个方法广场(int)可以在基类中使用不同名称的方法privat...

c# – Azure Worker角色 – 使用OnStart()与构造函数进行一次初始化

阅读各种文档和博客,听起来像OnStart是在角色开始接收流量之前初始化您需要的对象和事物的地方.但是,我不清楚是否存在使用worker角色类的相同实例可以多次调用OnStart的情况? 例如,我有一个注入容器和一个应该只创建一次的数据库连接.我需要确保角色无法停止并再次启动,将所有当前对象保留在内存中.在这种情况下,使用worker角色构造函数初始化对象似乎更好.解决方法:OnStart()(RoleEntryPoint类的成员,以及需要覆盖的方法)仅在角色...

c# – Api Controller具有无参数的公共构造函数错误【代码】

我使用Unity来做我的DI,我遇到了以下错误: – “尝试创建’UploadController’类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.” 我有以下UnityResolver: – public class UnityResolver : IDependencyResolver, IDependencyScope, System.Web.Http.Dependencies.IDependencyResolver {protected IUnityContainer container;public UnityResolver(IUnityContainer container){if (container == null){throw new Ar...

c# – 如何在我自己的实用程序类中访问TempData?或者TempData在构造函数中为null【代码】

我在一些观察/动作中使用TempData,但我想把它提取到某个类中.问题是如果我尝试在Controller的构造函数中创建我的类,那么TempDate就是null.更好的是,我想让我的课程可以注入Controller.所以我需要在创建类时访问TempData. 那么如何在一个单独的类中构造这个TempData呢? 这是ASP.NET Core 2.0 Web应用程序.解决方法:只需在需要的地方注入ITempDataDictionaryFactory,即可在任何地方访问临时数据.然后,您可以调用其GetTempData,它返回...

c# – 静态字段初始值设定项未在实例构造函数之前运行【代码】

我有以下课程:public class AssignmentStatusCode {public static AssignmentStatusCode Pending { get; } = new AssignmentStatusCode("P");public static AssignmentStatusCode Rejected { get; } = new AssignmentStatusCode("R");public static AssignmentStatusCode Approved { get; } = new AssignmentStatusCode("A");public static implicit operator string(AssignmentStatusCode assignmentStatusCode){return assignm...

c# – 当构造函数使用给定第一个构造函数的对象调用另一个构造函数时,可以检查null吗?【代码】

如果我有一个包含两个构造函数的类,如下所示:class Foo {public Foo(string name){...}public Foo(Bar bar): base(bar.name){...} }有没有什么方法可以在我得到空引用异常之前检查bar是否为null?解决方法:您可以使用静态方法执行此操作:class Foo {public Foo(string name) {...}public Foo(Bar bar): base(GetName(bar)) {...}static string GetName(Bar bar) {if(bar == null) {// or whatever you want ...throw new Argumen...

c# – 使用StreamWriter构造函数和File.CreateText之间的区别【代码】

有什么区别(CPU使用率,MSIL等):StreamWriter sw = new StreamWriter("C:\test.txt");和:StreamWriter sw = File.CreateText("C:\test.txt");?解决方法:不多……(通过Reflector)[SecuritySafeCritical] public static StreamWriter CreateText(string path) {if (path == null){throw new ArgumentNullException("path");}return new StreamWriter(path, false); // append=false is the default anyway }虽然我更喜欢使用File....

c# – 在Unity配置中,如何将connectionString传递给构造函数?【代码】

我在web.config中设置统一配置,我有一个类型,我想传递给它的连接字符串已经存在于同一个web.config文件中.<connectionStrings><add name="DatabaseConnectionString" connectionString="metadata=res://*/Database.csdl|res://*/Database.ssdl|....." providerName="System.Data.EntityClient" /></connectionStrings>在统一部分有:<type type="IDatabase" mapTo="Database" ><constructor><param name="connectionString" ><valu...

c# – 通用约束:强制类型具有静态函数和带参数的构造函数【代码】

我知道你可以写:class GenericClass<T> where T : new() { }强制执行T有一个空构造函数. 我的Qs是: >你能强制说T有一个具有特定参数类型的构造函数吗?喜欢:class SingletonFactoryWithEmptyConstructor<T> where T : new(int)>你能强制执行T有一个静态函数(比方说,void F()),这样你就可以在泛型类中使用这个函数了吗?喜欢 :class GenericClass<T> where T : void F() { void G (){T.F();} }我知道你可以指定T实现一个接口,但...

c# – 在IIS上托管WCF Web服务时显式调用服务构造函数【代码】

我想在Microsoft IIS(IIS主机)上托管我的WCF服务. 为此,我创建了我的服务:// The service public class MyService : IMyService {// Ctorspublic MyService() {// Def ctor: I don't want to call it}public MyService(...) : this() {// Parametric ctor, I want to call it!}... }// The contract [ServiceContract] public interface IMyService {... }我创建了一个svc文件(一种为我的服务提供基地址的好方法):<@ServiceHost ...

C#:在构造函数中使用它【代码】

在C#中可以在构造函数中使用它吗? (即,在构造函数中引用实例是否可以) 举个简单的例子:public class Test{public Test(){Type type = this.GetType()}}解决方法:是的,您可以在构造函数中使用它,但不能在字段初始值设定项中使用它. 所以这是无效的:class Foo {private int _bar = this.GetBar();int GetBar(){return 42;} }但这是允许的:class Foo {private int _bar;public Foo(){_bar = this.GetBar();}int GetBar(){return 4...

c# – Struct隐式默认构造函数与无参数构造函数

好的,我们听说struct不能有一个默认的无参数构造函数,这很好(https://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net ).但文档说“每个值类型都有一个隐式默认构造函数,用于初始化该类型的默认值.”从http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx 现在隐式默认构造函数和无参数默认构造函数之间有什么区别?解决方法:隐式默认构造函数是无参数构造函数,它由编译器自...

c# – 通用构造函数和继承【代码】

我有一个带有类约束的泛型类.public class MyContainer<T> where T : MyBaseRowMyBaseRow是一个抽象类,我也希望包含一些MyContainer的成员.public abstract class MyBaseRow {public MyContainer<MyBaseRow> ParentContainer;public MyBaseRow(MyContainer<MyBaseRow> parentContainer){ParentContainer = parentContainer;} }我遇到了从MyBaseRow继承的类的构造函数的问题,例如.public class MyInheritedRowA : MyBaseRow {public...

c# – 默认构造函数创建的`Dictionary`是否使用哈希码?

我需要使用我编写的类作为Dictionary的键的类型 我读了documentation on MSDN about the default constructor of DictionaryDictionary<TKey, TValue> requires an equality implementation todetermine whether keys are equal. This constructor uses the defaultgeneric equality comparer, EqualityComparer<T>.Default. If type TKey implements the System.IEquatable<T> generic interface, thedefault equality comparer u...

构造函数 - 相关标签