Tag: generics

Dictionary :如何使用T作为Func的generics类型?

我不知道如何清楚地表达它。 我有这个界面: interface IConverter { Dictionary<Type, Func> ConversionMethods { get; } } 基本上,它定义了一个契约,说实现它的类应该为它使用的所有自定义类型(无论是枚举还是其他任何东西)提供转换方法。 是否有可能将Func的generics类型中的object替换为相应的字典键的类型(因此不可能有两种不匹配的类型)? 我认为这是不可能的,但替代方案有点烦人(使用dynamic或object ,创建专门的字典……)。 编辑1:虚构的使用例子 interface IConverter { Dictionary<Type, Func> GetConversionMethods(); } enum A { AA,AB,AC } enum B { BA, BB, BC } class blah : IConverter { public Dictionary<Type, Func> GetConversionMethods() { var d = new Dictionary<Type, Func> { { typeof(A), […]

从另一个generics类添加generics方法约束

我不确定标题是否反映了我的意思,但…… 假设我有两个类, Entity和Component : public abstract class Entity { private List _components = new List(); public void AddComponent() where T : Component { T component = (T)Activator.CreateInstance(typeof(T)); component.Owner = this; _components.Add(component); } } public abstract class Component { public Entity Owner { get; protected set; } public abstract void Update(); } 正如您可能注意到的,上面的类是abstract classes ,意思是不打算直接使用。 但是,在开发的后期阶段,我知道某些Component需要的function只能通过inheritance到Entity类的特定类来附加/添加。 […]

使用动态类型调用generics扩展方法

我正在尝试执行返回类型为T的对象的扩展方法,但我正在尝试根据Header / Detail动态generics类型来设置类型T dynamic。 这可能有点冗长…… using System; using System.Collections.Generic; namespace Blah { public interface IHeader { string Name { get; set; } IDetail Detail { get; set; } } public interface IDetail { //Nothing the ‘Real’ implementation of this //interface will have it’s own properties. } public class GenericHeader : IHeader { public string Name […]

返回简单类型的通用方法

好的,我有一些我不太喜欢的代码。 鉴于我没有写它而只是inheritance它,我觉得我可以把它清理一下。 基本上,我们有一个NHibernate持久设置的对象。 我不会厌倦了实现细节,只是说它包含一个字典,其中包含我们在数据库中保存的实际值。 我们的典型属性实现如下所示: public string MyDumbProperty { get { return GetStringValue(“MyDumbProperty”, string.Empty); } set { SetValue(“MyDumbProperty”, value);} } 现在,从上面的实现可能很明显,我们为每个返回的类型(字符串,整数,长整数,浮点数等)分别使用Getxxxx方法,第二个参数是默认值。 我想要的是能够做如下的事情: public string MyDumbProperty { get { return GetValue(“MyDumbProperty”, string.Empty); } set { SetValue(“MyDumbProperty”, value);} } 我有几个理由想要这样做。 最重要的一点是,我个人不喜欢根据它们采用的参数类型命名的方法。 第二个是我想为这个垃圾制作一个resharper模板。 然后我可以填写属性的名称和类型并完成(假设resharper可以为该类型提供合理的默认值)。 我曾想过制作一个返回T的generics方法GetValue,但我不确定如何设置generics约束,所以这是可行的。 从这里返回的类型都是基本类型(字符串,整数等)。 有一对返回数组,但我可以总是做些不同的事情。 无论如何,这不是一个高度优先的问题,但每次看到它都会让我很烦。 我认为应该有更好的方法来做到这一点。 我想第一步就是重命名各种方法。 由于它们在第二个参数中传递的默认值类型不同,我至少可以将它们强制为相同的名称。 但我能做得更好吗? 如果我能够摆脱每种方法中多个重复代码的位置,那将是很好的,这些代码只是在使用的数据类型方面不同(TryParse在所有数字类型中)。

将列入字典

我有一个List ,它有3个属性A,B,C,我需要的是将这个List转换为Dictionary,结果看起来像 Dictionary (Property name) A = Value of A (Property name) B = Value of B (Property name) C = Value of C 请建议……

类型约束

我有以下类层次结构。 class Header { IEnumerable Item { get; set; } …. } class HeaderA : Header { …. } // Item should have the type of IEnumerable class HeaderB : Header { …. } // Item should have the type of IEnumerable class Item { …. } class ItemA : Item { …. } class […]

如何使用通用存储库模式按类型有条件地过滤IQueryable

我的通用存储库中有一个返回IQueryable : public virtual IQueryable All { get { DbSet set = Context.Set(); if (typeof(T).IsSubclassOf(typeof(OrganisationDependent))) return set.AsEnumerable() .Cast() .Where(x => x.OrganisationID == CurrentOrganisationID) .AsQueryable() .Cast(); return set; } } if语句的原因是大多数但不是所有表都有OrganisationID ,我想确保用户只看到他们所属组织的数据。 上面的代码工作,但为了使它工作,我不得不添加AsEnumerable()将数据拉入内存。 没有它,我收到错误消息 “无法将类型’Models.xxx’强制转换为’Models.OrganisationDependent’.LINQ to Entities仅支持转换EDM原语或枚举类型” 我的所有实体都直接inheritanceModelBase或OrganisationDependent : public abstract class OrganisationDependent : ModelBase { public int OrganisationID { get; set; } public virtual Organisation […]

Windows窗体中通用事件处理程序的解决方法

很久以前,我注意到Visual Studio的Windows窗体编辑器不支持包含generics类型参数的事件。 例如,像这样的事件 public event EventHandler<ListEventArgs> MyStrangeEvent { add { … } remove { … } } 哪里 public class ListEventArgs : EventArgs { List args; } 甚至没有出现在Visual Studio的属性管理器的事件列表中。 现在,这是一个有点人为的例子,可以通过重写类及其事件轻松修改以在Visual Studio中工作。 但是,我目前正在开发一个项目,由于兼容性原因我无法更改某些类。 我唯一能做的就是改变用户控件的事件。 此控件的事件当前如下所示: public event EventHandler<Plane.DrawingErrorEventArgs> DrawingError { add { _Plane.DrawingError += value; } remove { _Plane.DrawingError -= value; } } 请注意,无法更改基础Plane类(由_Plane实例表示,它是受保护的字段)。 它的DrawingError事件及其EventArgs类型在Plane类中声明如下: public […]

最佳实践 – 从C#中的generics集合中删除项目

我在Visual Studio 2008中使用C#和.NET 3.5。 我有一个通用字典,它将事件类型映射到通用的订阅者列表。 订阅者可以订阅多个事件。 private static Dictionary<EventType, List> _subscriptions; 要从订阅列表中删除订阅者,我可以使用这两个选项中的任何一个。 选项1: ISubscriber subscriber; // defined elsewhere foreach (EventType event in _subscriptions.Keys) { if (_subscriptions[event].Contains(subscriber)) { _subscriptions[event].Remove(subscriber); } } 选项2: ISubscriber subscriber; // defined elsewhere foreach (EventType event in _subscriptions.Keys) { _subscriptions[event].Remove(subscriber); } 我有两个问题。 首先,请注意选项1在删除项目之前检查是否存在,而选项2使用powershell删除,因为Remove()不会抛出exception。 在这两个中,这是首选的“最佳实践”方式吗? 第二,是否有另一种“更干净”,更优雅的方式来实现这一点,可能使用lambda表达式或使用LINQ扩展? 我仍然适应这两个function。 谢谢。 编辑 为了澄清,我意识到选项1和2之间的选择是速度(选项2)与可维护性(选项1)的选择。 在这种特殊情况下,我不一定会尝试优化代码,尽管这肯定是值得考虑的。 我想要了解的是,如果这样做有一个普遍成熟的做法。 如果没有,您将在自己的代码中使用哪个选项?

在generics类上使用“partial”

大家好, 我正在使用一个名为ViewModelCollection的generics类,它处理ViewModel列表并提供标准的add()和delete()命令。 现在我想知道我是否可以使用partial构造为某个ViewModel“扩展”这个类,其名称是CarViewModel 。 这样的事情可能吗? partial class ViewModelCollection { … some command and list stuff … } partial class ViewModelCollection { … special commands for car view model }