How to cast unknown object? [modified]
-
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
-
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
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:
-
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:
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?
-
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?
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:
-
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:
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.
-
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.
-
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:
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?
-
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?
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:
-
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
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 :)
-
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 :)
Thanks a lot.