4 Replies - 1376 Views - Last Post: 27 June 2014 - 05:35 AM Rate Topic: -----

#1 fernandomierhicks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 14

Invoke method in nested class using reflection

Posted 25 June 2014 - 03:00 PM

Hi,

I am having problems invoking methods in a nested class using reflection.
I have the following:

A parent class, Group, that holds instances of a simple class called Signal. I want to modify the number of instances inside the group class often. So, all my code has to be dynamic and use reflection to know how many instances of signal there are inside the Group class.
class Group{

public static Signal name1 { get; set; }
public static Signal name2 { get; set; }
public static Signal name3 { get; set; }
...

}




The signal class is just a simple class with methods and properties:

class Signal{

public string Description { get; set; }
public int Channel { get; set; }

public void Method1(int parameter1, int parameter2){
//do stuff
}




I am able to read the values of the properties of the static signal classes by using nested reflection like this:

Group variables = new Group();
            var type = typeof(Group);
            var properties = type.GetProperties();

            foreach (PropertyInfo property in properties)//Looop for every signal
            {
                string signal_variable_name = property.Name;
                string signal_property_value = (string)GetPropertyValue(variables, signal_variable_name + ".Description");


public static object GetPropertyValue(object obj, string propertyName)
        {
            foreach (var prop in propertyName.Split('.').Select(s => obj.GetType().GetProperty(s)))
                obj = prop.GetValue(obj, null);

            return obj;
        }




I had no luck invoking the method of the instances of signal class that are inside the Group class. I tried getting the methods name using getMethods() but could not navigate through the syntax.

How could I invoke and pass parameters to the method of the instances of signal using reflection?
Is there a better way of accessing the properties and methods of nested classes?

Thanks

Fernando

Is This A Good Question/Topic? 0
  • +

Replies To: Invoke method in nested class using reflection

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6116
  • View blog
  • Posts: 21,052
  • Joined: 05-May 12

Re: Invoke method in nested class using reflection

Posted 25 June 2014 - 07:28 PM

Show us your code for trying to reflect down to find the methods and invoke them.

As a quick aside, I'm both impressed and frightened by your GetPropertyValue() method. I applaud the cleverness of it as it parses and walks down the path to get the deepest property. I'm also keeping my fingers crossed that I would never have to be the one who has to debug into it if something does go wrong.

My advice is not to get too clever by trying to navigate through the syntax. Instead, be explicit about calling GetProperty() on the Group object to get back a Signal, and then calling GetMethod() on the Signal object to get the method.
Was This Post Helpful? 0
  • +
  • -

#3 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Invoke method in nested class using reflection

Posted 26 June 2014 - 01:03 AM

View Postfernandomierhicks, on 25 June 2014 - 11:00 PM, said:

A parent class, Group, that holds instances of a simple class called Signal. I want to modify the number of instances inside the group class often.


Normally the Group class would have a List<Signal> or some other collection. Is there a specific reason why you need to hard-code the signals as members of the class and then use reflection?
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Invoke method in nested class using reflection

Posted 26 June 2014 - 07:11 AM

Also... Why is the parent class reaching into and micromanaging the nested class? That not generally good design. Have the parent class raise an event such as "DropSignal(3)". The nested class would subscribe to the parent. It would then react as it is meant to. Its not the job of class one to micro-control class two. If you were going to do that there is no point is seperating the classes.

An analogy I like is the bingo caller. The guy stands on stage and yells "N-14"; that's the event. The subscribers (the players with cards) then all react at the same time marking their cards as they see fit whether that's with a marker or a chip or an M&M. What you're doing is having the caller run around to all the players and mark their cards for them.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: Invoke method in nested class using reflection

Posted 27 June 2014 - 05:35 AM

I just realized I may have been jumping the gun when I mentioned the debugging tutorials already given to you. While they are in our tutorials section, I just realized I did not already post my normal FAQ post that details them and provides some quick links. Sorry about that. Here it is, just in case you haven't seen it in any of the other 100 rookie threads nor looked at our tutorials section for debugging tutorials.



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1