Tag: generics

无法从用法中推断出方法的类型参数

namespace TestLibrary { [TestFixture] public class Class1 { public delegate T Initializer(); public static T MyGenericMethod(Initializer initializer) where T : class { return initializer != null ? initializer() : null; } [Test] public void Test() { var result = MyGenericMethod(MyInitializer); Assert.IsNotNull(result); } private object MyInitializer() { return new object(); } } } 在Visual Studion 2010中运行时,这是一段正常运行的代码。如果我尝试使用MSBUILD从命令行构建它… […]

实例化generics类型?

我目前正在研究generics集合类,我想创建一个从集合中返回对象的方法。 如果集合中不存在特定对象,则应创建对象,将其添加到集合中,然后返回。 我遇到了一些问题。 generics集合if表示抽象类的类型,我在实例化时遇到问题。 这是我的class级定义: public class CacheCollection : List<CacheItem> where T : EntityBase 这是我工作的部分完整的方法: public T Item(int Id) { CacheItem result = this.Where(t => t.Entity.Id == Id).First(); if (result == null) //item not yet in cache, load it! { T entity = new T(Id); //design time exception here: Cannot create an instance of the […]

通用方法:使用参数实例化generics类型

我有一个generics方法,它接受一个类型T,我需要能够调用一个构造函数,需要一个XmlNode。 目前,我试图通过一个具有我想要的构造函数的抽象基类来做到这一点(加上一个无参数的基类,所以我不必编辑“子类”而不是添加实际的子类)并通过它来约束。 如果我尝试实例化其中一个类,它会抱怨它: Cannot create an instance of the variable type ‘T’ because it does not have the new() constraint 如果我添加new()约束,我得到: ‘T’: cannot provide arguments when creating an instance of a variable type 我怎么能做我想要的?

无法巧妙地将类型Void转换为类型System.Collections.Generic.IList

尝试返回generics类型并获得标题中描述的错误。 我确信我做的事情很傻 – 建议赞赏…… public static IList GetGroupById(int groupId) { DashboardGroupType type = (DashboardGroupType)groupId; IList result = null; var obj = default(T); switch (type) { case DashboardGroupType.Countries: break; case DashboardGroupType.Customers: // this returns a list of typ IEnumerable obj = (T) CustomerRepository.GetAllCustomers(); break; case DashboardGroupType.Facilities: // this returns a list of typ IEnumerable obj = […]

c#递归reflection和通用列表设置默认属性

我试图使用reflection来实现以下目的: 我需要一个方法,我传入一个对象,这个方法将递归实例化具有子对象的对象,并使用默认值设置属性。 我需要实例化整个对象,根据需要进行多个级别。 此方法需要能够处理具有多个属性的对象,这些属性将是其他对象的通用列表。 这是我的示例代码(当我得到一个包含List的对象时,我得到一个参数计数不匹配exception: private void SetPropertyValues(object obj) { PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo property in properties) { if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && property.PropertyType.FullName.Contains(“BusinessObjects”)) { Type propType = property.PropertyType; var subObject = Activator.CreateInstance(propType); SetPropertyValues(subObject); property.SetValue(obj, subObject, null); } else if (property.PropertyType == typeof(string)) { property.SetValue(obj, property.Name, null); } else if (property.PropertyType […]

我可以创建List <WeakReference >吗?

我正在尝试使用4.5generics实现创建一个WeakReference列表,以便我可以避免类型检查和WeakReference目标的转换。 但是, WeakReference似乎不支持协方差,所以我试图建立一个解决方法。 我认为它应该是可行的,因为每个T都是特定inheritance链中的一种类型。 那么,我在想的就是这样: public class Animal { } public class Tiger : Animal { } public class Wolf : Animal { } var mylist = new List<WeakReference>(); mylist.Add(new WeakReference(new Animal())); mylist.Add(new WeakReference(new Tiger())); mylist.Add(new WeakReference(new Wolf())); 我已经尝试为WeakReference创建一个包装类(因为它是不可inheritance的),但这不起作用。 无论如何,列表将不接受除WeakReference之外的任何类型的WeakReference 。 我可以创建自己的通用WeakReference实现,但这似乎是打败了这一点,因为我在其中进行类型转换。 我找不到任何文档,但我有点假设框架版本更好地处理这个问题。 还有另一种方法可以解决这个问题,我没想到,或者我是在咆哮错误的树?

如何通过LINQ调用函数委托?

我想我在这里问正确的问题…… 我有4个存储过程,它们返回相同数据的不同子集。 我将此数据映射到服务器应用程序中的同一对象。 我将代码设置如下: internal static MyDataContext dc = new MyDataContext(); internal static List getData(DataType data) { List obj = null; switch (data) { case DataType.Type1: obj = mapObj(dc.getDataType1); break; case DateType.Type2: obj = mapObj(dc.getDataType2); break; … } } // This gives me an error that type T cannot be defined // private static List […]

如何在另一个通用基类上添加C#generics类型约束?

我已多次阅读有关C#generics类型参数约束的MSDN文档,但我无法弄清楚如何执行此操作,或确定它是否可行。 假设我有一个像这样的通用基类: public abstract class Entity { … } 这个抽象基类没有任何类型约束, TId可以是任何东西 – 结构,类等。 现在说我有一个通用的接口方法,我想将方法​​的generics类型约束到上面的类: public interface ICommandEntities { void Update(TEntity entity) where TEntity : ?????; } 我可以这个编译: public interface ICommandEntities { void Update(TEntity entity) where TEntity: Entity } …然而,我需要在执行方法时显式添加两个T1 ant T2genericsargs: commander.Update(abcEntity); 如果可能的话,我想让编译器推断所有内容,这样我就可以执行这样的方法: commander.Update(abcEntity); 这个活动可能吗? 到目前为止,我能让它工作的唯一方法是在generics基类之上添加一个空的非generics基类,并将其用作方法的类型约束: public abstract Entity {} public abstract EntityWithId : Entity […]

扩展方法和通用约束的问题

我有一个基本接口和几个inheritance的接口。 基本接口有扩展方法,用于修改对象并返回基类的新实例( IChildA.Touch() => IBase , IBase.Touch() => IBase )。 对于一个inheritance路径( IChildB和后代),我想实现返回与调用对象相同类型的对象的扩展方法( IGrandChildB.Touch() => IGrandChild )。 为此,我想指定一个受限于IChildB后代的通用扩展方法。 这工作到目前为止,但现在编译器无法解析来自IChildA的调用。 它尝试使用IChildB路径的扩展方法,而不是使用IBase接口的扩展方法。 有没有一种优雅的方法来解决这个问题? public interface IBase {} public interface IChildA : IBase {} public interface IChildB : IBase {} public static class BaseExtensions { public static IBase Touch(this IBase self) { return self; } public static T Touch(this […]

C#generics类型约束

这不应该是有效的C#代码吗? class A where T : class { public void DoWork() where K : T { var b = new B(); // <- compile time error } } class B where U : class { } 编译器吐出此错误: 错误CS0452:类型“K”必须是引用类型才能在generics类型或方法“ConsoleApplication1.B”中将其用作参数“U” 编译器是否应该能够确定K是约束为T类型还是从T派生,因此它显然应该是引用类型(T被约束为引用类型)?