草庐IT

list_type

全部标签

c# - 编译转换 : The type 'Object' is defined in an assembly that is not referenced

我正在对一个asp.NetMVC5网络应用程序进行一些更改,我在其中使用了typelite从C#类创建.ts定义(非常方便)。出于某种原因,现在我在执行T4时遇到了这个错误:Compilingtransformation:Thetype'Object'isdefinedinanassemblythatisnotreferenced.Youmustaddareferencetoassembly'mscorlib,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e,Retargetable=Yes'.和这个警告:C

c# - 使用 Type 变量转换对象

当然,以下是行不通的。有没有一种可能的方法,与此非常相似?TypenewObjectType=typeof(MyClass);varnewObject=givenObjectasnewObjectType; 最佳答案 newObjectType是Type类(包含有关类型的元数据)的实例,而不是type本身.这应该可行varnewObject=givenObjectasMyClass;或varnewObject=(MyClass)givenObject;转换为类型实例确实没有意义,因为编译时必须知道变量类型应该是什么,而类型实例是运行

c# - 为什么 List<double> 显式转换为 IEnumerable<object> 会抛出异常?

根据这个MSDNreferenceIEnumerable是协变的,这可以将对象列表隐式转换为可枚举对象:IEnumerablestrings=newList();IEnumerableobjects=strings;在我自己的代码中,我写了一行代码,当列表的项目类型是Point类时,它可以完美运行(Point是一个简单的类,具有三个双x、y、z属性):varobjects=(IEnumerable)dataModel.Value;//herepropertyValueisalistthatcouldbeofanytype.但是当列表的项类型为double时,上面的代码返回以下异常:Un

c# - 包含路径表达式必须引用 type.in 预加载中定义的导航属性

我尝试像这样包含匿名类型:除了CompanyTitle,PeriodTypeName之外,我还想要所有incomelist属性varincomeList=ctx.IncomeLists.Include(i=>new{CompanyTitle=i.CompanyId.ToString()+"/"+i.Company.CompanyName,PeriodTypeName=i.ListPeriods.Select(lp=>lp.PeriodType.PeriodTypeName)}).ToList()我的模型部分是这样的:但我得到以下异常:TheIncludepathexpressionmu

C# 功能请求 : implement interfaces on anonymous types

我想知道做这样的事情需要什么:usingSystem;classProgram{staticvoidMain(){varf=newIFoo{Foo="foo",Print=()=>Console.WriteLine(Foo)};}}interfaceIFoo{StringFoo{get;set;}voidPrint();}创建的匿名类型看起来像这样:internalsealedclassf__AnonymousType0j__TPar>:IFoo{readonlyj__TPari__Field;publicf__AnonymousType0(j__TParFoo){this.i__Fi

c# - Casting List<T> - 协方差/逆变问题

给定以下类型:publicinterfaceIMyClass{}publicclassMyClass:IMyClass{}我想知道如何转换List到List?我对协变/逆变主题不是很清楚,但我知道我不能因此而简单地使用List。我只能想出这个微不足道的解决方案;缺乏优雅,浪费资源:...publicListConvertItems(Listinput){varresult=newList(input.Count);foreach(varitemininput){result.Add(item);}returnresult;}....如何以更优雅/更高效的方式解决它?(请注意,我需要.N

c# - 在 c# Parallel.ForEach 中的 List.Add() 上出现 "Index out of bounds"错误

这是代码Listsomething=newList();Parallel.ForEach(anotherList,r=>{..dosomeworksomething.Add(somedata);});Indexoutofbounds错误大约每百次运行1次。有没有办法防止由线程引起的冲突(我假设)? 最佳答案 为了防止出现此问题,您可以使用ConcurrentQueue而不是List或并行部分中的类似并发集合。并行任务完成后,您可以将其放入List中。.有关详细信息,请查看System.Collections.Concurrent命名

c# - List<T>.Contains() 如何找到匹配项?

我有一个汽车对象列表Listcars=GetMyListOfCars();我想看看列表中是否有汽车if(cars.Contains(myCar)){}Contains使用什么来确定myCar是否在列表中。它是否对我的汽车对象执行“ToString()”。它是否使用Equals()方法,即gethashcode()?我知道我可以传递我自己的IEqualityComparer来强制我自己的实现,但只是想了解它默认情况下的作用。 最佳答案 直接来自MSDN-List.Contains:Thismethoddeterminesequalit

c# - 在 C# 中使用 Type.GetType() 返回的类型

我有一个关于如何可能的问题(如果可能的话:)例如,使用Type.GetType()返回的类型引用来创建该类型的IList?这里是示例代码:Typecustomer=Type.GetType("myapp.Customer");IListcustomerList=newList();//gotanerrorhere=[提前致谢! 最佳答案 像这样:TypelistType=typeof(List).MakeGenericType(customer);IListcustomerList=(IList)Activator.CreateIn

c# - 将填充的 List<BaseClass> 转换为 List<ChildClass>

我有一个List里面有成员。我想将列表(及其所有成员)转换为类型List,其中ChildClass继承BaseClass.我知道我可以通过foreach获得相同的结果:ListChildClassList=newList();foreach(variteminBaseClassList){ChildClassList.Add(itemasChildClass);}但是有没有更简洁的方法呢?注意-这是在WP7平台上完成的。 最佳答案 如果你真的确定所有元素都是可类型转换的,你可以这样做:ChildClassList=BaseClass