Run method of one nested class as property in another in C#

Asked

Viewed 191 times

1

I have the following class:

public class Feliz : IFeliz
{
   //public algumas propeiedades e tals... { get; set; }
   public bool EstaFeliz()
   {
      //Faz alguma coisa...
   }
}

And she’s a property in another class:

public Exemplo
{
   public IFeliz Feliz { get; set; }
   // Outras propriedades e métodos etc...
}

Now inside my executable I am creating by Reflection an instance of Example. And I want to access the method Happy.Estafeliz() through it. Is there a way? I’m trying something like this:

Executor(string ClasseChamada) // onde ClasseChamada = "Exemplo"
{
   //Pega o objeto Exemplo blz! (já testado)
   Type ExemploType = Type.GetType(ClasseChamada + ",namespace") ;
   ConstructorInfo ExemploConstructor = ExemploType.GetConstructor(Type.EmptyTypes);
   object ExemploClassObject = ExemploConstructor.Invoke(new object[] { });

   //Tentativa de pegar a propriedade Feliz para chamar seu método...
   PropertyInfo FelizPropery = ExemploType.GetProperty("Feliz"); //PropertyInfo permitem chamar métodos?
   MethodInfo methodFeliz = FelizType.GetType().GetMethod("EstaFeliz");
   methodFeliz.Invoke(FelizPropery, null);
}

As you may have noticed I’m kind of lost in that second part there... someone could save me?

  • 1

    The first question that needs to be asked is why are you using reflection for something that doesn’t need it?

  • Thank you for your attention @Maniero. I greatly simplified the example. Even to be objective and to facilitate the answers. I need this because the Example class may actually be 1 of 60 other classes that are part of my Persistence. I only take the class name as a string depending on where and by which object it will be invoked in the system.

  • I even made a small change in the context of my example for you to visualize better. I changed the beginning of the 3rd code snippet.

1 answer

1


Still, basically the problem is that you’re trying to invoke a method from an object that doesn’t exist yet.

In its definition of class Exemplo, you have a property of the type IFeliz not yet assigned by any object, so there is no way you can invoke a method.

Assuming you have a string with the name of the class to be assigned to the property Feliz (if it’s the same string, easier), just create a new instance of that class and assign it to the property. I usually do so:

Type tipo = Type.GetType("namespace." + nomeClasse);
var instancia = Activator.CreateInstance(tipo);

So far only created the object to be assigned to the property of Exemploclassobject

FelizPropery.SetValue(ExemploClassObject, instancia);

That was the assignment. Now you have the property Feliz, of the type object Exemplo pointing to an object of the type Feliz.

I followed your variable name FelizPropery...

Apart from the fact that you don’t need to do the assignment to invoke the method, you now have the instancia on hand to invoke it, knowing that it is the same object that has been assigned to the property Feliz of the type object Exemplo.

MethodInfo metodo = tipo.GetMethod("EstaFeliz");
bool resultado = (bool)metodo.Invoke(instancia, null);
  • Thank you! I am on the bus now. But ch3gando at home test and put the result.

  • Arao you are a genius! Thank you! That makes perfect sense. I was facing that the instance was "automatically" in the object it belonged to (i.e., in Example). But as you have shown, I need to create it. I tested it here and it worked fine. It solves a big problem of mine. I’ll do a post soon with the full code of what I’m really doing and put the link here for you to see what helped me build. Hugs!

  • Good Still! I hope you’re doing something interesting!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.