Tag: generics

数组的开放generics类型是什么?

当我执行int[] , string[] , T[] – 这是一个通用数组。 数组就像其他所有对象一样。 那么[]的实际开放generics类型是什么? 我假设它只是像Array类的一些语法糖,但我无法找到任何类型的东西。 如果你能在Jon Skeet之前以某种方式回答这个问题,可以获得奖励积分。

初始化未知类型的通用对象

如何初始化包含类型可能不同的通用对象的列表? 例如,我有以下内容: this.Wheres = new List<Where>(); 如您所知,是无效的语法。 但是,有时传递给Where的类型将是一个字符串,有时它将是DateTime等。我尝试使用object作为初始化类型,但这也不起作用。

反思和generics获得财产的价值

我试图实现一个允许输出csv文件的通用方法 public static void WriteToCsv(List list) where T : new() { const string attachment = “attachment; filename=PersonList.csv”; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader(“content-disposition”, attachment); HttpContext.Current.Response.ContentType = “text/csv”; HttpContext.Current.Response.AddHeader(“Pragma”, “public”); bool isFirstRow = true; foreach (T item in list) { //Get public properties PropertyInfo[] propertyInfo = item.GetType().GetProperties(); while (isFirstRow) { WriteColumnName(propertyInfo); isFirstRow = false; } Type type = […]

C#generics类的隐式转换

我有一个结构化为服务层的应用程序,它使用存储库层来实现持久性。 我正在尝试创建一个通用控制器类来重用共享行为,但我在尝试设置通用参数时遇到了麻烦。 以下代码: public class BusinessEntity { } public class Person : BusinessEntity { } public interface IRepository where T : BusinessEntity { } public interface IService where T : BusinessEntity where R : IRepository { } public partial interface IPersonRepository : IRepository { } public interface IPersonService : IService { } public abstract class […]

IsOrderedBy扩展方法

在我的一些测试中,我需要检查列表的顺序并执行类似的操作 DateTime lastDate = new DateTime(2009, 10, 1); foreach (DueAssigmentViewModel assignment in _dueAssigments) { if (assignment.DueDate < lastDate) { Assert.Fail("Not Correctly Ordered"); } lastDate = assignment.DueDate; } 我想做什么我把它变成IEnumerable上的扩展方法,使其可重用。 我的初衷是这个 public static bool IsOrderedBy(this IEnumerable value, TestType initalValue) { TestType lastValue = initalValue; foreach (T enumerable in value) { if(enumerable < lastValue) { return false; } […]

从堆栈中的类获取generics参数

我有一个名为Repository的generics类。 该类有一个函数,通过使用不同的generics参数初始化Repository类的新实例来“调用自身”。 这个“递归”可以继续 – 所以为了避免StackOverflowException,我需要检查堆栈中是否存在一个从Repository类调用的具有相同generics参数的方法。 这是我的代码: StackTrace stack = new StackTrace(); StackFrame[] frames = stack.GetFrames(); foreach (StackFrame frame in frames) { Type callingMethodClassType = frame.GetMethod().DeclaringType; if (callingMethodClassType.IsGenericType) { // BUG HERE in getting generic arguments of the class in stack Type genericType = callingMethodClassType.GetGenericArguments()[0]; if (genericType.Equals(entityType)) { wasAlready = true; break; } } } generics类型始终返回为T而不是正确的类型,如“User”或“Employee”(例如)。 […]

xml使用它的generics类型序列化generics类

我需要将包含Pair类型的对象的列表序列化为xml。 除了这些值,我还需要序列化它的generics类型( T和U的类型)。 首先,我创建了一个类PairList来保存对的列表,然后我创建了实际的类,它表示一对两个值,键和值。 [XmlRoot(“pairList”)] public class PairList{ [XmlElement(“element”)] public List<Pair> list; public PairList() { list = new List<Pair>(); } } public class Pair { [XmlAttribute(“key”)] public T key; [XmlAttribute(“value”)] public U value; [XmlAttribute(“T-Type”)] public Type ttype; [XmlAttribute(“U-Type”)] public Type utype; public Pair() { } public Pair(T t, U u) { key = t; value […]

在ICollection <Tuple >上实现.Contains()的最简单方法

假设我有一个Dictionary<String, Tuple> ,我想确定任何一个字典值是否有T1的T1。 我怎样才能最优雅地做到这一点? LINQ?

使用通用接口约束时的协方差/逆差异难题

public interface IShape{} public class Rectangle : IShape{} public class Base{} public class Derived : Base{} public interface IFoo where T : IShape where U : Base { T Convert(U myType); } public class MyFoo : IFoo { public Rectangle Convert(Derived myType) { throw new NotImplementedException(); } } class Program { static void Main(string[] args) […]

如何返回具有通用属性的C#Web服务?

我正在创建一个Web服务,并希望使用返回数据更优雅,而不是拥有消费者需要检查的许多属性。 根据幕后生成的数据,我需要能够返回错误数据或消费者期望的数据。 我不想拥有一个大的扁平对象,并在需要时填充属性,并让用户检查“成功”标志,我希望单个属性Data可以是错误类的实例,也可以是一个成功的阶级。 这就是我想要做的事情: class ItemResponse { public bool Success { get; set; } public T Data{ get; set; } } if( /*acceptance criteria*/ ) { ItemResponse resp = new ItemResponse(); resp.Data = new SuccessData(); } else { ItemResponse resp = new ItemResponse(); resp.Data = new ErrorData(); } return resp; public class SuccessData { } […]