【《刻意练习之C#》-0006- 类型系统】教程文章相关的互联网学习教程文章

c# – 需要根据StructureMap中请求的父类型不同地配置依赖关系【代码】

我们正在使用StructureMap,并试图弄清楚如何实现以下目标. 我们有2个接口: ICat,IDog 对于ICat,我们需要使用Cat的实例. Cat构造函数采用ISound的实例. 对于IDog,我们需要使用Dog的实例. Dog构造函数也接受了一个ISound实例. ISound有一个实现AnimalSound. 到现在为止还挺好.这部分我们可以很容易地在结构图中做到. 现在问题出现在AnimalSound上. AnimalSound有一个构造函数,可以使用LoudSound或SoftSound进行初始化. (LoudSound和...

c# – 我可以让StructureMap管理具体类型的生命周期吗?【代码】

我有一个名为TypeA的类型. TypeA实现了2个接口:IOne和ITwopublic class TypeA : IOne, ITwo {public TypeA(IThree ithree){...} }我想配置StructureMap,以便每个线程只有一个TypeA实例.我希望EITHER IOne或ITwo的任何解析都返回TypeA的相同实例(每个线程一个). 我试过以下配置:ObjectFactory.Configure(x => {var thread = Lifecycles.GetLifecycle(InstanceScope.ThreadLocal);x.For<TypeA>().LifecycleIs(thread).Use<TypeA>()...

如何在C#XML摘要中显示泛型类型【代码】

/// <summary> /// Something about this generic method which works with ??? type. /// </summary> /// <typeparam name="T">???</typeparam> /// <returns></returns> public T Foo<T>() {??? }是否可以在摘要标记中编写注释以显示当前给定的泛型类型. (正如visual-studio所做的那样)? (例如,在Foo< string>()的工具提示中,我们可以看到有关此泛型方法的一些内容,它适用于字符串类型.)解决方法:这个标签应该这样做< typeparam...

c# – 具有“out”参数的类似TryXXX的方法与返回可空值类型?【代码】

我经常在C#中看到这样的方法:(做一个可能给出或不给出结果的计算)bool TrySomething(SomeType inputData, out SomeOtherType result) { ... }为什么人们不使用这样的东西呢?Nullable<SomeOtherType> TrySomething(SomeType inputData) { ... }这只是性能差异吗?这是一个结构,所以必须没有堆分配,对吧?或者我错过了什么?解决方法:在C#2.0中引入了Nullable – 就像泛型一样.有很多代码早于此. 说实话,每当出现问题时,我都不会只...

c# – 带有实体框架的ASP.NET MVC – 更新/保存复杂类型属性【代码】

我有3个类,与3个数据库表相关:public class Stat {public int Id { get; set; }public string Name { get; set; }public List<Quantity> Quantities { get; set; } } public class Quantity {public int Id { get; set; }public string Name { get; set; }public virtual Stat Stat { get; set; }public virtual Unit Unit { get; set; } } public class Unit {public int Id { get; set; }public string Name { get; set; }publi...

c# – 如何深度复制强类型集合的成员【代码】

我有两个类XmlPerson和Person,每个类都有公共属性,没有方法也没有任何字段. 我如何将所有属性从Person深度复制到XmlPerson?我不想使用像MiscUtil.PropertyCopy或Automapper这样的第三方库.我已经设法复制了作为原始类型和强类型对象的“第一级”属性,但是当它出现时,我不知道. Person类的结构如下:public class Person {public string FirstName { get; set; }public string Surname { get; set; }public decimal? Salary { get;...

c# – 从程序集中公开不同的类型名称(别名)【代码】

这个问题与previous, unanswered question of mine有关,因为我仍在解决同样的问题;这是一个不同的方法. 我目前正在C#.NET项目中使用第三方SOAP Web服务.不幸的是,生成的客户端代理类和类成员的名称与我当前的约定不一致. 除非另有建议,否则我可以假设Svcutil不支持别名(但是,它似乎最容易在那个级别完成) 我现在正在考虑将Web服务消耗包装到它自己的项目中,然后组装,并简单地引用其他项目中的内容.我正在考虑这个问题,因为可以添加...

c# – Activator.CreateInstance()重载具有不同的返回类型【代码】

我正在使用Activator.CreateInstance在我的项目中构建对象.我注意到,根据您调用的CreateInstance的重载,您可以获得所请求的对象或ObjectHandle. 为什么是这样?这对我来说似乎非常违反直觉.public static Object CreateInstance( Type type )public static ObjectHandle CreateInstance( string assemblyName, string typeName )从here和here开始解决方法:的情况下public static Object CreateInstance( Type type )你有可能实例化...

c# – Azure存储客户端v4.1 – 期望非基本类型的值【代码】

我最近将我的ASP.NET项目(MVC5)升级为使用Storage Library 4.1定位Azure SDK 2.3,当我尝试将任何内容保存到Table Storage时遇到一个奇怪的错误. 错误:An unhandled exception of type ‘Microsoft.WindowsAzure.Storage.StorageException’ occurred in Microsoft.WindowsAzure.Storage.dll Additional information: A primitive value was specified; however, a value of the non-primitive type ” was expected.我的模型通过使...

c# – 类型为T的派生类的序列化【代码】

我有以下类需要序列化public class Boat {public string Brand { get; set; }public string Model { get; set; } }以及派生类public class WindBoat : Boat {public int MaxSpeed { get; set } }public class SpeedBoat<T> : Boat {public int MaxSpeed { get; set; }public Engine<T> Engine { get; set; } }Whan我尝试序列化Boat类,它说我需要为所有可能的子类添加XmlInclude,但是我不能添加SpeedBoat,因为我不知道我将提前有多少...

c# – 使Point3D成为从Point2D派生并实现IPoint3D接口的引用类型的原因?【代码】

抱歉,如果这对Code Review堆栈交换更好,但我认为,因为这个问题并不是专门针对代码,而是更多关于引用和值类型之间的概念差异,这里似乎更好. 搜索给了我这个稍微相关的问题:Best Way of Implementing these 3 classes in C#: Vector, Direction (unit vector), Point – 但它并不完全.另一方面,搜索“参考与值类型”会给出过于宽泛的答案. 最近我开始使用新的C#代码库.有很多现有的代码,其中包括:public interface IPoint2D {doub...

c# – Linq匿名类型成员必须在Sub Query中声明【代码】

我正在尝试将this SQL查询转换为Linq,以在asp MVC中的View中返回, Linq的预期产出:TotalPoints Name231 John 我在第二个选择新的子查询中得到此错误:Invalid expression term ‘.’ Invalid anonymous type member declarator. Anonymous type members mustbe declared with a member assignment, simple name or member access.Linq查询到目前为止:public ActionResult UserTotal(){List<TotalPointsUser> onetotal = fr...

c# – 从枚举中Swagger预定义的返回类型值【代码】

我需要能够告诉Swagger API文档某个API将返回某个枚举的子集. 想象一下,我有一个枚举UserType {User_Not_Found,User_Blocked,User_Duplicated,User_Active ……等等}. 一个api / users / search可能会在找到匹配时返回用户FullName,如果不匹配则返回“User_Not_Found”.return Request.CreateResponse(success ? HttpStatusCode.OK : HttpStatusCode.BadRequest, success ? fullName : UserType.User_Not_Found.ToString());问题:...

c# – 为什么继承的字段不属于最终类型【代码】

我有以下代码public abstract class Parent {AnObject AProperty {get; set;} } public class ChildA : Parent { } public class ChildB : Parent { }当我通过反射访问ChildA的实例时,我看到其成员AProperty的DeclaringType等于Parent.可悲的是,我想依靠反思来确定谁是ChildA,谁是ChildB. 更多上下文:我实际上是尝试通过NInject使用when子句绑定AProperty,以便根据要创建的对象的实际类型进行不同的解析.这是一个简单的例子:Kern...

c# – 在View中使用接口类型作为模型并使用实际类型属性和验证的最佳实践【代码】

我有这样的界面:public interface IFoo {decimal Amount { get; set; } }我有一些视图模型实现它:public class Foo1 : IFoo {[Display(Name = "Foo1 Amount")][Range(6, 11)]public decimal Amount { get; set; } }public class Foo2 : IFoo { [Display(Name = "Foo2 Amount")][Range(1, 5)]public decimal Amount { get; set; } }我不想为Foo1和Foo2中的每一个创建新视图. 所以,我创建了一个具有IFoo类型模型的视图.@mode...