草庐IT

value_type

全部标签

C# 反射 : how to get an array values & length?

FieldInfo[]fields=typeof(MyDictionary).GetFields();MyDictionary是一个静态类,所有字段都是字符串数组。如何获取每个数组的长度值,然后遍历所有元素?我尝试了这样的类型转换:fieldasArray但它会导致错误Cannotconverttype'System.Reflection.FieldInfo'to'System.Array'viaareferenceconversion,boxingconversion,unboxingconversion,wrappingconversion,ornulltypeconversion

c# - 编译器错误 : "error CS0307: The variable ' int' cannot be used with type arguments"

如果我有以下代码:privatevoidCheck(boola,boolb){}privatevoidCheck(inta,intb,intc,boolflag){Check(a(flag?c:b-10));}我在调用Check(int,int)时遇到编译时错误:errorCS0307:Thevariable'int'cannotbeusedwithtypearguments我也遇到了这些错误:errorCS0118:'b'isavariablebutisusedlikeatypeerrorCS0118:'a'isavariablebutisusedlikeatype为什么会出现这些错

c# - Type.GetType 返回 null

这个问题在这里已经有了答案:Type.GetType("namespace.a.b.ClassName")returnsnull(17个答案)关闭7年前。我正在尝试使用Type.GetType并传递“caLibClient.entity.Web2ImageEntity”完整类名。caLibClient.entity是命名空间,位于单独的程序集(caLibClient)中并添加到程序引用程序集列表中。当我从程序中调用Type.GetType时,它​​总是返回Null,有什么问题吗?

c# - Resharper 中的警告 "Return value of pure method is not used"

我有一个快速的问题,关于我正在工作的c#项目中从VisualStudio中的Resharper收到的警告。警告是:"ReturnValueofpuremethodisnotused"发生这种情况的方法如下:privatestaticboolFilePathHasInvalidChars(stringuserInputPath){try{//thisiswherethewarningoccurs:Path.GetFullPath(userInputPath);}catch(Exceptione){Log.Error(String.Format("TheProgramfailedtorun

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# - 如何将对象集合/字典序列化为 <key>value</key>

有没有办法将键/值对(最好是强类型,但也可能来自字典)序列化为下面所需的格式?publicListIdentifiers=newList();publicclassIdentifier{publicstringName{get;set;}publicstringDescription{get;set;}}这通常会序列化为以下内容:somenamesomedescription...我们考虑的另一种可能的方法是使用哈希表/字典:publicDictionaryIdentifiers=newDictionary{{"somename","somedescription"},{"anothe

c# - 构造函数或属性 : which one is the better choice while assigning values

在赋值时我们应该在属性上使用构造函数,反之亦然。 最佳答案 构造函数是一种非常方便且功能强大的契约类型-一种要求消费者在甚至可以使用您的对象之前提供某些信息的方法。因此,对于实例正常运行所必需的信息,请使用构造函数参数。这是依赖注入(inject)的基本概念-任何你依赖于完成你的工作的东西,都必须在你开始之前注入(inject)(提供)给你。属性可以代表一个有趣的问题。总的来说,经验告诉我,只要有可能,属性应该是只读的,对象通常应该尽可能在外部不可变。将公共(public)setter添加到属性会使您的类的复杂性成倍增加。当然总是有

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# - 为什么不能将 Type 用作常量值?

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