Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to cast unknown object? [modified]

How to cast unknown object? [modified]

Scheduled Pinned Locked Moved C#
questiondebuggingjsontutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tesic Goran
    wrote on last edited by
    #1

    Hi, I have this code:

    object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
    object val = ((Csla.Silverlight.PrimitiveCriteria)o).Value;

    As you can see I get some object by deserialization. In this particular case I expect the object that is actually of Csla.Silverlight.PrimitiveCriteria type and I can cast the object in second line as I did above. In this case I know the type of object because I've seen it at debug time. But, what to do if I need to determine the type of object at runtime? What to do in case I want to parametrize the whole process? It means the class I get by deserialization can be different. How can I use that class to cast the object in second line? Thank you in advance. Goran Tesic

    modified on Tuesday, May 10, 2011 6:19 AM

    L S 2 Replies Last reply
    0
    • T Tesic Goran

      Hi, I have this code:

      object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
      object val = ((Csla.Silverlight.PrimitiveCriteria)o).Value;

      As you can see I get some object by deserialization. In this particular case I expect the object that is actually of Csla.Silverlight.PrimitiveCriteria type and I can cast the object in second line as I did above. In this case I know the type of object because I've seen it at debug time. But, what to do if I need to determine the type of object at runtime? What to do in case I want to parametrize the whole process? It means the class I get by deserialization can be different. How can I use that class to cast the object in second line? Thank you in advance. Goran Tesic

      modified on Tuesday, May 10, 2011 6:19 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Tesic Goran wrote:

      How can I use that class to cast the object in second line?

      I'd suggest to use a shared interface/common base class; if all the deserialized classes share something in common, you can use that to access it. That doesn't mean that you're restricted to a single interface; you can ask the object what interfaces are implemented and act based on that information.

      I are Troll :suss:

      T 1 Reply Last reply
      0
      • L Lost User

        Tesic Goran wrote:

        How can I use that class to cast the object in second line?

        I'd suggest to use a shared interface/common base class; if all the deserialized classes share something in common, you can use that to access it. That doesn't mean that you're restricted to a single interface; you can ask the object what interfaces are implemented and act based on that information.

        I are Troll :suss:

        T Offline
        T Offline
        Tesic Goran
        wrote on last edited by
        #3

        Thank you for your answer. In this case, there's a basic interface that Csla.Silverlight.PrimitiveCriteria class implements, but that interface contains only basic methods. For example, Value property is specific for Csla.Silverlight.PrimitiveCriteria class. Other properties can be specific for other classes. What to do in that case?

        L 1 Reply Last reply
        0
        • T Tesic Goran

          Thank you for your answer. In this case, there's a basic interface that Csla.Silverlight.PrimitiveCriteria class implements, but that interface contains only basic methods. For example, Value property is specific for Csla.Silverlight.PrimitiveCriteria class. Other properties can be specific for other classes. What to do in that case?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I'd add my own interface. It doesn't have to be implemented in the CSLA-classes, just in the classes that you're going to serialize. That would give you the option to deserialize 'unknown', check if it matches an known interface and cast it thereto.

          I are Troll :suss:

          T 1 Reply Last reply
          0
          • L Lost User

            I'd add my own interface. It doesn't have to be implemented in the CSLA-classes, just in the classes that you're going to serialize. That would give you the option to deserialize 'unknown', check if it matches an known interface and cast it thereto.

            I are Troll :suss:

            T Offline
            T Offline
            Tesic Goran
            wrote on last edited by
            #5

            The problem is that I can't change the code in the classes I deserialize. They are all in some DLL that I use in my DLL.

            L 1 Reply Last reply
            0
            • T Tesic Goran

              The problem is that I can't change the code in the classes I deserialize. They are all in some DLL that I use in my DLL.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Tesic Goran wrote:

              The problem is that I can't change the code in the classes I deserialize.

              Are they sealed? If not, inherit, wrap and add an interface :)

              I are Troll :suss:

              T 1 Reply Last reply
              0
              • L Lost User

                Tesic Goran wrote:

                The problem is that I can't change the code in the classes I deserialize.

                Are they sealed? If not, inherit, wrap and add an interface :)

                I are Troll :suss:

                T Offline
                T Offline
                Tesic Goran
                wrote on last edited by
                #7

                If I understand you correctly, I could create new class, for example PrimitiveCriteriaNew that inherits from Csla.Silverlight.PrimitiveCriteria and implements new IInterfaceNew in this way:

                public interface IInterfaceNew
                {
                }

                public class PrimitiveCriteriaNew : Csla.Silverlight.PrimitiveCriteria, IInterfaceNew
                {
                }

                And use it in this way:

                object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
                object val = ((PrimitiveCriteriaNew)o).Value;

                Is this correct? If so, if I have 1000 classes in that external DLL, I should do the same 1000 times?

                L 1 Reply Last reply
                0
                • T Tesic Goran

                  If I understand you correctly, I could create new class, for example PrimitiveCriteriaNew that inherits from Csla.Silverlight.PrimitiveCriteria and implements new IInterfaceNew in this way:

                  public interface IInterfaceNew
                  {
                  }

                  public class PrimitiveCriteriaNew : Csla.Silverlight.PrimitiveCriteria, IInterfaceNew
                  {
                  }

                  And use it in this way:

                  object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
                  object val = ((PrimitiveCriteriaNew)o).Value;

                  Is this correct? If so, if I have 1000 classes in that external DLL, I should do the same 1000 times?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Tesic Goran wrote:

                  Is this correct?

                  Looks good to me, although I'd suggest you keep the interface-type after the cast, like this;

                  object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
                  IInterfaceNew val = ((IInterfaceNew)o).Value;

                  Tesic Goran wrote:

                  If so, if I have 1000 classes in that external DLL, I should do the same 1000 times?

                  No, you can probably generalize some interfaces in a way that they can be re-used.

                  I are Troll :suss:

                  1 Reply Last reply
                  0
                  • T Tesic Goran

                    Hi, I have this code:

                    object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin);
                    object val = ((Csla.Silverlight.PrimitiveCriteria)o).Value;

                    As you can see I get some object by deserialization. In this particular case I expect the object that is actually of Csla.Silverlight.PrimitiveCriteria type and I can cast the object in second line as I did above. In this case I know the type of object because I've seen it at debug time. But, what to do if I need to determine the type of object at runtime? What to do in case I want to parametrize the whole process? It means the class I get by deserialization can be different. How can I use that class to cast the object in second line? Thank you in advance. Goran Tesic

                    modified on Tuesday, May 10, 2011 6:19 AM

                    S Offline
                    S Offline
                    Sanjay J Patolia
                    wrote on last edited by
                    #9

                    Hi, Instead of writing : object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin); object val = ((Csla.Silverlight.PrimitiveCriteria)o).Value; You may go with dynamic here: Simple reading and access dynamic o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin); object val = o.Value; You don't need to cast it any where :) For more details about dynamic you may go on this. Perform Reflection and XML Traversing Using the dynamic Keyword in C#[^] This may be helpful to you. Thank You :)

                    T 1 Reply Last reply
                    0
                    • S Sanjay J Patolia

                      Hi, Instead of writing : object o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin); object val = ((Csla.Silverlight.PrimitiveCriteria)o).Value; You may go with dynamic here: Simple reading and access dynamic o = Csla.Serialization.Mobile.MobileFormatter.Deserialize(bin); object val = o.Value; You don't need to cast it any where :) For more details about dynamic you may go on this. Perform Reflection and XML Traversing Using the dynamic Keyword in C#[^] This may be helpful to you. Thank You :)

                      T Offline
                      T Offline
                      Tesic Goran
                      wrote on last edited by
                      #10

                      Thanks a lot.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups