C # display problem: how to set a class if it has an object called

I have multiple instances of a class in my code, each with diff configurations, etc. This class has a delegate event handler method that accepts an event from the sender and then needs to perform some action on one of the above objects.

I would like to know if reflection can solve this.

private CatClass cat1;
private CatClass cat2;
private CatClass cat3;

      

I would like to avoid checking each instance independently to see if I need an object. But instead, I would like to somehow restore the instance of the object that identified the dispatch event.

So

private void delegateHandler(string senderName, object sender, CustomEventType evt)
{
     // given reciept of the senderName string,
     // how do I perform an action on say the cat2 object

    // Is it possible to ask the class if it has an object defined
    // called 'cat2' and then proceed with performing an action on that object
}    

      

Thanks for the help.

0


source to share


6 answers


Is it very unlikely to achieve what you want? Out of curiosity, the sender won't give you a proper example? If not, then you should put the fired instances of CatClass into a dictionary and look up that dictionary as needed.



+1


source


C # works best when you "tell", not when you "ask".

In other words, it would be better to have an interface or base class in which a member is declared CatClass

that will allow you to work with a variable of that interface or base class type instead of using reflection. My only concern is that since the members you are putting on to use are private, this will prevent you from using the interface.



Are you really using a handler that digs into the sender through reflection to fetch private members? Perhaps you should reconsider your approach.

0


source


You can use reflection for this, but I would suggest not. Reflection is slow, and this is hardly a situation where it is needed.

Why not have a dictionary (if you want to look at them by name), a list or an array of CatClass and iterate over them to find out which one you want?

0


source


Well, like the posts mentioned above, Reflection is generally a bad idea.

this.GetType().GetMember(senderName) == null;

      

Will probably work here.

0


source


I doubt you even care about it, but your specific question on how to handle this in reflection was never given. This is basically how you do it.

private void delegateHandler(string senderName, object sender, CustomEventType evt)
{
    FieldInfo field = this.GetType().GetField(senderName, BindingFlags.NonPublic | BindingFlags.Instance);
    if(field != null && field.Name == senderName)
    {
        CatClass cat = (CatClass)field.GetValue(this);
        cat.DoWork();
    }
}

      

0


source


There are various ways to solve this problem. Reflection is definitely not the right choice.

One approach is to use an array of cats

Cat[] cats = new Cat[] { new Cat(), new Cat(), new Cat() };

      

Instead of working with cat names, you will work with cat numbers

private void delegateHandler(int catNumber, object sender, CustomEventType evt)
{
    DoSomethingWith(cats[catNumber]);
}

      

Another way is to pass a cat as sender (which is the usual way of doing things with event handlers)

private void delegateHandler(object sender, CustomEventType evt)
{
    Cat cat = (Cat)sender;
    DoSomethingWith(cat);
}

      

Another way is to give each cat a property name

public class Cat
{
    public string Name { get; set; }

    // Other properties and methods       
}

      

then use a dictionary to store cats

var catsByName = new Dictionary<string, Cat>();
Cat cat = new Cat { Name = "Tiger" };
catsByName.Add(cat.Name, cat);
...

      

Then you can get a cat by name like this

cat = catsByName["Tiger"];

      

Or if you are not sure if a cat exists

if (catsByName.TryGetValue("Misty", out cat)) {
    DoSomethingWith(cat);
} else {
    CatNotFound();
}

      

0


source







All Articles