1

次のように、リフレクションを使用して特定のオブジェクト プロパティを設定するサード パーティのライブラリがあります。(これは簡易版です)

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);
}

そして、null可能なプロパティを持つクラスがあります

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

次のコードを書くと、変換できないと表示intされますint?

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

Nullable は IConvertible を実装していないため、理にかなっています。次に、指定された値型のオブジェクトの null 許容バージョンを返すメソッドを作成することにしました。

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())equalsをデバッグするint?が、値をActivator.CreateInstace返さない場合intint?

これは私の場合です...助けはありますか?

4

2 に答える 2

0

このコードを試してください...

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);
          }
      }

ここから Convert.ChangeType() は 、LukeHによる Nullable 型で失敗します

UPDATE : 基本メソッドを非表示にします。

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; }
}

}

于 2013-04-25T14:13:44.710 に答える
0

これはうまくいくはずです:

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);
 }
于 2013-04-25T14:18:56.307 に答える