|
Alrighty, I'll check it out! Thanks for the info.
|
|
|
|
|
Hello,
I have developed a .NET application for three years. This application
builds several types of objects, and serialize them with the Binary formatter.
Everything has been fine, until...
Now, I need to build another application, independant from the first
one, that needs to access some of the serialized objects built with the first application.
Apparently, even though I define my objects with the same structure (even
the same source!), the 2nd application's deserializer refuses to load the objects:
.NET is looking for the assembly of the *original* application.
Therefore I rewrote the first application, with a DLL responsible for these
objects processing, including their serialization/deserialization. I was hoping that this
DLL, if shared by the two applications, would accept in both cases to load & save these
objects (because it would be the same assembly, right?).
But this DLL, when run by the two applications (even the first one), refuses to
deserialize anything (I guess the new version of my first application has not the
same "assembly" as when it did not have the DLL...)
Is there any way to tell .NET that another application needs to access the data?
I cannot believe that the serialization/deserialization system would not allow
two independant applications to share data stored in a file...
Any help would be welcome!
Max Silberztein
(please cc your answer to msilberz@univ-fcomte.fr)
|
|
|
|
|
You can deserialize to a type other than the original by assigning an instance of a class which inherits from SerializationBinder[^] to the BinaryFormatter 's Binder property.
You only need to override the BindToType method and return the type of the object you'd like to instantiate.
The simplest case would be:
public class Binder : SerialiationBinder
{
public Binder(){}
public Type BindToType(string assembly, string type)
{
return typeof(WhateverClassYouAreTryingToDeserializeAs);
}
}
BinaryFormatter formatter = new BinaryFormatter();
formatter.Binder = new Binder();
WhateverClassYouAreTryingToDeserializeAs wcyattda = (WhateverClassYouAreTryingToDeserializeAs)formatter.Deserialize(stream);
Charlie
if(!curlies){ return; }
|
|
|
|
|
Charlie Thanks for your help!
(1) When I use the method:
public Type BindToType (string assemblyName,string typeName)
{
return typeof (Grammar);
}
I get the exception message:
Possible Version mismatch. Type Nooj.Grammar has 36 members, number of members deserializes is 1.
but I have not changed anything in the definition of "Grammar"! There were
36 members, and they all are still there...
(2) If I use another version of the method (got from MSDN):
public Type BindToType (string assemblyName,string typeName)
{
Type typeToDeserialize = Type.GetType (String.Format("{0}, {1}", "Grammar", assemblyName));
return typeToDeserialize;
}
I then get the Exception message:
Type is not resolved for member Nooj.Grammar, Nooj, Version=1.0.1916.21705, Culture=neutral, PublicKeyToken=null.
(3) I noticed in MSDN that their new Version2Type (i.e. my "new" Grammar type) follows the interface "ISerializable". Therefore I tried to implement it (without the [] otherwiser it does not compile):
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("x", x);
info.AddValue("y", y);
// ... all the 36 fields
}
as well as:
private Grammar (SerializationInfo info, StreamingContext context)
{
x = info.GetInt32("x");
y = info.GetInt32("y");
// ... this would not be feasible because some of the fields are complex recursive objects...
}
but then I got the exception message:
Exception has been thrown by the target of an invocation
Any idea?
--Max
|
|
|
|
|
I finally found out the solution: because my object Nooj.Grammar contains
fields that are other objects, I need to make sure BindToType returns
each correct type (rather than just Nooj.Grammar).
public Type BindToType (string assemblyName,string typeName)
{
if (typeName == "Nooj.Grammar") return typeof (Grammar);
else if (typeName == "Nooj.GramType") return typeof (GramType);
else if (typeName == "Nooj.Graph") return typeof (Graph);
...
else return Type.GetType (typeName);
}
--Max
|
|
|
|
|
Does anyone know how to intercept the Paste operation (on ctrl+V) in a RichTextBox?
It appears that the RichTextBox is different from a TextBox in this way.
public override void WndProc(ref Message m)
{
if(m.Msg == 0x0302)
MessageBox.Show("Paste");
base.WndProc(ref m);
}
This chunk only works when you call the Paste() method directly. It does not work on Ctrl+V or Ctrl+Insert.
|
|
|
|
|
Well this is an answer without checking anything:
I would catch the key input (keydown event), check if you the user typed ctrl+v and then paste.
|
|
|
|
|
You can use Key Down event
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control && e.KeyCode==Keys.V)
{
}
}
You can get and Set data using Clipboard class
MCAD
|
|
|
|
|
Wow, that was simple.
Just Catch Ctrl+V then Call Paste(), and handle paste in the WndProc Method.
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control && e.KeyCode==Keys.V)
{
Paste();
e.Handled = true;
}
}
Thanks.
|
|
|
|
|
i have an application that should read the any pressed keys of keyboard in any other application. i guess i should read that pressed key from the windows but i dont know hao to do that.
plz help me.
Thanks
Rastegar
|
|
|
|
|
|
Thanks alot
Thanks
Rastegar
|
|
|
|
|
Hello gurus,
I'd like to know how to build a byte (or a word or double word) from a BitArray? Can someone show me a code snippet please?
Best regards.
Fred.
There is no spoon.
|
|
|
|
|
Loop throught the number of items needed to build the value, shift the value to the right and add the bit.
int b = 0;<br />
for (int i=0; i<8; b+=theArray[i++]?128:0) b>>=1;
---
b { font-weight: normal; }
|
|
|
|
|
Hello Guffa;
How can I access to a given bit of a byte?
I wish to switch on or off a given bit in a byte...
Thanks
Fred.
There is no spoon.
|
|
|
|
|
Use or to switch on bits, and to switch of bits, and xor to alter bits.
The value of the bits are:
First: 1
Second: 2
Third: 4
Fourth: 8
Fifth: 16
Sixth: 32
Seventh: 64
Eight: 128
So to turn off the fifth bit:
b = b and (255-16);
---
b { font-weight: normal; }
|
|
|
|
|
ok thanks
Fred.
There is no spoon.
|
|
|
|
|
|
Hello,
How can I make ListBox a multi select? And how can I itterate through the selected items using foreach statement?
Thank you.
|
|
|
|
|
Set the SelectionMode property to Multiple.
A foreach statement would look like this:
foreach (ListItem item in TheListBox.Items) {<br />
if (item.Selected) ...<br />
}
---
b { font-weight: normal; }
|
|
|
|
|
To make ListBox multiselect set SelectionMode
to SelectionMode.MultiSimple Or SelectionMode.MultiExtended
To get selected indexs use SelectedIndices property
MCAD
|
|
|
|
|
To use foreach with SelectedIndices
foreach(int i in listBox1.SelectedIndices)
{
MessageBox.Show(listBox1.Items[i].ToString());
}
this will get the selected items from listbox
MCAD
|
|
|
|
|
I am using a DataGrid on an ASP page and have enabled the EditCommand. When editing the textbox that comes up upon clicking Edit, and upon clicking Update, the TextBox does not contain the edited text, but instead the original text. What gives? Any ideas?
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox newdesc = (TextBox)e.Item.Cells[2].Controls[0];
String desc = newdesc.Text;
Response.Write(desc);
DBImage dbImage = new DBImage(ConfigurationSettings.AppSettings["ConnectString"]);
dbImage.UpdateDesc(id,desc);
this.DataGrid1.EditItemIndex = -1;
this.DataGrid1.DataBind();
}
|
|
|
|
|
In your line dbImage.UpdateDesc(id, desc) where does the variable id get set to specify which record gets updated?
|
|
|
|
|
You will have to call EndEdit before DataGrid1_UpdateCommand gets executed.
A little code snippet might help,
CurrencyManager currencyManager = (CurrencyManager) this.dataGridFormulaFee.BindingContext[this.dataTable];
if (currencyManager != null)
{
currencyManager.Position = this.currentGridRow;
DataGridColumnStyle dataGridColumnStyle = this.dataGridFormulaFee.TableStyles[0].GridColumnStyles[0];
this.dataGridFormulaFee.EndEdit(dataGridColumnStyle, this.currentGridRow, false);
}
Live Life King Size
Alomgir Miah
|
|
|
|