Tag: generics

如何在界面中使用不同的模板参数两次使用相同的界面?

我认为这个例子会更清楚。 我们希望在处理器类中看到两种具有不同参数的方法。 “int Process(int value);” “双重过程(双重价值);” 但是编译器说IR91:’Generics.IRoot’不能同时实现’Generics.IProcess’和’Generics.IProcess’,因为它们可能统一了某些类型参数替换。 public class Processor : IRoot { // Here we want 2 methods public int Process(int item) { } public double Process(double item) { } } public interface IProcess { TResult Process(TItem item); } public interface IRoot : IProcess, IProcess { }

当值类型可以相互转换时,为什么我不能将一个值类型的字典转换为另一个值类型的字典?

可能重复: 在C#中,为什么List 对象不能存储在List 变量中 以下为什么不工作? List castMe = new List(); IEnumerable getFromCast = (IEnumerable)castMe; // allowed. Dictionary<int, List> castMeDict = new Dictionary<int, List>(); Dictionary<int, IEnumerable> getFromDict = (Dictionary<int, IEnumerable>)castMeDict; // Not allowed 这是Dictionary铸造机制中的缺陷,还是我认为应该允许这样做? 谢谢。

隐式和显式委托创建之间的区别(有和没有generics)

请参阅下面的Go()方法中的四行: delegate void Action(T arg); delegate void Action(); void DoSomething(Action action) { //… } void DoSomething(Action action) { //… } void MyAction(T arg) { //… } void MyAction() { //… } void Go() { DoSomething(MyAction); // throws compiler error – why? DoSomething(new Action(MyAction)); // no problems here DoSomething(MyAction); // what’s the difference between this… DoSomething(new […]

使用队列创建BackgroundWorker

我需要创建队列并将其与BackgroundWorker一起使用。 所以我可以添加操作,然后在下一步完成时将在后台启动。 我通过谷歌找到了这个代码: public class QueuedBackgroundWorker { public void QueueWorkItem( Queue queue, T inputArgument, Func doWork, Action workerCompleted) { if (queue == null) throw new ArgumentNullException(“queue”); BackgroundWorker bw = new BackgroundWorker(); bw.WorkerReportsProgress = false; bw.WorkerSupportsCancellation = false; bw.DoWork += (sender, args) => { if (doWork != null) { args.Result = doWork(new DoWorkArgument((T)args.Argument)); } }; bw.RunWorkerCompleted […]

在C#4.0中,是否可以从generics类型参数派生类?

我一直在尝试这个,但我似乎无法解决这个问题。 我想做这个… public abstract class SingletonType : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton _singleton; public static TSingleton Singleton => _singleton ?? (_singleton = new TSingleton()); } 计划是像这样使用它,它可以在基类周围“包裹”单例模式…… public class SingletonFoo : SingletonType { } 但是,我一直这样做 无法从’TBaseClass’派生,因为它是一个类型参数 嗯……我认为类型正是你从中得到的! 那么我错过了什么? 注意:这当然是一个简单的例子,因为它没有添加任何有用的东西,但是假设SingletonType有许多与问题无关的其他逻辑,因此忽略了关注手头的问题。

通用扩展方法:无法从用法推断出类型参数

我正在尝试创建一个适用于类型化数据表的通用扩展方法: public static class Extensions { public static TableType DoSomething(this TableType table, param Expression<Func>[] predicates) where TableType : TypedTableBase where RowType : DataRow { // do something to each row of the table where the row matches the predicates return table; } [STAThread] public static void main() { MyTypedDataSet.MyTypedDataTable table = getDefaultTable(); } public static […]

为什么这个generics在编译时没有解决?

我有以下代码。 我希望它打印: A B C DONE 相反它打印 P P P DONE 为什么? UPDATE 我不是要求解决方案。 我想知道为什么会这样。 我认为generics在编译时得到了解决。 从我可以告诉它应该能够在编译时解决这些正确的方法,但显然它不是,我不明白为什么。 我正在寻找解释原因,而不是解决方案。 这是代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication50 { class Parent { public string FieldName { get; set; } public string Id { get; set; } } class ChildA : Parent { public string […]

.NET 4.0协方差

在回答另一个问题时,我试图做到以下几点。 我不认为我正确地解释了这个问题,但我确实想知道下面是否有可能(我的尝试失败了),如果不是为什么不: public class MyBaseClass { } public class MyClass : MyBaseClass { } public class B { } public class A : B { } static void Main(string[] args) { // Does not compile B myVar = new A(); } 我认为这可以使用带有协变类型参数的通用接口来工作: interface IB { } public class B : IB { } 但我错了,那也行不通。 编辑 […]

将具体对象类型作为generics方法的参数传递

我有一个使用通用方法的API如下 public static class DataProvider { public static Boolean DeleteDataObject(Guid uid, IDbConnection dbConnection) { // Do something here } public static IDbConnection GetConnection() { // Get connection } } 我的应用程序包含在运行时使用CodeDOM生成的类,为了跟踪我创建了一个名为IDataObject的接口。 我试图将每个对象的具体类型传递给上面的generics方法,如下所示: public static Boolean PurgeDataObject(this IDataObject dataObject, Guid uid) { return DataProvider.DeleteDataObject(uid, DataProvider.GetConnection()); } dataObject包含从IDataObjectinheritance的类的实例。 我有兴趣获得该类型并将其作为T传递。 我试图找出是否有可能在某种程度上使用dynamic这里。 typeof()和GetType()不能像Here中所述那样工作

通用局部视图:如何将generics类设置为模型?

我正在尝试在ASP.NET MVC应用程序中构建通用网格视图。 让我解释一些代码: public interface ITrustGrid { IPagedList Elements { get; set; } IList<IColumn> Columns { get; set; } IList Headers { get; } } 这是一个类的接口,允许我在控制器中设置列和表达式。 我将实现传递给部分视图,如下所示: <% Html.RenderPartial("SimpleTrustGridViewer", ViewData["employeeGrid"] as TrustGrid); %> 问题是我无法弄清楚如何使局部视图呈现网格通用。 换句话说,我想转此: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid>” %> 变成这样的东西: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ITrustGrid>” %> =>如何以最简单的方式使我的部分视图通用? 编辑: 我通过使用具有公共TrustGrid GetTrustGrid()方法的TrustGridBuilder解决了这个问题,该方法返回非genericsTrustGrid。 TrustGrid包含字符串而不是linq的东西。 所以我在GetTrustGrid()方法中执行linq并将字符串放在TrustGrid对象中。 感谢大家帮助我走上正轨。