草庐IT

Generics

全部标签

c# - 通用类型列表

我有一个通用类,我想为它创建一个列表。然后在运行时我得到项目的类型类publicclassJob{publicintID{get;set;}publicTaskTask{get;set;}publicTimeSpanInterval{get;set;}publicboolRepeat{get;set;}publicDateTimeOffsetNextExecutionTime{get;set;}publicJobRunOnceAt(DateTimeOffsetexecutionTime){NextExecutionTime=executionTime;Repeat=false;retu

c# - 如果对象是通用列表

有什么方法可以确定一个对象是否是一个泛型列表?我不会知道列表的类型,我只知道它是一个列表。我该如何确定? 最佳答案 这将返回“真”ListmyList=newList();Console.Write(myList.GetType().IsGenericType&&myListisIEnumerable);您是否想知道它是否完全是一个“列表”...或者您是否接受它是IEnumerable和通用的? 关于c#-如果对象是通用列表,我们在StackOverflow上找到一个类似的问题:

c# - 使用反射调用包含通用参数的静态方法

这个问题在这里已经有了答案:HowdoIusereflectiontocallagenericmethod?(8个答案)关闭8年前。在执行以下代码时,我收到此错误“无法对ContainsGenericParameters为真的类型或方法执行后期绑定(bind)操作。”classProgram{staticvoidMain(string[]args){MethodInfoMI=typeof(MyClass).GetMethod("TestProc");MI.MakeGenericMethod(new[]{typeof(string)});MI.Invoke(null,new[]{"Hel

c# - 当 T 是结构时,List<T>.Find 如何工作?

我有一个List>.我需要按照以下方式做一些事情list.Find(x=>x.Key=="foobar")但是,如果列表中不存在,行为会是什么?通常它会返回null,但结构不能为null。 最佳答案 我的建议是对不可为null的类型使用FindIndexintindex=list.FindIndex(x=>x.Key=="foobar");if(index>=0){//found!UseResult(list[index]);}如果Find()不成功,返回默认值default(T)。对于不可空类型,此结果无法与具有默认值的常规条目区

c# - 在运行时构建 c# 泛型类型定义

目前我不得不做这样的事情来在运行时构建类型定义以传递给我的IOC来解析。简化:Typet=Type.GetType("System.Collections.Generic.List`1[[ConsoleApplication2.Program+Person");我只知道运行时的泛型类型参数。有什么东西可以让我做这样的事情(假代码):Typet=Type.GetTypeWithGenericTypeArguments(typeof(List),passInType.GetType());或者我应该坚持我的hack,passInType.GetType()转换为字符串,构建通用类型字符串.

c# - 使用 LINQ 创建一个 List<T> 其中 T : someClass<U>

这与我之前的一个问题有关C#GenericListconversiontoClassimplementingList我有以下代码:publicabstractclassDataField{publicstringName{get;set;}}publicclassDataField:DataField{publicTValue{get;set;}}publicstaticListConvertXML(XMLDocumentdata){result=(fromdinXDocument.Parse(data.OuterXML).Root.Decendendants()selectnewDa

c# - 什么时候在 C# 中解析泛型类型?

根据this在stackoverflow上回答,C#中的泛型类型在运行时解析。然而,根据this回答,在C#中,泛型类型在编译时解析。我在这里错过了什么?换句话说,类型T是在编译时还是运行时解析的?更新:根据Oded的回答,在这种情况下,类型是封闭的具体类型(这意味着它将在编译时解析)classProgram{staticvoidMain(){vart=newTest();}}publicclassTest{}MSIL是否具有等同于classProgram{staticvoidMain(){vart=newTest();}}publicclassTest{}

c# - 通用 TryParse 扩展方法

代码取自here我想听听一些专家对这种扩展方法的意见。我确实打算使用它,但想听听我可能会遇到的任何已知问题。我对原始类型使用TryParse方法是否更好?publicstaticT?TryParse(thisobjectobj)whereT:struct{if(obj==null)returnnull;T?result=null;TypeConverterconverter=TypeDescriptor.GetConverter(typeof(T));if(converter!=null){try{stringstr=obj.ToString();result=(T)converter

c# - 获取通用列表中对象的索引

我有一个自定义对象列表,其中有两个属性作为标识符(IDa和IDb)。每次我删除一个对象时,我都需要知道它的索引。如何在不循环所有列表的情况下获取对象的索引?Listlist=newList();list.RemoveAll((MiniMapRecordp)=>p.IDa==IDa.SystemID&p.IDb==pInputRecordMap.IDb); 最佳答案 你想要的方法是FindIndex(Predicate)intindex=list.FindIndex(MiniMapRecordp=>p.IDa==IDa.SystemI

c# - new() 在 `where T: new()?` 中做了什么

下面代码中的new()做了什么?publicclassAwhereT:B,new() 最佳答案 这是对您的类的泛型参数的约束,这意味着作为泛型类型传递的任何类型都必须具有无参数构造函数。所以,publicclassC:B{publicC(){}}将是一个有效的类型。您可以创建A的新实例.但是,publicclassD:B{publicD(intsomething){}}将不满足约束条件,并且不允许您创建A的新实例.如果您还向D添加了一个无参数构造函数,那么它将再次有效。 关于c#-new