samedi 14 septembre 2019

Reflection using generic types when class is unknown

I'm loading a model with reflection:

        private void LoadAssemblies()
        {
            Assembly assembly = Assembly.LoadFrom(modelDll);
            var types = assembly.GetTypes();
            inputType = assembly.GetType(types.Where(x => x.FullName.Contains("Input")).FirstOrDefault().FullName);
            outputType = assembly.GetType(types.Where(x => x.FullName.Contains("Output")).FirstOrDefault().FullName);
            inputModel = Activator.CreateInstance(inputType);
            outputModel = Activator.CreateInstance(outputType);
            inputModel = JsonConvert.DeserializeObject(jsonPredict);
        }

Then I need to call a method with two generyc types

var predEngine = mlContext.Model.CreatePredictionEngine<typeof(inputModel), typeof(outputModel)>(mlModel); //here I cant convert this model loaded at runtime into a type to this generic method.

I've tried

var instance = Activator.CreateInstance(typeof(ModelOperationsCatalog)); //eror here
//System.MissingMethodException: 'No parameterless constructor defined for this object.'

instance.GetType()
                .GetMethod("CreatePredictionEngine")
                .MakeGenericMethod(inputType, outputType)
                .Invoke(this, new object[] { });*/





Aucun commentaire:

Enregistrer un commentaire