简体   繁体   中英

how to parse for object sender

I have this method and I have no idea what object sender is sending

void xTreve(object sender, Microsoft.SilverlightMediaFramework.Core.CustomEventArgs<Microsoft.SilverlightMediaFramework.Core.Media.PlaylistItem> e)
{
}

how do I check to see what object sender contains

I'm going to take it that your question is geared toward determining what sort of type sender is so you can work with it.

With that in mind, you would first need to get the type of sender , then you can cast it appropriately so you can operate on it.

For example, you can do the following in your method:

if (sender is TypeA)
{
    var iAmA = (TypeA)sender;

    // do something A-ish with sender
}
else if (sender is TypeB)
{
    var iAmB = (TypeB)sender;

    // do something B-ish with sender
}
else
{
    // do something else
}

Alternatively, the following does the same as the preceding:

Type type = sender.GetType();

if (type == typeof(TypeA))
{
    var iAmA = (TypeA)sender;

    // do something A-ish with sender
}
else if (type == typeof(TypeB))
{
    var iAmB = (TypeB)sender;

    // do something B-ish with sender
}
else
{
    // do something else
}

If the object is always going to be of the same type, but you just aren't sure what that type will be, then set a breakpoint inside the function and use the visual studio quickwatch window to inspect it. You will be able to see the control name and other properties of the sender object, as well as it's type. Once you know the type you know what to cast sender as in the code if you need to manipulate it.

If you need to tell which instance sender is it depends if sender has some kind of property to identify it. Consider the following code:

 public void randtest()
 {
     var rand = new Random();
     var obj1 = new object();
     var obj2 = new object();

     if (rand.Next() % 2 == 1)
     {
         method(obj1);
     }
     else
     {
         method(obj2);
     }

 }

public void method(object thing)
{
    //here i have no way to tell if thing is obj1 or obj2;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM