Tag: generics

如何交叉两个不同的IEnumerable集合

我认为这个问题之前已经被问到,但我还没有能够推断出一个明确的答案。 我试图找到最好的方式(或一种方式)来交叉两个完全不同的可相关集合。 A类: int z1 int z2 int z3 字符串z4 B级: int j5 int j6 T j7 T j8 字符串j9 ..我想在z2 == j6上将List与List相交。 可以这样做吗?

如何使用复合键进行字典?

我需要安排一种字典,其中键是一对枚举和int,值是对象。 所以我想将一对映射到某个对象。 一种选择是 public enum SomeEnum { value1, value2 } class Key { public SomeEnum; public int counter; // Do I have to implement Compare here? } Dictionary _myDictionary; 另一个选项是将enum和int转换为某个唯一键。 string key = String.Format(“{0}/{1}”, enumValue, intValue) 这种方法需要字符串解析,还有很多额外的工作。 如何轻松搞定?

C#List .RemoveAll() – 如何删除列表的子集?

我有2个类feed_Auto和具有多个匹配属性的Product 。 对于此特定问题,AutoID是我需要使用的唯一字段。 我有一个List ,有几百个唯一条目。 我有一个小的List有10个,20个唯一条目。 我现在想要从大列表中删除小列表中的所有项目。 如何使用RemoveAll({lambda expression})来完成此任务? 我发现的所有示例都取决于通用列表是简单类型(字符串,整数等)。 private static int DoInserts(ref List source, ref List target, Guid companyID) { List newProductList = new List(); List dropSourceList = new List(); using (var db = Helpers.GetProdDB()) { foreach (var src in source) { var tgt = target.Where(a => a.alternateProductID == src.AutoID && a.LastFeedUpdate 0) […]

generics运行时或编译时多态吗?

我读到以下格式属于参数多态,但是我们可以将它分为一个,运行时或编译时多态吗? public class Stack { // items are of type T, which is known when we create the object T[] items; int count; public void Push(T item) {…} //type of method pop will be decided when we create the object public T Pop() {…} }

在c#中执行此通用抽象类的最佳方法是什么?

我知道我做得不对,但我也知道有办法做到这一点。 我试图尽可能地通用和抽象,否则我的代码将变得非常混乱。 所以我在这里使用策略模式作为GetAggregateClient()方法。 我想要一个名为AbstractAggregate的抽象类,以便它使用generics。 将使用的类型是一系列数据类,即BlogItem,ResourceItem和AskItem。 这些数据类都inheritance自ListItem。 这就是背景信息。 这里的问题是我希望GetAbstractAggregate()返回一个实现AbstractAggregate的客户端类的实例,其中指定的项目类型取决于传入的枚举。但是,我不能返回“AbstractAggregate”。 编译器不会让我这样,因为AbstractAggregateFactory类不是通用的。 有没有人有最好的方法来做到这一点? 非常感谢。 public static class AggregateHelper { public enum AggregateTypes { TankTruckBlog, AskTankTruck, Resources } } public static class AbstractAggregateFactory { public static AbstractAggregate GetAggregateClient(AggregateHelper.AggregateTypes type) { switch (type) { case AggregateHelper.AggregateTypes.AskTankTruck: return new AskTankTruckAggregate(); case AggregateHelper.AggregateTypes.TankTruckBlog: return new TankTruckBlogAggregate(); case AggregateHelper.AggregateTypes.Resources: return new ResourcesAggregate(); default: […]

将ExpandoObject强制转换为匿名类型

我可以将ExpandoObject强制转换为匿名类型吗? var anoObj = new { name = “testName”, email = “testEmail” }; dynamic expandoObj = new System.Dynamic.ExpandoObject(); // Here I’m populating the expandoObj with same property names/types in anonymoustype(anoObj) // Now, how to convert this ExpandoObject to anonymoustype ? var newObj = (typeof(anoObj)expandoObj); // This doesn’t work 稍后添加 //这是我的实体 public class Customer { #region […]

如何使用reflection调用generics类的静态属性?

我有一个类(我无法修改)简化为: public class Foo { public static string MyProperty { get {return “Method: ” + typeof( T ).ToString(); } } } 我想知道当我只有一个System.Type时如何调用这个方法 即 Type myType = typeof( string ); string myProp = ???; Console.WriteLinte( myMethodResult ); 我试过的: 我知道如何使用reflection实例化generics类: Type myGenericClass = typeof(Foo).MakeGenericType( new Type[] { typeof(string) } ); object o = Activator.CreateInstance( myGenericClass ); 但是,由于我使用静态属性,这是否适合实例化一个类? […]

如何将字符串转换为linq表达式?

类似: 将字符串转换为Linq.Expressions或使用字符串作为选择器? 类似的那个: 将Linq表达式作为字符串传递? 另一个问题有相同的答案: 如何从C#中的字符串创建基于动态lambda的Linq表达式? 询问有这么多类似问题的事情的原因: 这些类似问题中接受的答案是不可接受的,因为它们都引用了4年前的一个库(授予它是由代码大师Scott Gu编写的)为旧框架(.net 3.5)编写的,并且除了链接作为答案。 有一种方法可以在代码中执行此操作,而不包括整个库。 以下是此情况的示例代码: public static void getDynamic(int startingId) where T : class { string classType = typeof(T).ToString(); string classTypeId = classType + “Id”; using (var repo = new Repository()) { Build( repo.getList(), b => b.classTypeId //doesn’t compile, this is the heart of the issue //How can […]

为什么我们不能使用密封类作为通用约束?

你能猜出在generics中不允许使用密封类进行类型约束的原因是什么? 我只有一个解释是给机会使用裸约束。

使用reflection来解决Linqed属性

我正在尝试编写一个通用方法,它将加载具有特定ID的特定类型的记录。 这是一种有效的方法: public abstract class LinqedTable where T : LinqableTable { public static T Get(long ID) { DataContext context = LinqUtils.GetDataContext(); var q = from obj in context.GetTable() where obj.ID == ID select obj; return q.Single(); } } public abstract class LinqableTable { public abstract long ID { get; set; } } 您可以忽略对LinqUtils.GetDataContext()的调用; 这是一个实用函数,我必须处理我的程序中有多个数据上下文的事实。 关键是现在我可以将我的任何类声明为LinqableTable子类,并且我可以通过调用LinqedTable.Get(ID)轻松地实例化该表的记录。 […]