Tag: generics

使用多个generics类型参数键入推断

我不明白为什么C#在以下完整情况下不会推断出类型: public interface IThing {} public class Thing1 : IThing {} public class Thing2 : IThing {} public interface IContainer {} public class Container1 : IContainer { public IThing A { get { return new Thing1(); } } public IThing B { get { return new Thing2(); } } } public class Container2 : IContainer […]

使用基类作为IEnumerable 的通用

我对OOP一般,inheritance和多态,接口等有很好的理解。我遇到了一个奇怪的情况,我不明白为什么它根本不起作用… 编辑:好的,我发现协方差(或逆变?)可以解决这个问题,但至关重要 我们还在使用.NET 2.0 如何在不转向C#4.0的情况下解决这个问题? 这是情况。 鉴于这两个类: public class CustomCollectionType : IEnumerable { /* Implementation here, not really important */ } public class Entity : EntityBase { /* Implentation here, not important */ } 当我尝试使用这种通用方法时,编译器会抱怨 public void LoopThrough(IEnumerable entityList) { foreach(EntityBase entity in entityList) { DoSomething(entity); } } 并尝试以这种方式使用它: CustomCollectionType entityList; /* Add items to […]

C#宏观式generics

我正在寻找一种方法来使用generics,这些generics仅在编译时存在以用于代码重用:不必复制/粘贴类和方法。 基本上是文本替换,比如宏,有一些类型检查。 更像是C ++模板。 我问的原因是: 普通的C#generics坚持总是在运行时创建generics类型(为什么??),哪些 1)不仅会产生不必要的限制(例如,不能从类型参数inheritance,这将是非常有用的), 2)但是这些运行时generics类型现在正在为我创建问题,因为.NET无法将它们序列化,因此当我将它们插入到RichTextBox中时,许多操作要么失败要么抛出“无法序列化generics类型”exception。 一切都在使用非generics类型,但我想让代码更通用以添加内容,所以我添加了generics( C#Generic Inheritance workaround ),它们打破了一切。 谢谢。

列出从子到父的分配

我想这样做: List test = new List(); 我class的完整代码是这样的: class Program { public static void Main(string[] args) { List test = new List(); test.Add(new Child()); test.Add(new AnotherChild()); } } class Parent { } class Child : Parent { } class AnotherChild : Parent { } 有人可以告诉我为什么这给了我这个错误: 错误2无法将类型’System.Collections.Generic.List’转换为’System.Collections.Generic.List’d:\ personal \ documents \ visual studio 2010 \ Projects \ […]

具有动态function构造的C#Fluent API

我正在为创建一个具有流畅API的小型SQL库而烦恼,并希望做到这样的事情: var person = connection.GetOne(“select * from [Person] where [Id] = 1”) .WithMany(“select * from [Pet] where [PersonId] = 1”) .WithMany(“select * from [Address] where [PersonId] = 1]”) .Build((person, pets, addresses) => { person.Pets = pets; person.Addresses = addresses; return person; }); 我之前已经构建了大量流畅的API,但所有这些API都变得更加简单,并且不依赖于generics。 我的问题是具体如何实现Build()结束函数。 我不确定它是否可能(看起来不像它但可能使用Expression是关键?)但是如何跟踪调用更高链方法中指定的generics类型(例如GetOne ( ),WithMany ())这样当调用.Build()时,所需的Func 是正确的类型? 在上面的示例中,我希望Func 为Func <Person,IEnumerable ,IEnumerable >,以便开发人员可以以他们需要的任何方式构造根项(person) – […]

如何用存储库模式问题解决这个generics?

我从上一个问题得到了这个代码,但它没有编译: public interface IEntity { // Common to all Data Objects } public interface ICustomer : IEntity { // Specific data for a customer } public interface IRepository : IDisposable where T : IEntity { T Get(TID key); IList GetAll(); void Save (T entity); T Update (T entity); // Common data will be added here […]

将字符串转换为T

我有一个方法需要将字符串转换为generics类型: T GetValue(string name) { string item = getstuff(name); return item converted to T // ???????? } T可以是int或date。

为什么C#generics的专业化有限?

问题是“什么是具体化?” 评论C#的generics: 维护类型信息,通过使用reflection检查类型参数,允许在一定程度上进行特化。 但是, 由于在任何具体化发生之前编译generics类型定义 (这是通过根据类型参数的约束编译定义来完成的 – 因此,编译器必须能够,因此专业化程度是有限的。即使在没有特定类型参数的情况下,“理解”该定义)。 “专业化”是什么意思? 它与具有特定类型参数的generics类型的实例化不同吗? “专业化程度有限”是什么意思? 为什么它“是在任何具体化发生之前编译generics类型定义的结果”?

通用类型推断无法使用动态?

我最近一直在使用Massive,一个返回IEnumerable 集合的Micro-ORM。 当我尝试使用LINQ查询其中一个集合时,我发现了一个意外的问题。 虽然编译器似乎没有任何问题可以解决这个问题,但是即使传递给它的一个参数被声明为动态,string.Format也会返回一个字符串… dynamic dynamicString = “d”; // just using a string here for simplicity, same problem occurs with any other type string explicitString = string.Format(“string is {0}”, dynamicString); // works without issues …在以下情况中似乎无法推断出这一事实: IEnumerable strings = new[] { “a”, “b”, “c” }; IEnumerable dynamics = strings; IEnumerable output = dynamics.Select(d => string.Format(“string is […]

通用列表,使用条件语句计数的项目

我有一个通用列表。 它有一个ListfilesToProcess.Count属性,它返回项目的总数,但我想用条件语句计算列表中的某些项目数。 我是这样做的: int c = 0; foreach (FilesToProcessDataModels item in ListfilesToProcess) { if (item.IsChecked == true) c++; } 是否有任何更短的方式,如int c = ListfilesToProcess.count(item => item.IsChecked == true);