Tag:

如何从Type变量创建Nullable ?

我正在使用表达式,我需要一个接收某种类型的对象(目前未知)的方法。 像这样的东西: public static void Foobar(object Meh) { } 我需要的是使此方法返回Meh的Nullable版本,但类型T来自Meh.GetType() 。 因此返回将是Nullable ,其中MehType是Meh的类型。 有什么想法或建议吗? 谢谢 更新:我之所以需要这个是因为这个例外: 没有为类型’System.Nullable`1 [System.Int32]’和’System.Int32’定义二元运算符Equal。 return Expression.Equal(leftExpr, rightExpr); 其中leftExpr是System.Nullable1[[System.Int32和rightExpr是System.Int32 。

C#Nullable数组

我有一个搜索function,但我希望LocationID是一个整数数组而不是一个整数。 我不知道该怎么做,因为我希望它也可以为空。 我看过做int?[]但是我必须检查每个条目的HasValue 。 有没有更好的办法? 这就是我目前所拥有的: public ActionResult Search(string? SearchString, int? LocationId, DateTime? StartDate, DateTime? EndDate)

取两个可以为空的值

假设我有两个可以为空的整数: int? a = 10; int? b = 20; 我想取最大的非null值,这样如果两个值都为null,则结果为null。 我可以写一些啰嗦的东西,例如: int? max; if (a == null) { max = b; } else if (b == null) { max = a; } else { max = a > b ? a : b; } 对于我的喜好,这感觉有点过于笨重(并且可能容易出错)。 返回更大值的最简单方法是什么,这也是空值的可能性?

可空属性与Nullable局部变量

我对Nullable类型的以下行为感到困惑: class TestClass { public int? value = 0; } TestClass test = new TestClass(); 现在, Nullable.GetUnderlyingType(test.value)返回底层的Nullable类型,即int 。 但是,如果我尝试获得这样的字段类型 FieldInfo field = typeof(TestClass).GetFields(BindingFlags.Instance | BindingFlags.Public)[0]; 我援引 Nullable.GetUnderlyingType(field.FieldType).ToString() 它返回System.Nullable[System.Int32]类型。 这意味着方法Nullable.GetUnderlyingType()具有不同的行为,具体取决于您获取成员类型的方式。 为什么会这样? 如果我只是使用test.value如何在不使用reflection的情况下判断它是否为Nullable ?

在C#中,`DateTime类型中的`?`是什么?`

我在使用System.DirectoryServices.AccountManagement时遇到了一些代码 public DateTime? LastLogon { get; } 是什么 ? 在DateTime之后。 我发现了一个参考? 运算符(C#参考) ,但它不是一回事。 (280Z28:这是使用Nullable Types的正确链接。)

可空类型和ReSharper警告

我有以下代码: private static LogLevel? _logLevel = null; public static LogLevel LogLevel { get { if (!_logLevel.HasValue) { _logLevel = readLogLevelFromFile(); } return _logLevel.Value; } } private static LogLevel readLogLevelFromFile() { … } 我在return语句中收到一个关于可能的System.InvalidOperationException的ReSharper警告,它建议我检查_logLevel以查看它是否为null 。 但是, readLogLevelFromFile返回LogLevel ,而不是LogLevel? ,所以当_logLevel为null时,无法达到return语句。 这只是ReSharper的疏忽,还是我错过了什么?

可以为空的枚举的扩展方法

我正在尝试为可为空的枚举编写一个Extension方法 。 与此示例一样: // ItemType is an enum ItemType? item; … item.GetDescription(); 所以我写了这个方法,由于某些我不理解的原因而无法编译: public static string GetDescription(this Enum? theEnum) { if (theEnum == null) return string.Empty; return GetDescriptionAttribute(theEnum); } 我在Enum?上收到以下错误Enum? : 只有非可空值类型才能成为system.nullable的基础 为什么? 枚举不能为null ! 更新: 如果有很多枚举,则ItemType只是其中一个的示例。

使用reflection时,C#确定Nullable属性DateTime类型

我有一个关于如何确定对象的Nullable属性类型的问题。 具有DateTime属性的ObjectA? CREATEDATE; 当我迭代它的属性,如下面的代码,我如何检查属性是否是Nullable DateTime类型? 谢谢 foreach (PropertyInfo pi in ObjectA.GetType().GetProperties()) { //do the compare here }

如何正确处理LinqToSql类的System.Nullable 字段?

我有一张桌子,里面有一些可以考虑的双字段。 使用LinqToSQL尝试直接使用该字段我得到 参数类型System.Nullable不能分配给参数类型double 我该如何正确处理?

C#中的可空方法参数

重复的问题 将空参数传递给C#方法 我可以在c#中为.Net 2.0做这个吗? public void myMethod(string astring, int? anint) { //some code in which I may have an int to work with //or I may not… } 如果没有,我能做些类似的事吗?