locked
Reflection or not for casting to child type. RRS feed

  • Question

  • This question relates to a solution I need for a Silverlight Application accessing a c# Web Service (NOT WCF), but I think it is of a more generic nature so I'm not asking the question on the Silverlight forum(s)

    In my Web Service I have a class WebMethodResult

    Most Web Methods in the service returns a type which inherits from WebMethodResult, for example

    public class GetVendorDetailsResult : WebMethodResult {...}

    In my Silverlight Application I want to check the properties (relating to the WebMethodResult base class) set in GetVendorDetailsResut, and all other Result classes which inherits from WebMethodResult in a generic manner.

    The method to check all result classes which inherit from WebMethodResult is:

    public static string CheckWebResult(AsyncCompletedEventArgs e)

    {

            if (e.Error != null) { .... } // Can check e.Error, but e.Result does not expose...

            //e.Result not visible here because e.Result is semi-dynamically created in Reference.cs and only relates

            // to child classes of AsyncCompletedEventArgs such as GetCreditDetailsCompletedEventArgs

            // CreateInstanceOfChildOfAsyncCompletedEventArgs (eg. GetCreditDetailsCompletedEventArgs) Type here

            // to be able to check properties set in e.Result which will have a base type of WebMethodResult.

            // Use Reflection or not ? And if so, to which extent is it needed ? What will the overhead be ?

    }

    One of the methods which will call CheckWebResult is:

    void _webService_GetCreditDetailsCompleted(object sender, GetCreditDetailsCompletedEventArgs e)
     {

                // e contains member e.Result which is of type GetCreditDetailsResult which inherits from WebMethodResult

                  WaterWatchApplication.CheckWebResult(e);

                ...

    }

    For some or other reason, none of the classes in the WebService reference (resultsets or other) are visible from the CheckWebResult method, only the async methods and completed events expose. I thought it was because I had a static class and CheckWebResult is static as well, but still I have static properties where I can obviously access the type such as:

    private static MyWebServiceReference.WebMethodResult  _webMethodResult = new WaterWatchService.WebMethodResult(); // Needed for type casting ???

    So the question is, how do I cast   AsyncCompletedEventArgs e in method CheckWebResult to   GetCreditDetailsCompletedEventArgs (or GetVendorDetailsCompletedEventArgs, or in fact any of the many CompletedEventArgs types) by knowing the child type through AsyncCompletedEventArgs e.GetType().ToString()   ?

    Casting AsyncCompletedEventArgs e to WebMethodResult would be preferable if possible....



    • Edited by Etienne Marais Monday, November 4, 2013 4:10 PM
    • Moved by Eason_H Tuesday, November 5, 2013 1:35 AM
    Monday, November 4, 2013 3:55 PM

All replies

  • Hi Etienne,

    Since the issue regards ASP.NET and website deployment. I suggestion you post the question in the ASP.NET forums at http://forums.asp.net/. It is appropriate and more experts will assist you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 5, 2013 1:35 AM
  • Hi Etienne,

    Since the issue regards ASP.NET and website deployment. I suggestion you post the question in the ASP.NET forums at http://forums.asp.net/. It is appropriate and more experts will assist you.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Not sure why this was moved. The question has more to do with c# OOB casting techniques than Silverlight or ASP.
    Tuesday, November 5, 2013 9:28 AM