Tag: 构造

构造函数链接中间变量

我有以下的重载构造函数,我正在努力找到一个很好的解决方案。 我看不到如何使用构造函数链接的中间赋值。 以下内容无效,但显示了我想要做的事情 public MyThing(IServiceLocator services, int? userId) { // blah…. } public MyThing(IServiceLocator services, string userName) { User user = services.UserService.GetUserByName(userName); int userId = user == null ? null : (int?)user.Id; // call the other constructor this(services, userId); } 我知道在有效代码中编写上述内容的唯一方法是 public MyThing(IServiceLocator services, string userName) : this(services, services.UserService.GetUserByName(userName) == null ? null : (int?)services.UserService.GetUserByName(userName).Id) […]

找不到类型的构造函数

exception消息: Constructor on type StateLog not found 。 我有以下代码,不适用于只有一个类: List list = new List(); string line; string[] lines; HttpWebResponse resp = (HttpWebResponse)HttpWebRequest.Create(requestURL).GetResponse(); using (var reader = new StreamReader(resp.GetResponseStream())) { while ((line = reader.ReadLine()) != null) { lines = line.Split(splitParams); list.Add((T)Activator.CreateInstance(typeof(T), lines)); } } 它不起作用的类的构造函数与其工作的其他类完全相同。 唯一的区别是这个类将传递16个参数而不是2-5。 构造函数看起来像这样: public StateLog(string[] line) { try { SessionID = long.Parse(line[0]); […]

:C#构造函数中的this(foo)语法?

我时不时地碰到我以前见过的语法,但从未使用过。 这是其中一次。 有人可以按照C#构造函数方法解释“:this”或“:base”的用途吗? 例如: public MyClass(SomeArg arg) : this(new SomethingElse(), arg) { } 我的直觉是它用于将默认参数映射到另一个构造函数方法。