草庐IT

generics

全部标签

c# - 为什么不在用作泛型类型参数的类上调用静态构造函数?

给定以下类:publicclassFoo{staticFoo(){Console.WriteLine("Fooisbeingconstructed");}}publicclassBar{publicvoidReferenceFooAsGenericTypeParameter(){Console.WriteLine("Fooisbeingreferencedasagenerictypeparameter");}}publicclassSampleClass{publicstaticvoidMain(){newBar().ReferenceFooAsGenericTypeParameter

c# - 重写继承方法时避免显式类型转换

我有一个基础抽象类,它也实现了一个特定的接口(interface)。publicinterfaceIMovablewhereTEntity:classwhereT:struct{TEntityMove(IMovermoverProvider);}publicabstractclassAnimal:IMovable{...publicvirtualAnimalMove(IMovermoverProvider){//performsmovementusingprovidedmover}}然后我继承了一些类,其中一些类必须重写基类的接口(interface)实现方法。publicclassS

c# - XML反序列化泛型方法

我有下一个XML文件:d639a54f-baca-11e1-8067-001fd09b1dfd-24145e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd0.28我将它反序列化为这个类:[XmlRoot("Root",IsNullable=false)]publicclassDocBalanceCollection{[XmlElement("Document")]publicListDocsBalanceItems=newList();}DocBalanceItem是:publicclassDocBalanceItem{[XmlElement("Id")]pub

c# - 检查对象是否为 C# 中的非特定泛型类型

假设我有以下类(class):publicclassGeneral{}我想知道一个对象是否属于那种类型。我知道我可以使用反射来确定对象是否属于Type.GetGenericTypeDefinition的泛型类型,但我想避免这种情况。是否可以做类似objisGeneral的事情?,或obj.GetType().IsAssignableFrom(typeof(General))?我很惊讶我找不到类似的问题,尽管我可能在搜索中使用了错误的关键字。 最佳答案 你可以这样做:varobj=newGeneral();vartype=obj.Ge

c# - C# 中的泛型函数声明

我正在尝试创建一些关于库中方法调用持续时间的统计信息。我不想用时间线包装对库的每个方法调用并跟踪它,而是想创建一个通用操作和函数来执行这些重复步骤。例如对于不返回值的方法,我创建了这个:privatereadonlyActiontimedAction=(name,action)=>{varsw=Stopwatch.StartNew();action.Invoke();trackDuration(name,sw.ElapsedMilliseconds);};可以用timedAction("methodname",()=>lib.methodname())调用。我想对返回值的方法做一些类似

c# - 当 <T> 的所有属性都是只读时,类别不会显示在集合 <T> 的 PropertyGrid 中

正如标题所说,当类“T”的所有属性都是只读时,我注意到类别没有显示在集合(OfT)的**PropertyGrid*(在其默认集合编辑器中)中.下面的代码代表了我的代码结构:C#:[TypeConverter(typeof(ExpandableObjectConverter))]publicclassTestClass1{publicTestClass2TestProperty1{get;}=newTestClass2();}[TypeConverter(typeof(ExpandableObjectConverter))]publicsealedclassTestClass2{[Typ

通用扩展方法的 C# 特化

我的MessageBus有以下扩展方法:publicstaticclassMessageBusMixins{publicstaticIDisposableSubscribe(thisIObservableobservable,MessageBusbus)whereT:class{...}publicstaticIDisposableSubscribe(thisIObservable>observable,MessageBusbus){...}}编译正常。但是当我尝试使用它时:IObservable>source=...;MessageBusbus=...;source.Subscrib

c# - Generics & Reflection - GenericArguments[0] 违反类型约束

我已经为这个问题绞尽脑汁了一段时间,本质上我是在尝试实现一个通用的存储库工厂,其名称如下:varresposFactory=newRepositoryFactory>();存储库工厂如下所示:publicclassRepositoryFactory:IRepositoryFactory{publicTGetRepository(GuidlistGuid,IEnumerablefieldMappings){AssemblycallingAssembly=Assembly.GetExecutingAssembly();Type[]typesInThisAssembly=callingAss

c# - 将 ExpandoObject 转换为匿名类型

我可以将ExpandoObject转换为匿名类型吗?varanoObj=new{name="testName",email="testEmail"};dynamicexpandoObj=newSystem.Dynamic.ExpandoObject();//HereI'mpopulatingtheexpandoObjwithsamepropertynames/typesinanonymoustype(anoObj)//Now,howtoconvertthisExpandoObjecttoanonymoustype?varnewObj=(typeof(anoObj)expandoObj)

c# - 无法使用动态参数和泛型调用扩展方法

我很好奇是否有其他人遇到过同样的问题...我在一个项目的ORM上使用Dapper,并从IDbConnection创建了一些我自己的扩展方法。为了简化代码,我遇到了(我发现的)令人费解的错误。我将详细介绍我所经历的过程。首先,我在一个名为DbExtensions的静态类中向我的项目添加了一个扩展方法。像这样:usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;publicstaticclassDbExtensions{publicstaticTScalar(thisIDbConnectioncnn,strin