草庐IT

type-inference

全部标签

c# - 如何解决错误 : Inconsistent accessibility: parameter type for generic c# interface?

将此代码写入我的项目时出现错误Error1Inconsistentaccessibility:fieldtype'System.Collections.Generic.List'islessaccessiblethanfield'Jain_milan.addchild.m_children'Error2Inconsistentaccessibility:parametertype'System.Collections.Generic.List'islessaccessiblethanmethod'Jain_milan.addchild.addchild(System.Collectio

C# 泛型 : cast generic type to value type

我有一个泛型类,它保存指定类型T的值。该值可以是int、uint、double或float。现在我想获取值的字节以将其编码为特定协议(protocol)。因此我想使用方法BitConverter.GetBytes()但不幸的是Bitconverter不支持泛型类型或undefinedobject。这就是为什么我要转换值并调用GetBytes()的特定重载。我的问题:如何将通用值转换为int、double或float?这不起作用:publicclassGenericClasswhereT:struct{T_value;publicvoidSetValue(Tvalue){this._va

c# - VS2010 'Show Derived Types' 类 View 中缺少选项

我正在VisualStudio2010中处理VisualC#项目,并在类View中浏览各种类型。根据this文档中,类View设置菜单中应该有一个类似于“显示基本类型”选项的“显示派生类型”选项。然而,它似乎不见了。我还没有设法找到任何错误报告或对这个问题的引用,所以我想知道我是否只是遗漏了一些东西。 最佳答案 我在VisualStudio2010中看到它,这是一个具有派生类型的C++项目。但是,在使用派生类型提到的C#项目中,未提供此选项。我正在检查一个没有派生类型的项目,看看这是否会导致该选项被禁用。编辑:似乎没有派生类型的项目

C# 方法组类型推断

我正在尝试编写一个提供参数并调用函数的通用方法,如下所示:classMyClass{publicintMethod(floatarg)=>0;}TResultCall(Funcfunc)=>func(default(T1));voidMain(){varm=newMyClass();varr1=Call(m.Method);varr2=Call(m.Method);//CS0411}最后一行编译失败CS0411.是否有任何解决方法可以让类型推断在这里工作?用例:使用AutoFixture生成函数调用参数。 最佳答案 很遗憾,不能,这

c# - 为什么不能将 Type 用作常量值?

引用MSDN-const(C#reference):Aconstantexpressionisanexpressionthatcanbefullyevaluatedatcompiletime.Therefore,theonlypossiblevaluesforconstantsofreferencetypesarestringandanullreference.根据:typeof(T)vs.Object.GetType()performance,typeof(T)是一个编译时表达式。那么为什么Type不能是常量值呢?以下代码不会编译:publicconstTypeINT_TYPE=ty

c# - 为什么不接受 IEnumerable(of T) 作为扩展方法接收器

在代码前完成问题:为什么是IEnumerablewhereT:ITest不被接受为期望thisIEnumerable的扩展方法的接收者?现在是代码:我有三种类型:publicinterfaceITest{}publicclassElement:ITest{}publicclassElementInfo:ITest{}还有两种扩展方法:publicstaticclassExtensions{publicstaticIEnumerableMethod(thisIEnumerablecollection)whereT:ITest{→returncollection.ToInfoObjects

c# - .NET:静态方法的推断泛型类型

假设我有publicstaticListMap(Listinputs,Funcf){returninputs.ConvertAll((x)=>f(x));}privateintSquare(intx){returnx*x;}publicvoidRun(){varinputs=newList(newint[]{2,4,8,16,32,64,128,256,512,1024,2048});//thisdoesnotcompilevaroutputs=Map(inputs,Square);//thisisfinevaroutputs2=Map(inputs,Square);//thisisa

c# - .NETStandard 1.0/.NET Core 中 Type.GetGenericArguments() 的等价物是什么?

.NETStandard1.0中“缺少”方法System.Type.GetGenericArguments(),我认为TypeInfo.GenericTypeArguments是的替代品GetGenericArguments(),但不幸的是,当提供开放泛型类型时,它们的行为会有所不同。以下面的代码为例:Typetype=typeof(ICommandHandler);type.GetGenericArguments();//return{TCommand}type.GetTypeInfo().GenericTypeArguments;//returnsemptyarray虽然GetGe

c# - Dapper 抛出 "Invalid type owner for DynamicMethod."

所以我正在尝试使用Dapper.net,而且我很喜欢它。我不喜欢的是当我尝试批量插入实体时抛出以下错误:DynamicMethod的类型所有者无效。atSystem.Reflection.Emit.DynamicMethod.Init(Stringname,MethodAttributesattributes,CallingConventionscallingConvention,TypereturnType,Type[]signature,Typeowner,Modulem,BooleanskipVisibility,BooleantransparentMethod,StackCra

c# - 如何从基类中获取子类的 `Type`

我有一个抽象基类,我想在其中实现一个方法来检索继承类的特性属性。像这样的……publicabstractclassMongoEntityBase:IMongoEntity{publicvirtualobjectGetAttributeValue(stringpropertyName)whereT:Attribute{varattribute=(T)typeof(this).GetCustomAttribute(typeof(T));returnattribute!=null?attribute.GetType().GetProperty(propertyName).GetValue(a