2017-08-16 148 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。 例如: -

String.ToString(); 

含蓄地實現Iconvertible

我無法確切知道你的代碼失敗的位置,你也沒有提到它,我只能告訴你,在客戶端方法的某個地方,你試圖分配兩種不同的類型,你不能這樣做。

對服務器和客戶端類都使用調試,並檢查您嘗試分配兩種不同類型的位置,然後實現所需的Iconvertible,即進行所需的轉換。

你的代碼在這裏唯一的問題是你試圖分配給不同的類型。

我的猜測是,此行帶來了麻煩:

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

當你試圖_dynamicProperties指定到Dictionary對象的時候,其實你聲明_dynamicProperties作爲IDictionary的對象。

Idictionary和字典對象之間存在細微的不同,它們不是同一類型。這是需要做的改變:

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