jjzjj

non-nullable

全部标签

c# - 从 Nullable 类型反射获取 PropertyType.Name

我想使用反射获取属性类型。这是我的代码varproperties=type.GetProperties();foreach(varpropertyInfoinproperties){model.ModelProperties.Add(newKeyValuePair(propertyInfo.PropertyType.Name,propertyInfo.Name));}这段代码propertyInfo.PropertyType.Name没问题,但是如果我的属性类型是Nullable我得到这个Nullable'1字符串,如果写FullName如果得到这个stirngSystem.Nulla

c# - Nullable<T> 混淆

为什么禁止以下内容?Nullable>鉴于structMyNullable{}MyNullable>不是 最佳答案 这是因为结构约束实际上意味着'notnullable'因为Nullable,尽管是一个结构,是可以为空的(可以接受值null)Nullable不是外部Nullable的有效类型参数。这在theconstraintsdocumentation中有明确说明whereT:structThetypeargumentmustbeavaluetype.AnyvaluetypeexceptNullablecanbespecified

c# - 数字必杀技 : Where does a callvirt of a non-existent method end up?

我在其基类中标记为抽象的库类上调用属性集访问器。现在在运行时我force应用程序针对另一个版本的库运行,其中类仅实现基类的底层接口(interface),但不是从它派生的。有趣的是,.NET将运行代码,但设置该属性没有任何效果。幕后发生了什么?违规代码:MyDbParameterparam=newMyDbParameter();param.ParameterName="p";Console.Out.WriteLine("ParameterName:"+param.ParameterName);库2.0(已编译)publicsealedclassMyDbParameter:System.

c# - Code Contracts 是否未能发现 Nullable<T>.HasValue 和 null 之间的明显关系?

我正在尝试将代码契约应用到我的代码中,但我遇到了一个令人费解的问题。这段代码不符合契约(Contract),但除非我真的很厚,否则我希望它能够轻松分析id在返回时必须有一个值if(id==null)thrownewInvalidOperationException(string.Format("{0}'{1}'doesnotyethaveanidentity",typeof(T).Name,entity));returnid.Value; 最佳答案 我已经弄清了这个行为的真相,这不是CodeContract的错。我在ILSpy中打开

c# - DisplayFormat 不适用于 Nullable<DateTime>

我试图在MVC中格式化一些DateTimes,但DisplayFormat没有应用于Nullable对象,我不知道为什么。它在CreatedDateTime上运行良好,但在LastModifiedDateTime上运行良好[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="{0:MM/dd/yyhh:mmtt}")]publicDateTimeCreatedDateTime{get;set;}[DisplayFormat(ApplyFormatInEditMode=true,DataFormatString="{0:M

c# - 在 Linq-To-Sql 中避免 "Nullable object must have a value."

我有一个这样的方法查询:publicIListGetBusinessObject(Guid?filterId){using(vardb=newL2SDataContext()){varresult=fromboindb.BusinessObjectswhere(filterId.HasValue)?bo.Filter==filterId.value:trueorderbybo.NameselectSqlModelConverters.ConvertBusinessObject(bo);returnresult.ToList();}}在运行时,这会抛出一个System.InvalidOp

c# - 为什么 integer == null 在 C# 中是一个有效的 boolean 表达式?

如果integer(int类型的变量)不可为空,为什么integer==null在C#中是一个有效的boolean表达式?(我不反对,其实我也喜欢,只是我不知道可以) 最佳答案 尽管int本身是不可空的,但存在到int?的隐式转换,是可空的。此时,如果该结构在两侧都声明了具有相同类型的==运算符,那么它也将被提升以处理可为null的类型。所以这不能编译:publicstructFoo{}classTest{staticvoidMain(){Foox=newFoo();if(x==null){...}}}...但是如果您给Foo一些运

c# - 在单个 C# 泛型方法中返回 nullable 和 null?

是否可以在C#泛型方法中返回对象类型或Nullable类型?例如,如果我有一个List的安全索引访问器我想返回一个值,以后可以用==null检查或.HasValue().我目前有以下两种方法:staticT?SafeGet(Listlist,intindex)whereT:struct{if(list==null||index=list.Count){returnnull;}returnlist[index];}staticTSafeGetObj(Listlist,intindex)whereT:class{if(list==null||index=list.Count){return

c# - 类型 'MyObject' 必须是不可为 null 的值类型才能将其用作泛型类型或方法 'T' 中的参数 'Nullable<T>'

我正在使用.netframework4.5我得到以下错误ErrorCS0453Thetype'MyObject'mustbeanon-nullablevaluetypeinordertouseitasparameter'T'inthegenerictypeormethod'Nullable'publicasyncTask>MyMethod(stringmyParamter){}我也试过publicasyncTaskMyMethod(stringmyParamter){}如果我将事物设置为可为空,那么为什么我会在方法名称下方看到一条红线并显示此错误消息计算器answer很简单,使返回类型

c# - 为什么我不能写 Nullable<Nullable<int>>?

Nullable的定义是:[SerializableAttribute]publicstructNullablewhereT:struct,new()约束whereT:struct暗示T只能是值类型。所以我很清楚我不能写:Nullablea;//error.makessensetome因为string是引用类型,不是值类型。但是我真的不明白为什么我不能写Nullable>b;//error.butwhy?为什么不允许?毕竟,Nullable是一个值类型,因此,它可以是Nullablle的类型参数.当我在ideone上编译它时,它给出了这个错误(ideone):errorCS0453:T