Tag: reflection可以为

可空属性与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 ?

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

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

Nullable通过HasValue = false的reflection创建,仅给出类型为“Type”的参数

给定一个Nullable的类型参数,如何创建具有HasValue = false的该类型的实例? 换句话说,填写此代码: //type is guaranteed to implement Nullable public static object Create(Type type) { //Instantiate a Nullable with reflection whose HasValue = false, and return it } 我的尝试,它不起作用(它抛出NullReferenceException ),因为没有默认的构造函数: static void Main(string[] args) { Console.WriteLine(Create(typeof(Nullable))); Console.ReadLine(); } //type is guaranteed to be implement from Nullable public static object Create(Type type) { //Instantatie a […]

通过reflection检测可空类型

令人惊讶的是,以下代码未通过Assert: int? wtf = 0; Assert.IsType<Nullable>(wtf); 所以只是出于好奇,你如何确定给定的实例是否是Nullable 对象?

从Nullable类型获取reflection中的PropertyType.Name

我想使用reflection获取属性类型。 这是我的代码 var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair (propertyInfo.PropertyType.Name, propertyInfo.Name) ); } 这段代码propertyInfo.PropertyType.Name ,但是如果我的属性类型是Nullable我得到这个Nullable’1字符串,如果得到这个名字,可以写FullName System.Nullable1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]