草庐IT

Generics

全部标签

c# - 具有泛型的对象类型中的 "apostrophe + number"是什么意思(例如 "Collection` 1")?

我有一个对象(MyObject)和一个属性(MyProperty)。我想获取它的类型名称(即String或MyClass等)。我使用:PropertyInfopropInfo=typeof(MyObject).GetProperty("MyProperty");Console.WriteLine(propInfo.PropertyType.Name);Console.WriteLine(propInfo.PropertyType.FullName);简单类型没问题,但当MyProperty是通用类型,我在获取它的名称时遇到问题(例如Collection)。它打印:Collection`

c# - 为什么我不能将 List<List<Foo>> 传递给 IEnumerable<IEnumerable<Foo>>

此代码产生两个编译时错误:privatevoidDoSomething(){List>myFoos=GetFoos();UseFoos(myFoos);}privatevoidUseFoos(IEnumerable>){}Thebestoverloadedmethodmatchfor'NameSpace.Class.UseFoos(System.Collections.Generic.IEnumerable>)'hassomeinvalidarguments和Argument1:cannotconvertfrom'System.Collections.Generic.List>'to'

c# - 我可以强制后代拥有无参数构造函数吗?

我正在尝试创建一个类似工厂模式的通用机制。工厂会是这样的:publicclassAPluginwhereActionType:IAction{//createanewaction.Note:ActionTypeshouldcontain//anemptyconstructorpublicActionTypeCreateAction(){returnActivator.CreateInstance();}}IAction的后代可能会隐藏无参数构造函数,这将导致工厂失败。 最佳答案 您可以通过提供通用约束来确保类具有不带参数的构造函数w

c# - 不能同时指定约束类和 0​​x104567910 或 'class' 约束

我正在尝试解决mockingissue通过创建IDbSet的自定义模拟。自定义模拟:publicclassDbSetMock:IDbSet{/*hiddenallotherimplementedmethods/properties*/publicTDerivedEntityCreate()whereTDerivedEntity:class,Tenant{thrownewNotImplementedException();}}create方法给出了一个构建错误,我不知道如何解决:cannotspecifybothaconstraintclassandthe'class'or'struct

c# - 泛型 IAbstract<T> 继承自 IAbstract

我正在努力实现这样的目标:interfaceIAbstract{stringA{get;}objectB{get;}}interfaceIAbstract:IAbstract{TB{get;}}classRealThing:IAbstract{publicstringA{get;privateset;}publicTB{get;privateset;}}所以我可以这样做:RealThingrt=newRealThing();IAbstractia=rt;IAbstractias=rt;objecto=ia.B;strings=ias.B;这可能吗? 最佳答案

c# - 将泛型类型参数转换为 C# 中的特定类型

如果您需要将泛型类型参数转换为特定类型,我们可以将其转换为一个对象并像下面这样进行转换:voidSomeMethod(Tt){SomeClassobj2=(SomeClass)(object)t;}有没有更好的方法来实现这一点,而不是将其强制转换为对象,然后再强制转换为特定类型?问题:我有一个接受泛型类型参数的泛型函数,在基于类型检查的函数内部我做了一些如下操作:voidSomeMethod(Tt){if(typeof(T).Equals(typeof(TypeA))){TypeA=(TypeA)(object)t;//Dosomeoperation}elseif(typeof(T).

c# - 是否可以在 .Net 3.5 中制作通用控件?

我已经声明了以下通用用户控件:publicpartialclassMessageBase:UserControl{protectedTmyEntry;publicMessageBase(){InitializeComponent();}publicMessageBase(TnewEntry){InitializeComponent();myEntry=newEntry;}}}但是编译器不允许我这样做:publicpartialclassMessageControl:MessageBase{publicMessageControl(){InitializeComponent();}}如何

c# - 为什么这种隐式转换不起作用?

我定义了一个通用类“Lazy”,用于延迟评估和缓存委托(delegate)Func的结果.我还定义了两个隐式转换运算符,因此我可以创建一个Lazy来自Funcs,我可以分配一个Lazy到T(获取Value的Lazy)这个想法是你可以传递一个Lazy代替T的实例,但在将其分配给T的实际实例之前,不执行计算/检索值的工作.//classLazy//Encapsulatesavaluewhichcanberetrievedwhenfirstaccessed,//andisthencached.classLazy{privateFunc_getter;privateT_cached;priva

c# - 如何将类型为 "Type"的变量传递给泛型参数

我正在尝试这样做:Typetype=Type.GetType(string.Format("Gestor.Data.Entities.{0},Gestor.Data",e.Item.Value));MetaDataUtil.GetColumnasGrid();但它不起作用,你知道我该怎么做吗? 最佳答案 你需要为此使用反射。varmethod=typeof(MetaDataUtil).GetMethod("GetColumnasGrid").MakeGenericMethod(new[]{type}).Invoke(null,nul

c# - 与 IComparable 一起使用的可空泛型类型。可能吗?

我正在尝试创建一个简单的Clamp(以便我可以绑定(bind)任何可比较的值......主要用于数字类型,如int、double等)问题是如果我执行以下操作会出现错误,但是accordingtoMSDNIComparable的CompareTo应该能够处理空值。引述:“根据定义,任何对象比较大于null,并且两个null引用相互比较相等。”publicstaticTClamp(thisTvalue,Tmin,Tmax)whereT:IComparable{if(value.CompareTo(max)>0)returnmax;if(value.CompareTo(min)(0,9999