Tag: generics

C#generics中的通配符等价物

假设我有一个generics类,如下所示: public class GeneralPropertyMap { } 在其他一些类中,我有一个方法,它接受一个GeneralPropertyMap数组。 在Java中,为了获取包含任何类型的GeneralPropertyMap的数组,该方法将如下所示: private void TakeGeneralPropertyMap(GeneralPropertyMap[] maps) { } 我们使用通配符,以便稍后我们可以调用TakeGeneralPropertyMap传递一堆具有T任何类型的GeneralPropertyMap ,如下所示: GeneralPropertyMap[] maps = new GeneralPropertyMap[3]; maps[0] = new GeneralPropertyMap(); maps[1] = new GeneralPropertyMap(); maps[2] = new GeneralPropertyMap(); //And finally pass the array in. TakeGeneralPropertyMap(maps); 我试图在C#中找出一个没有成功的等价物。 有任何想法吗?

从C#中的基类访问派生类的属性

在C#中,当generics列表仅包含基类时,访问派生类的属性的最佳方法是什么。 public class ClassA : BaseClass { public object PropertyA { get; set; } } public class ClassB: BaseClass { public object PropertyB { get; set; } } public class BaseClass { } public void Main { List MyList = new List(); ClassA a = new ClassA(); ClassB b = new ClassB(); MyList.Add(a); MyList.Add(b); for(int […]

拦截异步方法,通过DynamicProxy返回通用Task

我的问题与这篇文章有关拦截使用DynamicProxy调用异步方法 我想实现拦截器,该拦截器使用返回Task或Task结果的异步方法。 我使用下一个代码返回ContinueWith结果(以便在拦截器完成工作时调用方法等待) var task = invocation.ReturnValue as Task; invocation.ReturnValue = task.ContinueWith(c => { code that should execute after method finish }); 上面的代码适用于Task结果,但是在Task结果的情况下, ContinueWith会将返回类型从Task更改为Task 。 我需要调用重载方法ContinueWith返回Task ,但为此我需要将invocation.ReturnValue为Task 我没有找到以任何方式动态投射它的方法。 有谁知道如何制作它? 我也尝试通过reflection调用此方法,但参数是labmda函数,不能直接传递。

更快地替换Dictionary

我需要快速替换System.Collections.Generic.Dictionary 。 我的申请应该非常快。 所以,替换应该支持: generics 加 得到 包含 ……就是这样。 我不需要LINQ或任何东西的任何支持。 它应该很快 。 一个简单的代码如: Stopwatch stopWatch = Stopwatch.StartNew(); Dictionary dictionary = new Dictionary(); dictionary.Add(“fieldName”, “fieldValue”); dictionary.Add(“Title”, “fieldVaaaaaaaaaaaaaaaaalue”); Console.WriteLine(stopWatch.Elapsed); …打印00:00:00.0001274,这对我来说很多时间,因为我的应用程序正在做很多其他的事情,其中​​一些来自旧的慢速库我必须使用并且不依赖于我。 关于如何实施更快速的任何想法? 谢谢。

从C#类型变量初始化通用变量

我有一个类,它将Generic Type作为其初始化的一部分。 public class AnimalContext { public DoAnimalStuff() { //AnimalType Specific Code } } 我现在能做的就是 AnimalContext donkeyContext = new AnimalContext(); AnimalContext orcaContext = new AnimalContext(); 但我需要/想要做的是能够将一个AnimalContext声明为一个只在运行时才知道的类型。 例如, Animal a = MyFavoriteAnimal(); //returns an instance of a class //implementing an animal AnimalContext a_Context = new AnimalContext(); a_Context.DoAnimalStuff(); 这有可能吗? 我似乎无法在网上找到答案。

是否可以在.Net 3.5中进行通用控制?

我声明了以下Generic usercontrol: public partial class MessageBase : UserControl { protected T myEntry; public MessageBase() { InitializeComponent(); } public MessageBase(T newEntry) { InitializeComponent(); myEntry = newEntry; } } } 但编译器不允许我这样做: public partial class MessageControl : MessageBase { public MessageControl() { InitializeComponent(); } } 如何在C#中创建通用用户控件?

如何检查ObjectQuery 表达式树中是否存在OrderBy

我正在使用T4为LINQ to Entities实体生成存储库。 存储库包含(除其他外)适合于分页的List方法。 支持和不支持的方法的文档没有提到它,但你无法“调用” Skip无序的IQueryable 。 它会引发以下exception: System.NotSupportedException:方法’Skip’仅支持LINQ to Entities中的排序输入。 必须在方法’Skip’之前调用’OrderBy’方法。 我通过允许通过部分方法定义默认排序来解决它。 但是我在检查表达式树是否确实包含OrderBy遇到了问题。 我已经将问题减少到尽可能少的代码: public partial class Repository { partial void ProvideDefaultSorting(ref IQueryable currentQuery); public IQueryable List(int startIndex, int count) { IQueryable query = List(); ProvideDefaultSorting(ref query); if (!IsSorted(query)) { query = query.OrderBy(c => c.CategoryID); } return query.Skip(startIndex).Take(count); } public IQueryable List(string sortExpression, int […]

重构代码以避免类型转换

我在.Net 4.0中有以下C#代码。 它需要对IRetailBusiness进行IBusiness的类型转换。 //Type checking if (bus is IRetailBusiness) { //Type casting investmentReturns.Add(new RetailInvestmentReturn((IRetailBusiness)bus)); } if (bus is IIntellectualRights) { investmentReturns.Add(new IntellectualRightsInvestmentReturn((IIntellectualRights)bus)); } 业务场景: 我正在为投资控股公司设计软件系统。 该公司拥有零售业务和IntellectualRights业务。 BookShop和AudioCDShop是零售业务的例子。 EngineDesignPatent和BenzolMedicinePatent是IntellectualRights业务的例子。 这两种业务类型完全不相关。 这家投资公司有一个名为InvestmentReturn的概念(但每个企业对这个概念都完全无知)。 InvestmentReturn是从每个业务获得的利润,并使用ProfitElement进行ProfitElement 。 对于每种“业务类型”(Retail,IntellectualRights),使用的ProfitElement是不同的。 题 如何重构此类设计以避免此type casting和type checking ? 摘要投资 public abstract class InvestmentReturn { public double ProfitElement { get; set; } public IBusiness Business{ get; […]

用于创建集合中所有元素的深层副本的通用方法

我有各种不同对象类型的ObservableCollections。 我想编写一个方法来获取任何这些对象类型的集合,并返回一个新集合,其中每个元素都是给定集合中元素的深层副本。 以下是特定类的示例 private static ObservableCollection DeepCopy(ObservableCollection list) { ObservableCollection newList = new ObservableCollection(); foreach (PropertyValueRow rec in list) { newList.Add((PropertyValueRow)rec.Clone()); } return newList; } 如何为任何实现ICloneable的类使这个方法通用?

如何从逗号分隔的字符串创建List ?

鉴于变量 string ids = Request.QueryString[“ids”]; // “1,2,3,4,5”; 有没有办法将它转换成List而不做类似的事情 List myList = new List(); foreach (string id in ids.Split(‘,’)) { if (int.TryParse(id)) { myList.Add(Convert.ToInt32(id)); } }