2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00
This commit is contained in:
2020-01-26 14:30:39 +03:00
parent 5f4660fde2
commit 61a1683c26
22 changed files with 409 additions and 346 deletions

View File

@ -43,9 +43,13 @@ namespace Esyur.Data
{
public static object CastConvert(object value, Type destinationType)
{
if (value == null)
return null;
//if (destinationType.IsArray && destinationType.GetElementType().IsArray)
// Console.Beep();
var sourceType = value.GetType();
if (destinationType == sourceType)
@ -54,10 +58,9 @@ namespace Esyur.Data
}
else
{
if (sourceType.IsArray)
if (sourceType.IsArray && (destinationType.IsArray || destinationType == typeof(object)))
{
if (destinationType.IsArray)
destinationType = destinationType.GetElementType();
destinationType = destinationType.GetElementType();
var v = value as Array;
@ -65,29 +68,34 @@ namespace Esyur.Data
for (var i = 0; i < rt.Length; i++)
{
try
{
#if NETSTANDARD
if (destinationType.GetTypeInfo().IsInstanceOfType(v.GetValue(i)))
#else
if (destinationType.IsInstanceOfType(v.GetValue(i)))
#endif
rt.SetValue(v.GetValue(i), i);
else
rt.SetValue(Convert.ChangeType(v.GetValue(i), destinationType), i);
}
catch
{
rt.SetValue(null, i);
}
rt.SetValue(CastConvert(v.GetValue(i), destinationType), i);
// try
// {
//#if NETSTANDARD
// if (destinationType.GetTypeInfo().IsInstanceOfType(v.GetValue(i)))
//#else
// if (destinationType.IsInstanceOfType(v.GetValue(i)))
//#endif
// rt.SetValue(v.GetValue(i), i);
// else
// rt.SetValue(Convert.ChangeType(v.GetValue(i), destinationType), i);
// }
// catch
// {
// rt.SetValue(null, i);
// }
}
return rt;
}
else
{
try
{
var underType = Nullable.GetUnderlyingType(destinationType);
if (underType != null)
{
@ -96,15 +104,26 @@ namespace Esyur.Data
else
destinationType = underType;
}
#if NETSTANDARD
if (destinationType.GetTypeInfo().IsInstanceOfType(value))
#else
if (destinationType.IsInstanceOfType(value))
#endif
{
return value;
}
else if (typeof(IUserType).IsAssignableFrom(destinationType))
{
var rt = Activator.CreateInstance(destinationType) as IUserType;
rt.Set(value);
return rt;
}
else if (sourceType == typeof(Structure) && sourceType.IsAssignableFrom(destinationType))
{
return Structure.FromStructure((Structure)value, destinationType);
}
else
{
return Convert.ChangeType(value, destinationType);
}
}
catch
{