Example #1
0
        public object Resolve(Type type, string name = null)
        {
            var          mappingKey   = new MappingKey(type, name);
            MappingValue mappingValue = null;

            if (!string.IsNullOrEmpty(name))
            {
                var mapping = _mappings.FirstOrDefault(it => it.Key.InstanceName == mappingKey.InstanceName && it.Key.Type == mappingKey.Type).Value;
                mappingValue = mapping;
            }

            if (string.IsNullOrEmpty(name) && _mappings.Any(t => t.Key.Type == mappingKey.Type))
            {
                mappingValue = _mappings.FirstOrDefault(t => t.Key.Type == mappingKey.Type).Value;
            }
            else if (mappingValue == null)
            {
                throw new NotFoundTypeFromResolveException($"Not contain definition registered for {type.Name} {name}");
            }

            if (_mappingTypes.TryGetValue(mappingValue.TypeRegister, out Func <MappingValue, object> funcResult))
            {
                return(funcResult.Invoke(mappingValue));
            }

            return(null);
        }
Example #2
0
    protected float CalculateLinearMapping(Transform updateTransform, MappingValue targetValue = MappingValue.xValue)
    {
        Vector3 direction    = endTransform.position - startTransform.position;
        Vector3 displacement = updateTransform.position - startTransform.position;

        return(displacement[(int)targetValue] / direction[(int)targetValue]);
    }
Example #3
0
        private void Register(RegisterType registerType, Type from, Type to, string instanceName = null, object obj = null)
        {
            var mappingKey = new MappingKey(from, instanceName);

            if (!_mappings.ContainsKey(mappingKey))
            {
                var mappingValue = new MappingValue(registerType, to, from, instanceName, obj);
                _mappings.Add(mappingKey, mappingValue);
            }
        }
Example #4
0
        private object ResolveInstanceFromSingleton(MappingValue mappingValue)
        {
            var mappingKey = new MappingKey(mappingValue.TypeBase, mappingValue.InstanceName);

            if (mappingValue.Instance == null)
            {
                var mappingVal = (MappingValue)mappingValue.Clone();
                mappingVal.TypeRegister = Enum.RegisterType.Type;
                mappingValue.Instance   = ResolveInstanceFromType(mappingVal);
                _mappings[mappingKey]   = mappingValue;
            }
            return(mappingValue.Instance);
        }
Example #5
0
        private object ResolveInstanceFromType(MappingValue mappingValue)
        {
            List <object> @params     = new List <object>();
            var           constructor = mappingValue.TypeResolve.GetConstructors().FirstOrDefault(it => it.IsPublic);

            if (constructor == null)
            {
                throw new TypeToSolveDoesNotContainPublicConstructorException($"Type to solve does not contain a public constructor {mappingValue.TypeResolve.Name}");
            }
            foreach (var parameterInfo in constructor.GetParameters())
            {
                @params.Add(Resolve(parameterInfo.ParameterType));
            }
            object[] paramsObject = @params?.ToArray();
            return(Activator.CreateInstance(mappingValue.TypeResolve, paramsObject != null && paramsObject?.Count() > 0 ? paramsObject : null));
        }
Example #6
0
 private object ResolveInstanceFromInstace(MappingValue mappingValue)
 {
     return(((MappingValue)mappingValue.Clone()).Instance);
 }