dynamic cast?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie McQuay

    dynamic cast?


    I receive an object from an event and I need to go through a long list
    of types to determine the type to cast to.
    For example

    if (myObject is A)
    {
    propertyGrid.Se lectedObject = (A)myObject;
    }
    else if (myObject is B)
    {
    propertyGrid.Se lectedObject = (B)myObject;
    }


    is there a way to do something like the following?

    Type myType = myObject.GetTyp e();
    propertyGrid.Se lectedObject = (myType )myObject;

    Thanks,

    Jamie
  • Marc Gravell

    #2
    Re: dynamic cast?

    Re my comment about faking it with PropertyDescrip tor, see here:


    Marc

    Comment

    • Jamie McQuay

      #3
      Re: dynamic cast?

      if (myObject is IListSource) { // special case; obtain
      list
      myObject = ((IListSource)m yObject).GetLis t();
      }
      if(myObject is IList) { // lists; show first item
      myObject = ((IList)myObjec t)[0];
      }
      propertyGrid.Se lectedObject = myObject;
      >
      Hi Marc,

      Perfect, this is what I'm looking for... thanks.

      This is different yes... I'm displaying an xml based file format that
      cantains lots of steps and lots of subitems. When I click on a
      collection in the propertygrid I have a custom editor that loads the
      names of each item in the collection into a listBox and the
      propertygrid is set the the first one.

      Thanks everyone,

      Jamie

      Comment

      • Marc Gravell

        #4
        Re: dynamic cast?

        This is different yes... I'm displaying an xml based file format that
        cantains lots of steps and lots of subitems.  When I click on a
        collection in the propertygrid I have a custom editor that loads the
        names of each item in the collection into a listBox and the
        propertygrid is set the the first one.
        If you insist... but PropertyGrid has good support for UITypeEditor
        and TypeConverter [noting in particular GetStandardValu es] which can
        be made to do an awful lot of what you have just described... but if
        you are happy...

        Marc

        Comment

        • Jamie McQuay

          #5
          Re: dynamic cast?

          If the item is indexed in some way, does it implement an interface that
          you can cast it to, instead of having to cast to the specific instance?
          Something like IList, or ICollection<T>? That way, you can access any
          collection that implements one of those interfaces and then use the indexer
          on the interface.
          Using the IList interface did the trick... thanks.

          Comment

          Working...