Example #1
0
 public TypeRegistry(Assembly assembly)
 {
     Assembly = assembly;
     foreach (var x in assembly.GetTypes())
     {
         if (!x.IsClass && !x.IsEnum)
         {
             continue;
         }
         var name = ConfigurationName.GetOrNull(x);
         if (!name.HasValue)
         {
             continue;
         }
         typeMapping.Add(name.Value.Fullname, x);
     }
 }
Example #2
0
 public object MapFrom1C(object source, Type type)
 {
     if (source == null || source == DBNull.Value)
     {
         return(null);
     }
     if (type == typeof(object))
     {
         if (source is MarshalByRefObject)
         {
             var typeName = GetFullName(source);
             type = mappingSource.TypeRegistry.GetType(typeName);
         }
         else
         {
             type = source.GetType();
         }
     }
     type = Nullable.GetUnderlyingType(type) ?? type;
     if (type == typeof(DateTime))
     {
         var dateTime = (DateTime)source;
         return(dateTime == nullDateTime ? null : source);
     }
     if (type == typeof(Guid))
     {
         var guid = Guid.Parse(globalContext.String(source));
         return(guid == Guid.Empty ? (object)null : guid);
     }
     if (type.IsEnum)
     {
         return(Call.IsEmpty(source) ? null : MapEnumFrom1C(type, source));
     }
     if (type == typeof(Type))
     {
         return(ConvertType(source));
     }
     if (type == typeof(Type[]))
     {
         var typesObject = ComHelpers.Invoke(source, "Типы");
         var typesCount  = Call.Количество(typesObject);
         var result      = new Type[typesCount];
         for (var i = 0; i < result.Length; i++)
         {
             var typeObject = Call.Получить(typesObject, i);
             result[i] = ConvertType(typeObject);
         }
         return(result);
     }
     if (typeof(Abstract1CEntity).IsAssignableFrom(type))
     {
         var configurationName = ConfigurationName.GetOrNull(type);
         var isEmpty           = configurationName.HasValue &&
                                 configurationName.Value.HasReference &&
                                 Call.IsEmpty(source);
         if (isEmpty)
         {
             return(null);
         }
         var result = (Abstract1CEntity)FormatterServices.GetUninitializedObject(type);
         result.Controller = new EntityController(new ComValueSource(source, this, false));
         return(result);
     }
     if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
     {
         var itemType = type.GetGenericArguments()[0];
         if (!typeof(Abstract1CEntity).IsAssignableFrom(itemType))
         {
             throw new InvalidOperationException("assertion failure");
         }
         var itemsCount = Call.Количество(source);
         var list       = ListFactory.Create(itemType, null, itemsCount);
         for (var i = 0; i < itemsCount; ++i)
         {
             list.Add(MapFrom1C(Call.Получить(source, i), itemType));
         }
         return(list);
     }
     return(source is IConvertible?Convert.ChangeType(source, type) : source);
 }