簡體   English   中英

Activator.CreateInstance和Convert.ChangeType的空問題

[英]Nullable problems with Activator.CreateInstance and Convert.ChangeType

我有一個第三方庫,它使用反射如下設置給定的對象屬性。 (這是簡化版)

public void Set(object obj, string prop, object value) {
    var propInf = obj.GetType().GetProperty(prop);
    value = Convert.ChangeType(value, propInf.PropertyType);
    propInf.SetValue(obj, value, null);
}

而且我們有一個帶有可空屬性的類

class Test
{
    public int? X { get; set; }
}

當我編寫以下代碼時,它說不能將int轉換為int?

var t = new Test();
Set(t, "X", 1);

由於Nullable沒有實現IConvertible,所以這很有意義。 然后,我決定編寫一個方法,該方法返回給定值類型對象的可空版本。

public object MakeNullable(object obj) {
    if(obj == null || !obj.GetType().IsValueType)
        throw new Exception("obj must be value type!");

    return Activator.CreateInstance(
        typeof(Nullable<>).MakeGenericType(obj.GetType()), 
        new[] { obj });
}

我希望按如下方式使用此方法。

var t = new Test();
Set(t, "X", MakeNullable(1));

但是它仍然說不能將int轉換為int? 當我調試typeof(Nullable<>).MakeGenericType(obj.GetType())等於int? 但是Activator.CreateInstace返回的是int值而不是int?

這就是我的情況...有什么幫助嗎?

試試這個代碼...

public void Set(object obj, string prop, object value)
      {
          //var propInf = obj.GetType().GetProperty(prop);
          //value = Convert.ChangeType(value, propInf.PropertyType);
          //propInf.SetValue(obj, value, null);
          var property = obj.GetType().GetProperty(prop);
          if (property != null)
          {
              Type t = Nullable.GetUnderlyingType(property.PropertyType)
                           ?? property.PropertyType;

              object safeValue = (value == null) ? null
                                                 : Convert.ChangeType(value, t);

              property.SetValue(obj, safeValue, null);
          }
      }

從這里,LukeH的Nullable Types上的Convert.ChangeType()失敗

更新:隱藏基本方法。

namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
using System.Linq;

internal class Program
{
    private static void Main(string[] args)
    {
        EnterPoint t = new EnterPoint();
        t.BeginProcess();
    }
}

public class EnterPoint
{
    public void BeginProcess()
    {
        var t = new Test(); //Object

        try
        {
            baseDllMethod m = new baseDllMethod(); //Your DLL
            m.Set(t, "X", 1); //Problem Method, this give you error
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR EN DLL METHOD");    
        }


        xyz_D der = new xyz_D(); //Derivated method
        der.Set(t, "X", 1) ; //HIDE BASE METHOD
    }
}

public class baseDllMethod //YOUR DLL HERE
{
    public void Set(object obj, string prop, object value)
    {
        var propInf = obj.GetType().GetProperty(prop);
        value = Convert.ChangeType(value, propInf.PropertyType);
        propInf.SetValue(obj, value, null);
    }
}

public class xyz_D : baseDllMethod //Inherits
{
    public new void Set(object obj, string prop, object value) //Hide base method
    {
        var property = obj.GetType().GetProperty(prop);
        if (property != null)
        {
            Type t = Nullable.GetUnderlyingType(property.PropertyType)
                         ?? property.PropertyType;

            object safeValue = (value == null) ? null
                                               : Convert.ChangeType(value, t);

            property.SetValue(obj, safeValue, null);
        }
    }
}

public class Test //Your Object
{
    public int? X { get; set; }
}

}

這應該工作:

public static object ChangeType(object value, Type conversionType)
{
   if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition() == typeof(Nullable<>))
   {
       if (value == null)
           return null;
       var nullableConverter = new NullableConverter(conversionType);
       conversionType = nullableConverter.UnderlyingType;
    }
    return Convert.ChangeType(value, conversionType);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM