/// <summary>
        /// Initalizes TypeGo variable
        /// </summary>
        /// <param name="arrayTypeGoInfo">TypeGo variable to initialize</param>
        /// <param name="options">Serializer or deserializer options</param>
        public void Initialize(TypeGoInfo <List <T> > arrayTypeGoInfo, ITypeOptions options)
        {
            arrayTypeGoInfo.IsNoQuotesValueType = false;
            //set the default value of variable
            arrayTypeGoInfo.DefaultValue = default;

            if (TryGetValueOfTypeGo(typeof(T), out object result))
            {
                typeGoInfo = (TypeGoInfo <T>)result;
            }
            else
            {
                typeGoInfo = BaseTypeGoInfo.Generate <T>(Options);
            }

            arrayTypeGoInfo.JsonSerialize = JsonSerialize;

            //set delegates to access faster and make it pointer directly usage for json deserializer
            arrayTypeGoInfo.JsonDeserialize = JsonDeserialize;

            //set delegates to access faster and make it pointer directly usage for binary serializer
            arrayTypeGoInfo.BinarySerialize = BinarySerialize;

            //set delegates to access faster and make it pointer directly usage for binary deserializer
            arrayTypeGoInfo.BinaryDeserialize = BinaryDeserialize;

            //create instance of object
            //arrayTypeGoInfo.CreateInstance = ReflectionHelper.GetActivator<TObject>(baseType);

            CastToArray = arrayTypeGoInfo.Cast;
        }
示例#2
0
        /// <summary>
        /// Initalizes TypeGo variable
        /// </summary>
        /// <param name="typeGoInfo">TypeGo variable to initialize</param>
        /// <param name="options">Serializer or deserializer options</param>
        public void Initialize(TypeGoInfo <TObject> typeGoInfo, ITypeOptions options)
        {
            TypeGoInfo = typeGoInfo;
            typeGoInfo.IsNoQuotesValueType = false;
            var baseType = Nullable.GetUnderlyingType(typeGoInfo.Type);

            if (baseType == null)
            {
                baseType = typeGoInfo.Type;
            }
            baseType = ReflectionHelper.GenerateTypeFromInterface(baseType, options);

            typeGoInfo.DefaultBinaryValue = new byte[] { 0 };

            //set delegates to access faster and make it pointer directly usgae
            typeGoInfo.JsonSerialize = JsonSerialize;

            //set delegates to access faster and make it pointer directly usage for json deserializer
            typeGoInfo.JsonDeserialize = JsonDeserialize;

            //set delegates to access faster and make it pointer directly usage for binary serializer
            typeGoInfo.BinarySerialize = BinarySerialize;

            //set delegates to access faster and make it pointer directly usage for binary deserializer
            typeGoInfo.BinaryDeserialize = BinaryDeserialize;

            //create instance of object
            typeGoInfo.CreateInstance = ReflectionHelper.GetActivator <TObject>(baseType);

            typeGoInfo.SerializeProperties   = typeGoInfo.Properties.Values.ToArray();
            typeGoInfo.DeserializeProperties = typeGoInfo.Properties.Values.ToArray();
            GenerateProperties();
        }
示例#3
0
 /// <summary>
 /// Generate default variables to an option
 /// </summary>
 /// <param name="options"></param>
 public static void GenerateDefaultVariables(ITypeOptions options)
 {
     foreach (var variableType in ReflectionHelper.VariableTypes)
     {
         var method = typeof(BaseTypeGoInfo).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic)
                      .FirstOrDefault(x => x.Name == "Generate" && x.GetGenericArguments().Length == 1);
         var genericMethod = method.MakeGenericMethod(variableType.Key);
         genericMethod.Invoke(null, new object[] { options });
     }
 }
示例#4
0
        public ITypeOptions Create(ModelOptions options, Type type)
        {
            ITypeOptions result = null;

            if (type.IsDictionary(out Type keyType, out Type valueType) &&
                keyType == typeof(string) &&
                valueType == typeof(object))
            {
                result = _typeOptions;
            }

            return(result);
        }
示例#5
0
        /// <summary>
        /// Initalizes TypeGo variable
        /// </summary>
        /// <param name="typeGoInfo">TypeGo variable to initialize</param>
        /// <param name="options">Serializer or deserializer options</param>
        public void Initialize(TypeGoInfo <byte[]> typeGoInfo, ITypeOptions options)
        {
            typeGoInfo.IsNoQuotesValueType = false;
            //set the default value of variable
            typeGoInfo.DefaultValue    = default;
            typeGoInfo.JsonSerialize   = JsonSerialize;
            typeGoInfo.JsonDeserialize = JsonDeserialize;

            //set delegates to access faster and make it pointer directly usage for binary serializer
            typeGoInfo.BinarySerialize = BinarySerialize;

            //set delegates to access faster and make it pointer directly usage for binary deserializer
            typeGoInfo.BinaryDeserialize = BinaryDeserialize;
        }
示例#6
0
        public virtual ITypeOptions GetTypeOptions(Type type)
        {
            return(_options.GetOrAdd(type, keyType =>
            {
                for (int i = TypeOptionsFactories.Count - 1; i >= 0; i--)
                {
                    ITypeOptions typeOptions = TypeOptionsFactories[i].Create(this, keyType);
                    if (typeOptions != null)
                    {
                        return typeOptions;
                    }
                }

                throw new InvalidOperationException($"Can't get options for type {type.GetFriendlyName()}.");
            }));
        }
示例#7
0
        public bool IsBackReference(
            ModelOptions options,
            TypeMap typeMap,
            IMemberOptions memberOptions,
            out BackReferenceMap backReferenceMap)
        {
            bool result = false;

            backReferenceMap = null;

            ITypeOptions memberTypeOptions = options.GetTypeOptions(memberOptions.Type);

            if (typeMap.Parent != null)
            {
                if (typeMap.TargetOptions.IsEntity &&
                    (typeMap.Parent.TargetOptions.Type == memberTypeOptions.Type ||
                     typeMap.Parent.TargetOptions.Type == memberTypeOptions.ItemType))
                {
                    backReferenceMap = new BackReferenceMap
                    {
                        MemberOptions     = memberOptions,
                        MemberTypeOptions = options.GetTypeOptions(memberOptions.Type),
                        Parent            = typeMap.Parent
                    };
                    result = true;
                }
                else if (typeMap.Parent.Parent != null &&
                         typeMap.Parent.TargetOptions.IsCollection &&
                         (typeMap.Parent.Parent.TargetOptions.Type == memberTypeOptions.Type ||
                          typeMap.Parent.Parent.TargetOptions.Type == memberTypeOptions.ItemType))
                {
                    backReferenceMap = new BackReferenceMap
                    {
                        MemberOptions     = memberOptions,
                        MemberTypeOptions = options.GetTypeOptions(memberOptions.Type),
                        Parent            = typeMap.Parent.Parent
                    };
                    result = true;
                }
            }

            return(result);
        }
        /// <summary>
        /// property info of a type
        /// </summary>
        public PropertyGoInfo(PropertyInfo property, ITypeOptions options)
        {
            //coming from change structure
            if (property == null)
            {
                if (options.TryGetValueOfTypeGo(typeof(TPropertyType), out object typeGoInfoProperty))
                {
                    TypeGoInfo = (TypeGoInfo<TPropertyType>)typeGoInfoProperty;
                }
                else
                {
                    TypeGoInfo = BaseTypeGoInfo.Generate<TPropertyType>(options);
                }
                GetValue = (ref TObject obj) => default;
                SetValue = (TObject obj, TPropertyType value) => { };
            }
            //coming from normal binary Go
            else
            {
                if (options.TryGetValueOfTypeGo(property.PropertyType, out object typeGoInfoProperty))
                {
                    TypeGoInfo = (TypeGoInfo<TPropertyType>)typeGoInfoProperty;
                }
                else
                {
                    TypeGoInfo = BaseTypeGoInfo.Generate<TPropertyType>(options);
                }

                PropertyCallerInfo<TObject, TPropertyType> propertyCaller;

                try
                {
                    propertyCaller = ReflectionHelper.GetDelegateInstance<TObject, TPropertyType>(property);
                }
                catch (Exception ex)
                {
                    throw new Exception($"Cannot create delegate for property {property.Name} in type {TypeGoInfo.Type.FullName}", ex);
                }
                GetValue = propertyCaller.GetValueAction;
                SetValue = propertyCaller.SetValueAction;
            }
            Type = typeof(TPropertyType);
        }
示例#9
0
        /// <summary>
        /// Initialize variable to typeGo
        /// </summary>
        /// <param name="typeGoInfo">TypeGo</param>
        /// <param name="options">Serializer's options or settings</param>
        public void InitializeBase(BaseTypeGoInfo typeGoInfo, ITypeOptions options)
        {
            typeGoInfo.Variable = this;
            CurrentCulture      = options.CurrentCulture;
            Options             = options;

            AddTypes                  = options.AddTypes;
            TryGetValueOfTypeGo       = options.TryGetValueOfTypeGo;
            HasGenerateRefrencedTypes = options.HasGenerateRefrencedTypes;

            var thisType   = this.GetType();
            var findMethod = thisType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                             .Where(x => x.Name == "Initialize").FirstOrDefault();

            if (findMethod == null)
            {
                throw new Exception($"I tried to find method Initialize in type of {thisType.FullName} but not found this method pls add it");
            }
            findMethod.Invoke(this, new object[] { typeGoInfo, options });
        }
示例#10
0
        /// <summary>
        /// Initalizes TypeGo variable
        /// </summary>
        /// <param name="typeGoInfo">TypeGo variable to initialize</param>
        /// <param name="options">Serializer or deserializer options</param>
        public void Initialize(TypeGoInfo <string> typeGoInfo, ITypeOptions options)
        {
            Encoding = options.Encoding;
            typeGoInfo.IsNoQuotesValueType = false;

            //set the default value of variable
            typeGoInfo.DefaultValue = default;

            //set delegates to access faster and make it pointer directly usage
            typeGoInfo.JsonSerialize = JsonSerialize;

            //set delegates to access faster and make it pointer directly usage for json deserializer
            typeGoInfo.JsonDeserialize = JsonDeserialize;

            //set delegates to access faster and make it pointer directly usage for binary serializer
            typeGoInfo.BinarySerialize = BinarySerialize;

            //set delegates to access faster and make it pointer directly usage for binary deserializer
            typeGoInfo.BinaryDeserialize = BinaryDeserialize;

            IsUnixNewLine = Environment.NewLine.Length == 1;
        }
示例#11
0
        public void TestMappingComponent()
        {
            // GIVEN a configured application
            Module module = new Module();

            // GIVEN a db context
            module.AddDbContext <TestDbContext>(cfg =>
            {
                var connection = new SqliteConnection($"DataSource=file:TestMapping?mode=memory&cache=shared");
                connection.Open();
                cfg.UseSqlite(connection);
            });

            // GIVEN a configuration
            module.AddDbContextConfiguration <TestDbContext, TestDbContextConfiguration>();

            // WHEN the application is built
            IServiceCollection services = new ServiceCollection();

            module.ConfigureServices(services, null, null);

            IServiceProvider serviceProvider = services.BuildServiceProvider();

            // THEN model configuration is applied
            TestDbContext dbContext = serviceProvider.GetService <TestDbContext>();

            // get the random annotation to check that ConfigureModel was executed.
            IEntityType entityType = dbContext.Model.FindEntityType(typeof(TestDocument));
            IProperty   property   = entityType.FindProperty("Id");

            Assert.Contains(property.GetAnnotations(), a => a.Name == "test annotation");

            // THEN mapper configuration is applied
            ITypeOptions typeOptions = dbContext.GetInfrastructure().GetService <Mapper>().GetTypeOptions(typeof(TestDocument));

            Assert.True(typeOptions.GetMember("Name").IsKey);
        }
        /// <summary>
        /// Generates types interface to new types
        /// </summary>
        /// <param name="type"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        internal static Type GenerateTypeFromInterface(Type type, ITypeOptions options)
        {
            Type[] genericTypes = null;
            if (type.GenericTypeArguments.Length > 0)
            {
                genericTypes = type.GenericTypeArguments;
                type         = type.GetGenericTypeDefinition();
            }

            if (options.CustomTypeChanges.TryGetValue(type, out Type newType))
            {
                type = newType;
            }

            if (genericTypes != null)
            {
                for (int i = 0; i < genericTypes.Length; i++)
                {
                    genericTypes[i] = GenerateTypeFromInterface(genericTypes[i], options);
                }
                type = type.MakeGenericType(genericTypes);
            }
            return(type);
        }
示例#13
0
        /// <summary>
        /// Initializes a TypeGo for a runtime type
        /// the typeGo makes use of everything faster with easy access
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TypeGoInfo <T> Generate <T>(ITypeOptions options)
        {
            lock (_lockobj)
            {
                Type type = typeof(T);
                if (options.TryGetValueOfTypeGo(type, out object find))
                {
                    return((TypeGoInfo <T>)find);
                }
                var  baseType   = Nullable.GetUnderlyingType(type);
                bool isNullable = false;
                //when type is not nullable
                if (baseType == null)
                {
                    baseType = type;
                }
                else
                {
                    isNullable = true;
                }

                TypeGoInfo <T> typeGoInfo = new TypeGoInfo <T>
                {
                    Properties = new Dictionary <string, BasePropertyGoInfo <T> >(),
                    Type       = type,
                };

                options.AddTypes(type, typeGoInfo);

                if (isNullable)
                {
                    if (baseType == typeof(bool))
                    {
                        InitializeVariable <BoolNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(DateTime))
                    {
                        InitializeVariable <DateTimeNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(uint))
                    {
                        InitializeVariable <UIntNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(long))
                    {
                        InitializeVariable <LongNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(short))
                    {
                        InitializeVariable <ShortNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(byte))
                    {
                        InitializeVariable <ByteNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(double))
                    {
                        InitializeVariable <DoubleNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(float))
                    {
                        InitializeVariable <FloatNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(decimal))
                    {
                        InitializeVariable <DecimalNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(sbyte))
                    {
                        InitializeVariable <SByteNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(ulong))
                    {
                        InitializeVariable <ULongNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(ushort))
                    {
                        InitializeVariable <UShortNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(int))
                    {
                        InitializeVariable <IntNullableVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(Guid))
                    {
                        InitializeVariable <GuidNullableVariable>(typeGoInfo, options);
                    }
                    //else if (baseType == typeof(byte[]))
                    //    InitializeVariable<ByteArrayNullableVariable>(typeGoInfo, options);
                    else if (baseType.IsEnum)
                    {
                        BaseVariable variable = (BaseVariable)typeof(EnumNullableVariable <>).MakeGenericType(baseType)
                                                .GetMethod("Initialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                                                .Invoke(null, new object[] { });
                        variable.InitializeBase(typeGoInfo, options);
                    }
                }
                else
                {
                    if (baseType == typeof(bool))
                    {
                        InitializeVariable <BoolVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(DateTime))
                    {
                        InitializeVariable <DateTimeVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(uint))
                    {
                        InitializeVariable <UIntVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(long))
                    {
                        InitializeVariable <LongVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(short))
                    {
                        InitializeVariable <ShortVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(byte))
                    {
                        InitializeVariable <ByteVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(double))
                    {
                        InitializeVariable <DoubleVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(float))
                    {
                        InitializeVariable <FloatVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(decimal))
                    {
                        InitializeVariable <DecimalVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(sbyte))
                    {
                        InitializeVariable <SByteVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(ulong))
                    {
                        InitializeVariable <ULongVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(ushort))
                    {
                        InitializeVariable <UShortVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(int))
                    {
                        InitializeVariable <IntVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(byte[]))
                    {
                        InitializeVariable <ByteArrayVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(string))
                    {
                        InitializeVariable <StringVariable>(typeGoInfo, options);
                    }
                    else if (baseType == typeof(Guid))
                    {
                        InitializeVariable <GuidVariable>(typeGoInfo, options);
                    }
                    else if (baseType.IsEnum)
                    {
                        BaseVariable variable = (BaseVariable)typeof(EnumVariable <>).MakeGenericType(typeof(T))
                                                .GetMethod("Initialize", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                                                .Invoke(null, new object[] { });
                        variable.InitializeBase(typeGoInfo, options);
                    }
                    //array data
                    else if (baseType.IsArray)
                    {
                        var elementType = baseType.GetElementType();
                        var method      = typeof(BaseTypeGoInfo)
                                          .GetMethod("InitializeVariable", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                                          .MakeGenericMethod(typeof(ArrayVariable <>).MakeGenericType(elementType));
                        method.Invoke(null, new object[] { typeGoInfo, options });
                    }
                    //enumrable list data
                    else if (baseType.GetGenericArguments().Length > 0 && baseType.GetGenericTypeDefinition() == typeof(List <>))
                    {
                        var elementType = baseType.GetGenericArguments()[0];
                        var method      = typeof(BaseTypeGoInfo)
                                          .GetMethod("InitializeVariable", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
                                          .MakeGenericMethod(typeof(GenericListVariable <>).MakeGenericType(elementType));
                        method.Invoke(null, new object[] { typeGoInfo, options });
                    }
                    //object daat
                    else
                    {
                        InitializeVariable <ObjectVariable <T> >(typeGoInfo, options);
                    }
                }

                return(typeGoInfo);
            }
        }
示例#14
0
        /// <summary>
        /// Initializes a variable to a TypeGo
        /// </summary>
        public static TVariable InitializeVariable <TVariable>(BaseTypeGoInfo typeGoInfo, ITypeOptions options)
            where TVariable : BaseVariable, new()
        {
            TVariable variable = new TVariable();

            variable.InitializeBase(typeGoInfo, options);
            return(variable);
        }