mercredi 5 juillet 2023

How I can use method call with some argument [Reflection]

I try to use Dynamic DLL (Hangfire Core). Some methods of this lib has non-standard arguments, for example


 public static IGlobalConfiguration UseSimpleAssemblyNameTypeSerializer([NotNull] this IGlobalConfiguration configuration)

If i want call method with reflection help, I did:

  1. Load Assembly
  2. Get type from assembly
  3. Get method from type
  4. Try Invoke method

and, I got error:

Object of type 'System.RuntimeType' cannot be converted to type 'Hangfire.IGlobalConfiguration'."

My code:

        private static void GetField(string typeName, string pathToDll, string fieldName)
        {
            Type GlobalConfigurationType = Assembly.LoadFile(@pathToDll).GetType(typeName);
            Type GlobalConfigurationExtensionType = Assembly.LoadFile(@pathToDll).GetType("Hangfire.GlobalConfigurationExtensions");

            var propertyInfos = GlobalConfigurationType.GetProperties();
            var someProperty = propertyInfos[0];

            var UseSimpleAssemblyNameTypeSerializerMethod = GlobalConfigurationExtensionType.GetMethod("UseSimpleAssemblyNameTypeSerializer");

            UseSimpleAssemblyNameTypeSerializerMethod?.Invoke(null, new object[] { someProperty.PropertyType });
        }

How to do the conversion correctly?

I tried to set .PropertyType as argument





Aucun commentaire:

Enregistrer un commentaire