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.
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.
source to share
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();
}
}
source to share
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();
}
source to share