2017-08-16 17 views
0

DynamicPropertiesプロパティをJSONdataプロパティに移動するために、私はこのリフレクション機能を持っているため、DynamicPropertiesに来たときに例外をスローします。 "System.InvalidCastException: 'オブジェクトはIConvertibleを実装する必要があります。'"誰か助けて?プロパティをクラスの例外プロパティに移動する "System.InvalidCastException: 'オブジェクトはIConvertibleを実装する必要があります。'"

public IHttpActionResult Get(ODataQueryOptions<Client> options) 
    { 
     if(queryNew.ElementType == typeof(Client)){} 
     else //if (queryNew.ElementType.Name == "SelectSome`1") 
     { 
      var results = new List<Client>(); 
      try 
      { 
       foreach (var item in queryNew) 
       { 
        var dict = ((ISelectExpandWrapper)item).ToDictionary(); 
        var model = DictionaryToObject<Client>(dict); 
        results.Add(model); 

       } 

       return Ok(results); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 
} 

    private static T DictionaryToObject<T>(IDictionary<string, object> dict) where T : new() 
    { 
     T t = new T(); 
     PropertyInfo[] properties = t.GetType().GetProperties(); 

     foreach (PropertyInfo property in properties) 
     { 
      if (!dict.Any(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase))) 
       continue; 
      KeyValuePair<string, object> item = dict.First(x => x.Key.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)); 
      Type tPropertyType = t.GetType().GetProperty(property.Name).PropertyType; 
      Type newT = Nullable.GetUnderlyingType(tPropertyType) ?? tPropertyType; 
      if(dict.Any(x => x.Key.Equals("DynamicProperties", StringComparison.InvariantCultureIgnoreCase))) 
      { 
       object temp = JsonConvert.SerializeObject(item.Value, Formatting.Indented); //Convert.ChangeType(item.Value.ToString(), newT); 
       t.GetType().GetProperty("JsonData").SetValue(t, temp, null); 
      } 
      object newA = Convert.ChangeType(item.Value, newT); 
      t.GetType().GetProperty(property.Name).SetValue(t, newA, null); 
     } 
     return t; 
    } 

クライアントクラス

public class Client 
{ 
    public Guid Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public string ParticipantId { get; set; } 
    public DateTime? BirthDate { get; set; } 
    public int Gender { get; set; } 
    public byte? Age { get; set; } 
    public int Status { get; set; } 
    public string Notes { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public DateTime? UpdatedDate { get; set; } 
    public DateTime? DeletedDate { get; set; } 
    public bool IsDeleted { get; set; } 
    public int? DefaultLanguageId { get; set; } 
    public Guid UserId { get; set; } 
    public string JsonData { get; set; } 

    public virtual ICollection<ClientTag> ClientTags { get; private set; } 

    protected IDictionary<string, object> _dynamicProperties; 
    public IDictionary<string, object> DynamicProperties 
    { 
     get 
     { 
      if (_dynamicProperties == null) 
      { 
       if (this.JsonData == null) 
       { 
        _dynamicProperties = new Dictionary<string, object>(); 
       } 
       else 
       { 
        _dynamicProperties = Mapper.Map<IDictionary<string, object>>(JObject.Parse(this.JsonData)); 
       } 

       //_dynamicProperties.Source.ListChanged += (sender, e) => { this.AssessmentData = _dynamicProperties.Source.ToString(); }; 
      } 

      return _dynamicProperties; 
     } 
    } 
    public void UpdateJsonDataFromDynamicProperties() 
    { 
     this.JsonData = Mapper.Map<JObject>(_dynamicProperties).ToString(); 
    } 
} 

答えて

3

あなたがIConvertableエラーを取得した場合、それはあなたが別のタイプを割り当てることを試みていることを意味します。 たとえば、文字列にテキストボックスを割り当てると、オブジェクトを文字列に割り当てることができないためエラーが発生します。Iconvertibleを使用する必要があります。 例えば:

暗黙的 Iconvertibleを実装
String.ToString(); 

あなたのコードがどこで失敗したのかは分かりませんが、あなたはそれを言及しませんでした。クライアントメソッドのどこかで、2つの異なる型を割り当てようとしています。

サーバークラスとクライアントクラスの両方でdebugを使用し、2つの異なるタイプを割り当てようとしている場所を確認して、必要な変換を行うことを意味する目的のアイコンバーブルを実装します。

あなたのコードの唯一の問題は、異なるタイプに割り当てようとしていることです。あなたが実際にあなたがたIDictionaryオブジェクトとして_dynamicPropertiesを宣言したときに、辞書オブジェクトに_dynamicPropertiesを割り当てるしようとしているとき

if (this.JsonData == null) 
       { 
        _dynamicProperties = new Dictionary<string, object>(); 
       } 
       else 
       { 
        _dynamicProperties = Mapper.Map<IDictionary<string, object>>(JObject.Parse(this.JsonData)); 
       } 

私の推測では、この行は、トラブルの原因となっていることです。

Idictionaryと辞書オブジェクトの間には微妙な違いがありますが、それらは同じタイプではありません。変更を行う必要があります:

if (this.JsonData == null) 
        { 
         _dynamicProperties = new IDictionary<string, object>(); 
        } 
        else 
        { 
         _dynamicProperties = Mapper.Map<IDictionary<string, object>>(JObject.Parse(this.JsonData)); 
        }