在类中将属性移动到另一个属性会引发异常"System.InvalidCastException: 'Object must implement IConvertible.'"?



我试图找到将动态属性移动到 JSONdata 属性的东西,因为我有这个反射函数来完成这项工作,当涉及到动态属性时,它会抛出异常"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();
}
}

当您收到可逆转的错误时,这意味着您已尝试将一种类型分配给另一种类型。 例如,如果您尝试将文本框分配给字符串,您将收到一个错误,因为您无法将对象分配给字符串,您必须使用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分配给字典对象时,实际上您将_dynamicProperties声明为 IDictionary 对象。

Idictionary 和字典对象之间有一个微妙的区别,它们不属于同一类型。 这是需要完成的更改:

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

最新更新