|
I was trying to convince myself more than you I think
I knew I read it somewhere once, and testing confirmed it.
Its obscurely "documented" here[^].
I appreciate the discussion, thanks!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
Thanks for the link, it was quite instructive, although I couldn't achieve useable results so far with a SerializationBinder...
Nevertheless: thanks for taking your time.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
dogAsAnimal is still a Dog object, regardless of the "cast".
You can see that in the debugger.
"as" doesn't create a new object of a different type.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Well, the debugger shows "Animal {Dog}" as type for dogAsAnimal . a yields "Animal" and d yields "Dog", so I expected dogAsAnimal to be an Animal -type reference to an object that is Dog .
It's the same with a direct cast.
I cannot access the Dog -only properties of dogAsAnimal , but the BinaryFormatter seems to see through the cast and serialize the deepest inheritance.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
What if you make the Dog class [Serializable], mark all its members
[NonSerialized], and use a SerializationBinder[^]
for deserialization?
I'm not sure if it will work or not
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the suggestion, I'll take a look.
Jeez, I didn't expect it to get so complicated...
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
I was bored, so I tried it. Works great
[Serializable]
public class Animal
{
public string str
{
get;
set;
}
}
[Serializable]
public class Dog : Animal
{
[NonSerialized] <code>
public Int32 A;
[NonSerialized] <code>
public Int32 B;
public Dog()
{
A = 5;
B = 10;
}
}
sealed class DogToAnimalDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;
String assemVer1 = Assembly.GetExecutingAssembly().FullName;
String typeVer1 = "Dog";
if (assemblyName == assemVer1 && typeName == typeVer1)
{
typeName = "Animal";
}
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
...
Dog dog = new Dog();
dog.A = 3;
dog.B = 5;
dog.str = "Animal String";
MemoryStream MemStream = new MemoryStream();
BinaryFormatter Serializer = new BinaryFormatter();
Serializer.Serialize(MemStream, dog);
MemStream.Seek(0, SeekOrigin.Begin);
Serializer.Binder = new DogToAnimalDeserializationBinder();
Animal animal = (Animal)Serializer.Deserialize(MemStream);
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
Hmmm, I've played around with the SerializationBinder a bit, but didn't get a useable result either.
If I always return typeof(Animal) as target type in BindToType , then the deserializing end doesn't require knowledge about Dog , but unfortunately it doesn't work because then even Animal 's properties aren't deserialized if I have a Dog object in my serialization stream.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Right.
The only way I can see to do it, if the deserialization end has
no access to the Dog class (or an equivalent class), is to add a
method to Dog that returns an Animal object and serialize that object.
It's really unconventional anyway....it generally doesn't make sense
to only serialize the base class portion of an object. But if you have
a special need I suppose you need a special solution.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I think I got it!
After your and Giorgi's hints concerning the SerializationBinder , I looked at BinaryFormatter 's other properties and found SurrogateSelector and subsequently ISerializationSurrogate .
Using these two interfaces I was able to control the serialization process in a way that only Animal 's properties are being serialized and during deserialization a real Animal object is being created.
This should do the trick...
Thanks for taking your time!
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Very cool! Thanks for the update!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm writing my first VSTO Outlook add-in. Basically "Hello World". It's VS 2008 targeting Outlook 2007. It's a standard MS VS build/publish.
In the startup handler I've got:
try
{
Office.CommandBar commandBar = activeExplorer.CommandBars["Standard"];
button = (Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing, missing, missing);
button.Style = Office.MsoButtonStyle.msoButtonCaption;
button.Caption = "TEST";
button.Tag = "btnProcess";
return true;
}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
'button' is a local Office.CommandBarButton object.
It installs nicely and puts a 'TEST' button on the standard toolbar as expected. But when I uninstall via Control Panel, the button is still there when I restart Outlook.
I guess I need something in the shutdown handler to kill the button - but what? Office.CommandBarButton class does not have a dispose event. How come the button is still there after uninstallation, and how do I solve that? I'm a bit confused here. Help!
JustAStupidGurl
|
|
|
|
|
Hi My Friends,
I need free C# TreeList code that will run on the web.
ASAP please. Thanks.
|
|
|
|
|
inerp wrote: ASAP please. Thanks.
hahaha... *breath* good one.
Im sorry, but we dont write code for you. Can I suggest heading over to rentacoder.com or google. take your pick.
|
|
|
|
|
Just leave your e-mail and whole codeproject community will send the code immediately.
|
|
|
|
|
Try the ASP.NET forum then!
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - coming soon ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
Dear friend,
It is clear that You have just registered to codeproject few minutes ago
We here do not do the job on behalf of you
Pleas, be more specific..
Try to write the code, when you face a trick post it with your sample code
Please read the most above message on the board with the title: "How to get an answer to your question" by Mr. Chris (CP Admin)
foreach(Minute m in MyLife)
myExperience++;
|
|
|
|
|
Clearly defining the problem is 90% of the process. Now, write the code.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hello Everyone,
I have a secenerio :
In my project I call a webservice,it has several methods.
I have a globals class which is purely static and its method are also static.
Now scene is that I want to access the webservice method into my Global class's method, so it is not possible to create the webservice object and call it into the static function because static call only static class and method.
But I just do one R&D i defined the webservice object as static as follows
static WebServiceClassName obj=new WebServiceClassName();
and I supersied that now obj is call every method from the webservice into the static function.
so it is a good approach to define an object as static whenever the class is not static.
So my question is that
Can I create an object as static as the point of oops or this is good for the project.
Deepak
Smile a Lots,Its Costs Nothing
|
|
|
|
|
Deepak the Cool wrote: it is not possible to create the webservice object and call it into the static function because static call only static class and method
Not true. Static methods can call non-static methods if it has a reference to the non-static object.
Deepak the Cool wrote: Can I create an object as static as the point of oops or this is good for the project.
Not sure what you mean by that.
You can create a static reference to an object if you need it. However, be aware that the object will be around for the life of the application until you set the reference to null even if nothing else references it.
|
|
|
|
|
I need to implement centralized exception handler and logger (for code reuse) with the following features:
- Class must record the exception in event log, log file...
- Class must inform user about exception in user friendly way.
-Class must send report to development team about critical errors.
I need some design and implementation guidelines about this problem.
matix
|
|
|
|
|
Have a look at Enterprise Library[^]
log4net is also worth considering.
matixsc wrote: - Class must inform user about exception in user friendly way.
I don't think all classes should be informing the user, the main thing is to get exceptions logged and not to throw away information stored in the exception. Only classes that are responsible for UI should communicate with users.
|
|
|
|
|
Giorgi Dalakishvili wrote: log4net is also worth considering.
I'll include log4net as an option too.
This class should be configurable enough to let caller decide what will be logged, where to log, what information enduser will see...
So, I need design guidelines, best practices, source code is also welcomed in order to fulfil my task.
Regards, matixsc
|
|
|
|