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?
The first question that needs to be asked is why are you using reflection for something that doesn’t need it?
– Maniero
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.
– AlamBique
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.
– AlamBique