Ejemplo n.º 1
0
        public static TImplementation Transform <TImplementation, TModel>(TModel model, IOsuClient client)
            where TModel : class
        {
            var modelType        = typeof(TModel);
            var implementingType = typeof(TImplementation);

            var assemblyTypes = implementingType.Assembly
                                .GetTypes()
                                .Where(x => x.GetInterfaces().Length > 0 && !x.IsInterface);

            var modelProperties          = modelType.GetProperties();
            var implementingProperties   = implementingType.GetProperties();
            var implementingConstructors = implementingType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
            var implementingConstructor  = implementingConstructors.First(x => x.GetParameters().Length == 0);
            var implementingInstance     = implementingConstructor.Invoke(null);

            foreach (var modelProperty in modelProperties)
            {
                var implementingProperty = implementingProperties
                                           .FirstOrDefault(x => x.Name == modelProperty.Name);

                if (implementingProperty is null)
                {
                    continue;
                }

                var modelValue = modelProperty.GetValue(model);
                if (modelValue is null)
                {
                    continue;
                }

                var modelValueType = modelValue.GetType();
                if (!modelValueType.IsClass ||
                    modelValueType.Name == "String" ||
                    modelValueType.IsAssignableTo(implementingProperty.PropertyType))
                {
                    if (implementingProperty.PropertyType.IsEnum)
                    {
                        modelValue = Enum.Parse(implementingProperty.PropertyType, modelValue.ToString(), true);
                    }

                    implementingProperty.SetValue(implementingInstance, modelValue);
                    continue;
                }

                if (modelValue is IEnumerable modelValueEnumerable)
                {
                    var valueImplementingTypeEnumerable = assemblyTypes.FirstOrDefault(x => x.IsAssignableTo(implementingProperty.PropertyType.GenericTypeArguments.First()));
                    var tempList = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(valueImplementingTypeEnumerable));

                    foreach (var value in modelValueEnumerable)
                    {
                        var itfValueEnumerable = Method
                                                 .MakeGenericMethod(valueImplementingTypeEnumerable, modelValueType.GenericTypeArguments.First())
                                                 .Invoke(null, new[] { value, client });

                        if (itfValueEnumerable is IClientEntity)
                        {
                            valueImplementingTypeEnumerable.GetProperty("Client").SetValue(itfValueEnumerable, client);
                        }

                        tempList.Add(itfValueEnumerable);
                    }

                    implementingProperty.SetValue(implementingInstance, tempList);
                    continue;
                }

                var valueImplementingType = assemblyTypes.LastOrDefault(x => x.IsAssignableTo(implementingProperty.PropertyType));
                var itfValue = Method.MakeGenericMethod(valueImplementingType, modelValueType)
                               .Invoke(null, new[] { modelValue, client });

                if (itfValue is IClientEntity)
                {
                    valueImplementingType.GetProperty("Client").SetValue(itfValue, client);
                }

                implementingProperty.SetValue(implementingInstance, itfValue);
            }

            if (implementingInstance is IClientEntity)
            {
                implementingType.GetProperty("Client").SetValue(implementingInstance, client);
            }

            return((TImplementation)implementingInstance);
        }
Ejemplo n.º 2
0
 public RequestService(IOsuClient osuClient)
 {
     this.osuClient = osuClient;
 }
Ejemplo n.º 3
0
 public OsuController(IOsuClient osuClient)
 {
     this.osuClient = osuClient;
 }
Ejemplo n.º 4
0
 public OsuTestService(ILogger <OsuTestService> logger, IOsuClient client)
 {
     _logger = logger;
     _client = client;
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Converts a <see cref="Mods"/> into a string separated with the modes separator from the <see cref="OsuClientConfiguration"/>.
 /// </summary>
 /// <param name="mode">Mode to convert.</param>
 /// <param name="instance">Instance on which we use the mode separator.</param>
 /// <returns></returns>
 public static string ToModString(this Mods mode, IOsuClient instance)
 {
     return(mode.ToModString(instance.Configuration.ModFormatSeparator));
 }