-1

I have the following class:

public class Happy : IHappy
{
   //public some properties and etc... { get; set; }
   public bool IsHappy()
   {
      //Do something...
   }
}

And it is a property in another class:

public class Exemplo
{
   public IHappy Happy { get; set; }
   // Other properties and methods etc...
}

Now inside my executable I'm creating an example instance by reflection. And I want to access the Happy.IsHappy() method through it. It is possible? I'm trying something like this:

DoThiks(string CalledClass) // where CalledClass = "Exemplo"
{
   //Instantiate the object Exemplo (working fine)
   Type ExemploType = Type.GetType(CalledClass + ",namespace");
   ConstructorInfo ExemploConstructor = ExemploType.GetConstructor(Type.EmptyTypes);
   object ExemploClassObject = ExemploConstructor.Invoke(new object[] { });

   //Problem is here.
   //Try instantiate the Happy property to call his method...
   PropertyInfo HappyPropery = ExemploType.GetProperty("Happy"); //PropertyInfo can call methods?
   MethodInfo methodHappy = HappyPropery.GetType().GetMethod("IsHappy");
   methodHappy.Invoke(HappyPropery, null);
}

As you may have noticed, I'm kind of lost in this second part... Could anyone save me?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
AlamBique
  • 167
  • 1
  • 9
  • 1
    What isn't working? Also, if your class is known at compile time, you might consider using `nameof` instead of strings. – BurnsBA Nov 07 '17 at 18:35
  • 1
    I don't see ANY property on that class...show REAL code. Also you're trying to invoke a method of the type of PropertyInfo...not on the instance of the object... – Adriano Repetti Nov 07 '17 at 18:40
  • @BurnsBA It not compile. The last 3 lines do not work, they're an approximate direction of what I'm trying to do. – AlamBique Nov 07 '17 at 18:43
  • 1
    Why are you even using reflection here? Stack Overflow has _lots_ of answers already which describe how to use reflection to navigate multiple levels of object containment, which you should read if you want to do this. But reflection is one of those things that, if you are having trouble doing simple things with it like the above, it's possible you've made the wrong choice going with reflection in the first place. Consider it a variation on the "if you have to ask how much it is, you can't afford it." – Peter Duniho Nov 07 '17 at 18:44
  • @AdrianoRepetti It really is super simplified. To stay focused and facilitate answers. The situation is coplicated, my english is bad and the code is giant. but I'll try to add an explanation of the real case. – AlamBique Nov 07 '17 at 18:46
  • @PeterDuniho Thanks for you attention. I know Stack Overflow has lots of answers... I try to find my answer.But after 1 day reading I have not found it yet. – AlamBique Nov 07 '17 at 18:49
  • See any number of the answers here: https://stackoverflow.com/search?q=%5Bc%23%5D+reflection+nested+property. – Peter Duniho Nov 07 '17 at 18:52
  • Ready! I discovered the answer and posted it. Thanks to whoever wanted to contribute. And I ignore those who showed indifference, implying that it was a simple or much debated problem, but in fact they did not know the answer. – AlamBique Nov 07 '17 at 22:03

2 Answers2

1

I guess you need

var methodHappy = HappyPropery.PropertyType.GetMethod("IsHappy");
var propertyValue = HappyPropery.GetValue(ExemploClassObject);
if (propertyValue != null)
  var isHappy = methodHappy.Invoke(propertyValue, new object[0]);

see MSDN

archnae
  • 381
  • 2
  • 5
  • Thanks! I no have a comouter now, Im in transit by bus. Lol. When I get home. I'll try and tell you if it works. – AlamBique Nov 07 '17 at 19:21
  • @AlamBique Note that the value of the property will be null unless it is being set somewhere else (i.e. in the `Exemplo` constructor). – SpruceMoose Nov 07 '17 at 20:16
  • archnae I was trying and I found! The **if** in your answer helped me to see. I will write the answer. Realy thanks! And thanks @CalC too. – AlamBique Nov 07 '17 at 21:48
  • That solves a big problem of mine. I'm going to make a post soon with the full code of what I'm really doing. And I put the link here so you can see what helped me build it. Hugs! – AlamBique Nov 07 '17 at 22:05
0

Basically, the problem is that you are trying to invoke a method of an object that does not yet exist. Do I need to assign the Happy property, or is there no way to invoke a method.

void DoThiks(string CalledClass) // where CalledClass = "Exemplo"
    {

        Type ExemploType = Type.GetType("TestesWindowsForms.TesteReflexao.Exemplo");
        ConstructorInfo ExemploConstructor = ExemploType.GetConstructor(Type.EmptyTypes);
        object ExemploClassObject = ExemploConstructor.Invoke(new object[] { });


        //Solution here:
        //Creating the object to be assigned to the property of ExampleClassObject
        Type HappyType = Type.GetType("TestesWindowsForms.TesteReflexao.Happy");
        object HappyClassObject = Activator.CreateInstance(HappyType);

        PropertyInfo HappyPropery = ExemploType.GetProperty("Happy");
        HappyPropery.SetValue(ExemploClassObject, HappyClassObject);
        //That was the assignment. Now I have the Happy property of the type object pointing to an object of type Happy.

        MethodInfo EstaHappy = HappyType.GetMethod("IsHappy");
        bool Resultado = (bool)EstaHappy.Invoke(HappyClassObject, null);

        MessageBox.Show(Resultado.ToString());
    }
AlamBique
  • 167
  • 1
  • 9